21static dlhandle load_lib(
const std::string& path);
22static void close_lib(
dlhandle handle);
25#define ERR_DLNOTFOUND 1001
26#define ERR_FACTORYNOTFOUND 1002
27#define ERR_DLHANDLENOTFOUND 1003
54 std::string dlFileName;
61 bool doesFileExists(
const std::string& name) {
63 return (stat(name.c_str(), &buffer) == 0);
67 std::shared_ptr<GLogger> log;
82 DynamicLib(std::shared_ptr<GLogger> logger, std::string path) : dlFileName(path), log(logger),
handle(nullptr) {
84 log->debug(
NORMAL,
"Trying ", dlFileName);
87 if (!doesFileExists(dlFileName)) {
88 log->debug(
NORMAL, dlFileName,
" not found...");
91 dlFileName = gemcRoot.string() +
"/lib/" + path;
93 log->debug(
NORMAL,
"Trying ", dlFileName);
97 if (!doesFileExists(dlFileName)) {
98 log->debug(
NORMAL, dlFileName,
" not found...");
101 dlFileName = gemcRoot.string() +
"/build/" + path;
103 log->debug(
NORMAL,
"Trying ", dlFileName);
106 if (doesFileExists(dlFileName)) {
107 handle = load_lib(dlFileName);
109 char const*
const dlopen_error = dlerror();
110 log->error(
ERR_DLHANDLENOTFOUND,
"File ", dlFileName,
" found, but handle is null. dlopen_error >> ",
113 else { log->info(0,
"Loaded ", dlFileName); }
115 else { log->error(
ERR_DLNOTFOUND,
"could not find ", dlFileName); }
133 log->debug(
DESTRUCTOR,
"Destroying ", dlFileName);
150dlhandle load_lib(
const std::string& lib)
155 if (lib.find(
'/') != std::string::npos) { h = dlopen(lib.c_str(), RTLD_NOW); }
158 std::string cwdPath =
"./" + lib;
159 h = dlopen(cwdPath.c_str(), RTLD_NOW);
163 if (!h) { h = dlopen(lib.c_str(), RTLD_NOW); }
172static void close_lib(
dlhandle handle) { dlclose(handle); }
#define ERR_DLHANDLENOTFOUND
void * dlhandle
Opaque handle returned by dlopen and consumed by dlsym / dlclose.
std::filesystem::path gemc_root()
Helper that loads a shared library and holds its POSIX handle.
DynamicLib(std::shared_ptr< GLogger > logger, std::string path)
Construct and attempt to load a dynamic library.
dlhandle handle
POSIX handle of the dynamic library.
~DynamicLib()
Destructor closes the library handle (if loaded).
DynamicLib()=default
Default constructor (does not load anything).