【发布时间】:2018-02-28 10:52:08
【问题描述】:
我正在尝试将一个小型 C++ 测试程序 main.o 链接到第 3 方静态库,但得到一些莫名其妙的 undefined reference 错误。
具体来说:
g++ -o secoTest -lpthread main.o libEAPI.a
main.o: In function `main':
main.cpp:(.text+0x29fe): undefined reference to `EApiWDog_SetConfigAndStart(unsigned int, unsigned int, unsigned int, unsigned int, unsigned char, unsigned char, unsigned char)'
main.cpp:(.text+0x33fc): undefined reference to `EApiSetCarrier(unsigned char)'
main.o:main.cpp:(.text+0x3956): more undefined references to `EApiSetCarrier(unsigned char)' follow
main.o: In function `main':
main.cpp:(.text+0x3965): undefined reference to `EApiGPIOGetSerialCfg(unsigned char*)'
main.cpp:(.text+0x3a0a): undefined reference to `EApiSetCarrier(unsigned char)'
main.cpp:(.text+0x3a19): undefined reference to `EApiGPIOSetSerialCfg(unsigned char)'
main.cpp:(.text+0x3adf): undefined reference to `EApiFanEnable(unsigned int, unsigned char)'
main.cpp:(.text+0x3b83): undefined reference to `EApiFanDisable()'
collect2: ld returned 1 exit status
但是,符号似乎出现在库中。例如:
nm --demangle libEAPI.a | grep EApiFanDisable
00003c20 T EApiFanDisable
奇怪的是符号不完全相同。在main.o 中是
nm --demangle main.o | grep EApiFanDisable
U EApiFanDisable()
所以一个有() 而一个没有。
同样,
nm --demangle main.o | grep EApiSetCarrier
U EApiSetCarrier(unsigned char)
nm --demangle libEAPI.a | grep EApiSetCarrier
000015d0 T EApiSetCarrier
如果我完全从命令行中省略了库(例如,g++ -o secoTest -lpthread main.o),它会按预期显示很多错误。
main.o 引用带有和带有() 的外部符号[为什么?]:
U EApiVgaSetBacklightEnable
U EApiWDogStart
U EApiWDogTrigger
U EApiFanEnable(unsigned int, unsigned char)
U EApiFanDisable()
U EApiSetCarrier(unsigned char)
但图书馆只有符号没有() [为什么?]:
000020e0 T EApiVgaSetBacklightEnable
000024e0 T EApiWDogStart
000026f0 T EApiWDogTrigger
00003c20 T EApiFanDisable
00003bf0 T EApiFanEnable
000015d0 T EApiSetCarrier
这会是未定义引用的原因吗?我将如何解决它?不知道下一步该往哪里看...
(我无法修改第三方库,但有头文件。)
编辑
正如 lisyarus 所建议的,这里是 nm,没有 --demangle。确实,符号不同。 g++ 编译器 (v4.4.7) 为某些符号生成一个损坏的符号 而库总是有普通符号... [为什么?]
nm libEAPI.a main.o | grep EApiWDogTrigger
000026f0 T EApiWDogTrigger
U EApiWDogTrigger
nm libEAPI.a main.o | grep EApiSetCarrier
000015d0 T EApiSetCarrier
U _Z14EApiSetCarrierh
【问题讨论】:
-
nm没有拆解显示什么?可能会发生该库是使用不同的编译器/标准/等编译的,并且损坏的名称有所不同。
标签: c++ static-libraries