I have encountered a strange case of undefined symbols in a share library that I cannot resolve. Let's say that my program needs to use a library A
, which in turn depends on another library B
, neither of which I can modify.
I first build library B
using their own automake system, which creates both static libB.a
and libB.so
libraries. The static library libB.a
contains some duplicated mangled symbols, originating from different object files, some of which are undefined. This seems normal to me. The shared library libB.so
contains only one version of each of these symbols, none of which are undefined.
Then I build library A
using, again, their own build system (not automake this time but some poorly written makefile). Schematically, what the build system of A is doing is just
g++ -shared -fPIC -DPIC -std=c++17 -fPIC -fopenmp -I<path/to/includes> -Wl,-rpath,<path/to/libs> -L<path/to/libs> -lB A.cc -o libA.so
which I assume just builds libA.so
using the shared library libB.so
. Ultimately my goal is to link my main program to libA.so
.
Here is where my problems start. My main program fails to link to libA.so
claiming some undefined symbols. Looking into libA.so
with nm
shows me that it has indeed some undefined symbols, the same ones that were undefined in the static libB.a
, though this time only one copy of each undefined symbol is shown. Furthermore, when I run ldd
on libA.so
it does not show linkage to libB.so
, which makes me think it's using the static version of the library. However, as a test, I deleted the static libary libB.a
after building B
but before building A
, while leaving the shared library libB.so
untouched, but that did not change anything.
I also tried to create a static version of A
and link my main program to that, but it suffers from the same problem where now the undefined symbol linking error turns into an undefined reference to X error.
So my questions are: why doesn't libB.so
shows when running ldd
on libA.so
? Why only the undefined verson of the symbols ends up in libA.so
, even though I'm (supposedly) linking to libB.so
, which does not have undefined symbols? What can I do to resolve the issue, knowing that I cannot change the make systems of A
or B
, except providing some flags? Thanks in advance for the help.
Edit: I just found out that if I create a static version of A
and link my main program to it before linking to B
it succeeds. I'm not sure why though.
So my questions are: why doesn't libB.so shows when running ldd on libA.so?
See this answer.
What can I do to resolve the issue, knowing that I cannot change the make systems of A or B, except providing some flags?
Changing the libA.so
link command to be ... A.cc -o libA.so -lB
should fix this.