g++ 5.4.0
Ubuntu 16.04
i have several shared libraries and here is the abstraction of them:
libtest0.so: definition of `void foo()`
libtest1.so: `extern void foo()`, and not depends on libtest0.so
libtest2.so: use libtest1.so and depends on libtest1.so
and now i have a binary:
a.out: use libtest2.so
of course a.out
has undefined symbol of foo()
. To resolve it, i tried:
g++ main.cc -L. -ltest2 -ltest0
since libtest0.so has definition of foo()
.
However, it does not work:
$g++ -g main.cc -L. -ltest2 -ltest0
libtest1.so: undefined reference to `foo'
collect2: error: ld returned 1 exit status
My questions are:
- why does this "undefined reference" happen?
- any solution to tell the linker that libtest0.so has definition of
foo()
?
thanks.
Update1:
if i link libtest0 when compile libtest2, then compile:
g++ -g main.cc -L. -ltest2
everything is fine now. However, in the real world problem, i cannot change the linkage of libtest2.so
Update2
clang++ works with this cmd: (and the linker is ld, not lld)
# clang version 11.0.0
clang++ -g main.cc -L. -ltest2 -ltest0
Update3
an example is: https://gist.github.com/xgwang/da93edcc06542264157f600eb64bc082
just bash it on linux. Tested on Ubuntu 16.