gcc共享库中有关嵌套依赖项的“未定义引用”

问题描述 投票:0回答:1

g ++ 5.4.0

Ubuntu 16.04

我有几个共享库,这是它们的抽象:

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

现在我有一个二进制文件:

a.out: use libtest2.so

当然a.out具有foo()的未定义符号。为了解决这个问题,我尝试过:

g++ main.cc -L. -ltest2 -ltest0

因为libtest0.so具有foo()的定义。

但是,它不起作用:

$g++ -g main.cc -L. -ltest2 -ltest0
libtest1.so: undefined reference to `foo'
collect2: error: ld returned 1 exit status

我的问题是:

  1. 为什么会发生这种“未定义的引用”?
  2. 告诉链接器libtest0.so具有foo()定义的任何解决方案?

谢谢。

Update1:​​

如果我在编译libtest2时链接libtest0,则进行编译:

g++ -g main.cc -L. -ltest2

现在一切都很好。但是,在现实世界中,我无法更改libtest2.so的链接。

Update2

clang ++与此cmd一起使用:(并且链接器是ld,不是lld)

# clang version 11.0.0
clang++ -g main.cc -L. -ltest2 -ltest0

Update3

一个例子是:https://gist.github.com/xgwang/da93edcc06542264157f600eb64bc082

只需在Linux上对其进行重击。在Ubuntu 16上测试。

g ++ 5.4.0 Ubuntu 16.04,我有几个共享库,这是它们的抽象:libtest0.so:`void foo()`的定义libtest1.so:`extern void foo()`,而不依赖于libtest0 .so ...

c++ linker shared-libraries
1个回答
0
投票

首先,引用here(强调是我的话:]

© www.soinside.com 2019 - 2024. All rights reserved.