From b0d801d715fad8104ed36e1c96de347009e2bf3e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vitaliy=20Cherepanov/SRR-AI=20Tools=20Lab/=2E/=EC=82=BC?= =?utf8?q?=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 17 May 2018 11:38:47 +0300 Subject: [PATCH] nnc: dynamic library interface (#228) * nnc: dynamic library interface This commit implements interface for loading dynamic libraries and calling its interfaces. This functional will be used to load and communicate with plugins. Signed-off-by: Vitaliy Cherepanov * nnc: dynamic library interface move sources to contrib/nnc Signed-off-by: Vitaliy Cherepanov * nnc: dynamic library interface fix namespaces and code style Signed-off-by: Vitaliy Cherepanov * nnc: dynamic library interface fix build Signed-off-by: Vitaliy Cherepanov * nnc: dynamic library interface add info to "<<" operator Signed-off-by: Vitaliy Cherepanov * nnc: dynamic library interface fix issues Signed-off-by: Vitaliy Cherepanov * nnc: dynamic library interface fix issues Signed-off-by: Vitaliy Cherepanov --- contrib/nnc/CMakeLists.txt | 1 + contrib/nnc/include/module/plugin/shared_library.h | 38 ++++++++++++ contrib/nnc/src/module/plugin/shared_library.cpp | 72 ++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 contrib/nnc/include/module/plugin/shared_library.h create mode 100644 contrib/nnc/src/module/plugin/shared_library.cpp diff --git a/contrib/nnc/CMakeLists.txt b/contrib/nnc/CMakeLists.txt index aea34cd..cd2c828 100644 --- a/contrib/nnc/CMakeLists.txt +++ b/contrib/nnc/CMakeLists.txt @@ -5,6 +5,7 @@ file(GLOB_RECURSE CL_SRC src/*.cpp include/*.h) add_executable(nnc ${CL_SRC}) target_link_libraries(nnc PRIVATE nncc_core) target_link_libraries(nnc PRIVATE nncc_foundation) +target_link_libraries(nnc PRIVATE dl) target_include_directories(nnc PUBLIC include) add_subdirectory(libs) diff --git a/contrib/nnc/include/module/plugin/shared_library.h b/contrib/nnc/include/module/plugin/shared_library.h new file mode 100644 index 0000000..24e9029 --- /dev/null +++ b/contrib/nnc/include/module/plugin/shared_library.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +namespace nncc +{ +namespace contrib +{ +namespace module +{ +namespace plugin +{ + +class SharedLibrary +{ +public: + explicit SharedLibrary(const std::string &fullPath); + + void *findFunc(const std::string &funcName); + std::string getPath() const; + + +private: + bool loadLibrary(); + +private: + std::string _path; + void *_handle; + bool _isLoaded; +}; + +std::ostream &operator<<(std::ostream &st, const SharedLibrary &lib); + +} // namespase plugin +} // namespace module +} // namespace contrib +} // namespace nncc diff --git a/contrib/nnc/src/module/plugin/shared_library.cpp b/contrib/nnc/src/module/plugin/shared_library.cpp new file mode 100644 index 0000000..b3a8cf8 --- /dev/null +++ b/contrib/nnc/src/module/plugin/shared_library.cpp @@ -0,0 +1,72 @@ +#include +#include + +#include "module/plugin/shared_library.h" + +namespace nncc +{ +namespace contrib +{ +namespace module +{ +namespace plugin +{ + +SharedLibrary::SharedLibrary(const std::string &fullPath) + : _handle(nullptr), _path(fullPath), _isLoaded(false) +{ +} + +void *SharedLibrary::findFunc(const std::string &funcName) +{ + if (!loadLibrary()) + { + return nullptr; + } + + // reset errors + dlerror(); + + void *func = dlsym(_handle, funcName.c_str()); + char *dlsym_error = dlerror(); + + if (nullptr != dlsym_error) + { + std::cout << "Cannot load symbol '" << funcName << "': " << dlsym_error << std::endl; + return nullptr; + } + return func; +} + +bool SharedLibrary::loadLibrary() +{ + if (nullptr == _handle && !_isLoaded) + { + _isLoaded = true; + + // open the library + std::cout << "Opening " << _path << std::endl; + + _handle = dlopen(_path.c_str(), RTLD_LAZY); + + if (!_handle) + { + std::cout << "Cannot open library <" << _path << ">: " << dlerror() << std::endl; + } + } + + return (nullptr != _handle); +} + +std::string SharedLibrary::getPath() const { return _path; } + +std::ostream &operator<<(std::ostream &st, const SharedLibrary &lib) +{ + st << lib.getPath(); + return st; +} + +} // namespase plugin +} // namespace module +} // namespace contrib +} // namespace nncc -- 2.7.4