开发者

Dynamic Loading: Undefined Symbol In Shared Static Library

I have an executable that loads .so plugins.

The executable is linked with -rdynamic so that symbol callback can occur.

I have a static library that is included in the executable. This has a function called BLAH_hello() in the .a

The static library is not used in the executable. ie there are no calls to BLAH_hello() in the executable code.

However, the .so does have calls to BLAH_hello().

When I dlopen() the .so it complains about an undefined symbol to BLAH_hello()

If I include a dummy call to BLAH_hello() in the executable code, like BLAH_hello(NULL);. The symbol is included in the executable and when the .so is loaded it finds the symbol.

I'm sure I could also link the .so against the .a but multiple dynamically loaded .so's use the BLAH_hello call so it makes sense to have it in the executable. I'm also worried about symbol conflict if I link the library into each .so.

So what I'm wondering, is how to get the symbols of the .a into the executable even if they aren't actually used in the execu开发者_高级运维table?


When I dlopen() the .so it complains about an undefined symbol to BLAH_hello()

If I include a dummy call to BLAH_hello() in the executable code, like BLAH_hello(NULL);. The symbol is included in the executable and when the .so is loaded it finds the symbol.

That is completely normal and expected. It's just how linkers work with archive libraries.

If you must include BLAH_hello into the main executable, add -Wl,-u,BLAH_hello to the executable link line.


@nbt:

Linking the .so against the .a is the obvious and correct thing to do.

This should not generate symbol conflicts when loading the .so into an executable.


As mentioned above, the linker discards .o files with no referenced symbols. This is problem, when the executable needs to dlopen() external shared libs. In such case the linker options '--whole-archive' and '--no-whole-archive' can be used, when linking the executable. All .a libs between these options will have all symbols included. It increases the executable size, but removes the need to link to executable's static libs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜