gcc 构建链接,但共享库未与 ldd 一起出现 [英] gcc build links but shared library does not appear with ldd

查看:28
本文介绍了gcc 构建链接,但共享库未与 ldd 一起出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须构建的程序.程序依赖libAlibA依赖libB.两个库都在同一个文件夹中,但 ldd libA.so 不包括 libB.so 所以我必须在链接时添加它.

I've a program that I must build. The program depends on libA, and libA depends on libB. Both libs are in the same folder but ldd libA.so does not include libB.so so I must add it while linking it.

这是我的 gcc 命令:

gcc -L/path/to/libraries/lib -lA -lB -I/path/to/libraries/include main.cpp

gcc -L/path/to/libraries/lib -lA -lB -I/path/to/libraries/include main.cpp

程序构建并链接,但没有启动.它给了我以下错误:

The program builds and links, but it does not start. It gives me following error:

./a.out:符号查找错误:/path/to/libraries/lib/libA.so:未定义符号:symbol_used_in_libA_but_defined_in_libB

./a.out: symbol lookup error: /path/to/libraries/lib/libA.so: undefined symbol: symbol_used_in_libA_but_defined_in_libB

使用 ldd 我可以看到 libB.so 不包含在我的二进制文件中:

With ldd I can see that libB.so is not included in my binary:

linux-vdso.so.1 =>  (0x00007fffaecd9000)
libA.so => /path/to/libraries/lib/libA.so (0x00007effc02a4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007effbfebb000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007effbfca5000)
/lib64/ld-linux-x86-64.so.2 (0x00007effc05cb000)

我有这些条件:

  • /path/to/librariesLD_LIBRARY_PATH
  • 运行 ldconfig 没问题,ldconfig -p 找到 libA.solibB.so
  • 如果在 gcc 命令中我将 -lB 更改为 -lBB 它会给我一个链接器错误,所以我认为 gcc 找到正确 libB.so 即使它没有将其链接到可执行文件中.
  • /path/to/libraries is inside LD_LIBRARY_PATH
  • running ldconfig is ok and ldconfig -p find both libA.so and libB.so
  • If in gcc command I change -lB with -lBB it gives me a linker error, so I think that gcc find correctly libB.so even if it does not link it inside the executable.

我做错了什么?为了将可执行文件链接到这两个库,我可以做些什么?

What I'm doing wrong? What I can do in order to link the executable to both libraries?

推荐答案

大多数 Linux 发行版(我假设您使用的是基于 ldd 输出的 Linux)似乎配置了 gcc 默认情况下将 --as-needed 传递给 ld (例如,参见 这里 对于 Debian).这意味着最终的库/可执行文件将仅依赖于一个库(即,该库有一个 DT_NEEDED 标记),如果该库的某些符号实际被库/可执行文件使用.

Most Linux distributions (I assume you are using Linux based on the output of ldd) seem to configure gcc as to pass --as-needed to ld by default (e.g., see here for Debian). This means the final library/executable will only depend on a library (i.e., have a DT_NEEDED tag for that library) if some symbol of that library is actually used by the library/executable.

在您的情况下, main.cpp 不使用 libB 的任何功能,因此链接器不会添加 libB 作为最终可执行文件.您可以通过将 --no-as-needed 标志传递给链接器来解决它.例如,

In your case, main.cpp does not use any functions of libB so the linker does not add libB as a dependency of the final executable. You can work around it by passing the --no-as-needed flag to the linker. E.g.,

gcc -Wl,--no-as-needed ...

gcc -Wl,--no-as-needed ...

当然,正确的解决方法是重新链接 libA 并确保将 libB 列为依赖项.

Of course, the proper fix is to relink libA and make sure it lists libB as a dependency.

这篇关于gcc 构建链接,但共享库未与 ldd 一起出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆