首页 > 解决方案 > 奇怪的错误 - 链接到自己的库 gcc 时未定义的引用

问题描述

我在创建和链接库方面几乎没有经验,但这次我为这个愚蠢的事情发疯了。

-bash-3.2$ g++ -m32 src/main.cpp -Iinc -lblpapi3_32 -I/tmp/diff/include -L/tmp/diff/lib -lbbbmds
/tmp/ccQCKwnF.o: In function `main':
main.cpp:(.text+0x14e): undefined reference to `MDS::BloombergSource::BloombergSource()'
main.cpp:(.text+0x4ed): undefined reference to `MDS::BloombergSource::startSession()'
main.cpp:(.text+0x555): undefined reference to `MDS::BloombergSource::openService()'
main.cpp:(.text+0x560): undefined reference to `MDS::BloombergSource::authorize()'
main.cpp:(.text+0x5fc): undefined reference to `MDS::BloombergSource::AddSubscription(char const*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, BloombergLP::blpapi::CorrelationId const&)'
main.cpp:(.text+0x655): undefined reference to `MDS::BloombergSource::subscribe()'
collect2: ld returned 1 exit status

确认我的库引用了这些函数,例如下面

-bash-3.2$ nm /tmp/diff/lib/libbbbmds.so |grep startSession
0000000000002a62 t _ZN3MDS15BloombergSource12startSessionEv

进一步确认,

-bash-3.2$ c++filt _ZN3MDS15BloombergSource12startSessionEv
MDS::BloombergSource::startSession()

还,

-bash-3.2$ file /tmp/diff/lib/libbbbmds.so
/tmp/diff/lib/libbbbmds.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped

已尝试重新排序 libs/src。

标签: gccg++linker-errors

解决方案


nm输出列表MDS::BloombergSource::startSession()函数作为t- 具有内部链接的函数。其他翻译单元不提供此类功能。


推荐阅读