c++ - 如何确定库间依赖关系?

标签 c++ gcc ld undefined-symbol nm

我的项目由几个静态库组成,它们在最后一步链接在一起。现在我遇到了问题,link order of the library is important (否则我会收到 undefined symbol 链接器错误)。有时我会遇到问题,我必须重新排序链接库(-lcommon -lsetup -lcontrol 等)。目前这是一个愚蠢的试错:重新排序,编译,检查错误,重新排序,编译等等。

因此,我编写了一个小程序来向我展示库间依赖关系,并为我生成要链接的库的顺序。它从 nm 中读取已定义的('T'、'B' 等)和 undefined symbol ('U')并删除 weak symbols ('w'、'W'、'v' 和 'V')来自“ undefined symbol 列表”。现在它为每个 undefined symbol 确定解析它的库。

但我的程序向我显示循环依赖...我的错误是什么?

如果它们真的存在,我根本无法链接...那么在分析 nm 输出时我错过了什么?还是分析 nm 输出不是获取这些依赖项的方法?

libcommon.a:
         U _ZN15HardwareUnit23GetHardwareSerialNumberEv
libhardware.a:
00000484 T _ZN15HardwareUnit23GetHardwareSerialNumberEv
libsecurityaccess.a:
         U _ZN15HardwareUnit23GetHardwareSerialNumberEv
---
libhardware.a:
         U _ZN21ApplicationProfile26GetApplicationSettingsPathERK7QString
libsecurityaccess.a:
00004020 T _ZN21ApplicationProfile26GetApplicationSettingsPathERK7QString
         U _ZN21ApplicationProfile26GetApplicationSettingsPathERK7QString

最佳答案

链接具有循环依赖性的库的另一个选项是为此使用一个特殊的链接器选项。男人:

   -( archives -)
   --start-group archives --end-group
       The archives should be a list of archive files.  They may be either
       explicit file names, or -l options.

       The specified archives are searched repeatedly until no new
       undefined references are created.  Normally, an archive is searched
       only once in the order that it is specified on the command line.
       If a symbol in that archive is needed to resolve an undefined
       symbol referred to by an object in an archive that appears later on
       the command line, the linker would not be able to resolve that
       reference.  By grouping the archives, they all be searched
       repeatedly until all possible references are resolved.

       Using this option has a significant performance cost.  It is best
       to use it only when there are unavoidable circular references
       between two or more archives.

不过,消除循环依赖总是更干净。

关于c++ - 如何确定库间依赖关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4779759/

相关文章:

c++ - undefined reference to vtable 错误是由现代 g++ 编译器解决的吗?

c - #define 与 static const 的优化(在 avr-gcc 中)

linker - 究竟什么是目标文件中的符号引用?

c++ - 共享库链接到具有公共(public)接口(interface)但不同实现的静态库

c++ - 在 C++ 中不替换地采样 3 个整数

c++ - 在 Visual C++ 6.0 中跟踪变量变化

c++ - 哪个 C++ STL 容器提供 `extract_max()` 、 `find(element_value)` 和 `modify(element)` 功能?

c++ - Makefile : "Unable to open output file" …"No such file or directory" 编译错误

function - 为什么 GCC 源代码中特定于目标的函数声明使用 K&R 样式而不是原型(prototype)?

ruby-on-rails - 无法安装 autotest-fsevent 或 rs-fsevent;收到 ld 错误但已安装 xcode