Dlopen () with library dependencies

My program uses plugins that are dynamically loaded using dlopen (). The location of these plugins can be arbitrary, so they are not necessarily in the library path. In some cases, one plugin must depend on another plugin. So if A and B are dynamic libraries, I will first load A and then load B, which uses the symbols defined in A.

My reading of the dlopen () documentation implies that if I specify RTLD_GLOBAL it all works. But this is not the case. When I call dlopen () on the second library, it fails with the message that it cannot find the first one (which has already been loaded with dlopen ()):

Error loading library /usr/local/openmm/lib/plugins/libOpenMMRPMDOpenCL.dylib: dlopen(/usr/local/openmm/lib/plugins/libOpenMMRPMDOpenCL.dylib, 9): Library not loaded: libOpenMMOpenCL.dylib
Referenced from: /usr/local/openmm/lib/plugins/libOpenMMRPMDOpenCL.dylib
Reason: image not found

      

How can I make this work?

+3


source to share


1 answer


See this answer here: Image dlopen () not found

If you change the library to be named @ rpath / blah.dylib, you should be able to do so.

Edit:



I am also using cmake, use this:

set_target_properties(${MY_LIB} PROPERTIES BUILD_WITH_INSTALL_RPATH 1 INSTALL_NAME_DIR "@rpath")

      

This also doesn't break on other platforms, but make sure you haven't called CMAKE_SKIP_RPATH or it won't be called.

+1


source







All Articles