Linking against multiple shared libraries that all

2019-04-25 07:47发布

问题:

Say you have 2 share libraries, lib1.so and lib2.so, that both have libcommon.a statically linked into them. Would the compiler complain about ambiguous symbol reference if you were to dynamically link both lib1.so and lib2.so? Or would be the compiler be smart enough to know libcommon symbols are shared between lib1 and lib2 and allow you to dynamically link against both?

回答1:

The static library would be used to resolve the links internally but the external linkage would not be propagated to the shared library interface, so there would be no conflict. Each shared library would include its own copy of the static library code.



回答2:

There won't be a conflict because when you link to shared libraries, the linker will use the definition from the first shared library which provides the symbol and won't look further at the other shared libraries. Symbols included from the .a will be exported in both shared libraries but won't conflict.



回答3:

Suppose the two shared libraries linked with the different static libraries. But the static libraries both contain a function with the same name. There would be conflict.

I'm sure of that because I have a tcl/tk application, it load two tcl libraries(.so). Both of the libraries are static linked with the openssl library. but with different version. A segmentation fault occurred when I run the tcl application. I trace it into the openssl. There is a function implementation changed in the new version.



标签: c linux linker