Reputation: 183
Lets as a example i have a dylib named as libtest.dylib and i am loading this via dlopen(/usr/local/path/libtest.dylib, RTLD_NOW|RTLD_GLOBAL) with a separate process.
Now inside the libtest.dylib, i have a method called as std::string find_loaded_dylib_path() which should return me the path of the dylib which has been loaded.
Can anybody help me out how can i do that? If you have any sample program that can be great help
Platform: Mac OSX Language: C++, C
thanks,
Upvotes: 0
Views: 847
Reputation: 539925
You can use dladdr()
, which gives information about the image containing a given symbol.
A C solution (which I am more familiar with than C++) could look like this:
#include <dlfcn.h>
char *my_dylib_path(void)
{
Dl_info info;
if (dladdr(my_dylib_path, &info) != 0) {
return strdup(info.dli_fname);
} else {
// this should not happen :-)
return NULL;
}
}
The idea is that "my_dylib_path" is a symbol in the dynamic library itself.
I have tested this with a "normal" shared library, but it should work with dynamic library loaded
via dlopen()
as well.
Upvotes: 1