Search code examples
c++cshared-librariesdlopenldd

Check C++ shared dependencies at runtime (ldd on self)


Is there a way to check or access the names of the libraries a C++ binary is dynamically linked to within the binary itself?

Essentially I want to run ldd on a binary, without running ldd.

The use case is using dlopen from dlfcn.h. I have a C++ library which is linked to libpython, but I don't know if its libpython3.5m.so, libpython3.4m.so, libpython2.7.so, etc...

I want to call

void* handle = dlopen( "@PYTHON_LIBRARY@", RTLD_LAZY | RTLD_GLOBAL );

where "@PYTHON_LIBRARY@" resolves to the path to whatever the current libpython the module is currently dynamically linked to.

Is there any way to do this?


Solution

  • I want to call void *handle = dlopen("...", ...)

    Presumably you want to then call dlsym(handle, "SomePythonSymbol").

    That is a pointless thing to do. Instead of performing dynamic lookup, simply call the SomePythonSymbol directly.

    To answer your original "what version of libpython am I linked to" question, on a GLIBC-based system you can use dl_iterate_phdr to enumerate all currently loaded shared libraries.

    If you already have a symbol that you know is defined in libpython, dladdr1 will make finding the library even easier.