Cha*_*rly 5 c++ gcc ld undefined-symbol nm
我的项目由几个静态库组成,这些库在最后一步中链接在一起.现在我遇到了问题,库的链接顺序很重要(否则我得到一个未定义的符号链接器错误).有时我遇到问题,我必须重新排序链接库(-lcommon -lsetup -lcontrol等).目前这是一个愚蠢的试验和错误:重新排序,编译,检查错误,重新排序,编译等等.
所以我写了一个小程序来向我展示库间依赖关系,并生成链接库的顺序.它从nm读取定义的('T','B'等)和未定义的符号('U')并从中删除弱符号('w','W','v'和'V') '未定义的符号列表'.现在它为每个未定义的符号确定解析它的库.
但我的程序向我展示了循环依赖...我的错误是什么?
如果它们真的存在,我根本无法联系......那么在分析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
Run Code Online (Sandbox Code Playgroud)
将库与循环依赖关系链接起来的另一个选择是使用特殊的链接器选项.男子ld:
-( 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.
Run Code Online (Sandbox Code Playgroud)
尽管如此,消除循环依赖关系总是更清晰.