问题
Is there a way to dynamically link shared libraries that have dependencies?
For example, I have two libraries, libA.so and libB.so. libB.so calls functions defined in libA.so.
In my main program, I wish to load the two libraries with dlopen. However, if I try:
dlopen(libA.so);
dlopen(libB.so);
Then the second dlopen will fail as libB has unrecognized symbols.
I can think of a few workarounds such as building all the object files into a single shared library, or to have libB.so call dlopen on libA.so, but that's extra work.
I guess the way I'm imagining this to work is like in the case of kernel modules where you can use "EXPORT_SYMBOL()" to allow other modules to call functions defined in a previously loaded module.
Can something similar be done with shared libraries? Or will I have to use my workarounds?
回答1:
I experienced a similar situation, this is what worked for me (using a gcc toolchain):
When you create the shared object libB.so, you unconditionally link the library libA.so, the command should look like this:
gcc -shared -Wl,--no-as-needed -lA -o libB.so b.o
You can then check that libA.so indeed became a dependency for the dynamic linker:
$ ldd libB.so
linux-gate.so.1 => (0xb77ba000)
libA.so => not found
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75f7000)
/lib/ld-linux.so.2 (0xb77bb000)
In your main program it should be sufficient to only dlopen() the library libB.so, and the other library should get linked automatically by the dynamic linker.
回答2:
Have you tried using RTLD_GLOBAL?
RTLD_GLOBAL The symbols defined by this library will be made available for symbol resolution of subsequently loaded libraries.
This should work fine even if B depends on A:
void * const handleA = dlopen("libA.so", RTLD_NOW | RTLD_GLOBAL);
void * const handleB = dlopen("libB.so", RTLD_NOW);
来源:https://stackoverflow.com/questions/26619897/dynamic-linking-of-shared-libraries-with-dependencies