我的项目由几个静态库组成,它们在最后一步链接在一起。现在我遇到了问题,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/