I have a need for certain functions in a shared library to know the path to that library in order to find various resources. What I have in source.cpp:
#include
#include
static std::string myPath;
__attribute__((constructor))
void SetPath() {
Dl_info dl_info;
dladdr((void*)SetPath,
myPath = dl_info.dli_fname; //
As the comment shows, when built with optimizations enabled, that line produces a segfault. With no optimizations, it works just fine. What magic must be done in order to ensure that this variable exists before the "constructor" function is called when loading the shared library?