【C++】g++编译链接静态库的符号未定义问题和解决办法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wlhwaii/article/details/88026673

问题描述:
链接库:libsnlp_client.a 时提示大量未定义符号或引用:

/home/work/wlh01/searchsuggest/git-suggest/suggest/scf/snlp_client/snlpservice_TokenizerService_ClientInterface.cpp:22: undefined reference to `scf::ProxyStandard::ProxyStandard(std::string, char*, bool)'
/home/work/wlh01/searchsuggest/git-suggest/suggest/scf/snlp_client/snlpservice_TokenizerService_ClientInterface.cpp:26: undefined reference to `scf::Parameter::Parameter(int, char*, void*, bool, ParaType)'
/home/work/wlh01/searchsuggest/git-suggest/suggest/scf/snlp_client/snlpservice_TokenizerService_ClientInterface.cpp:29: undefined reference to `scf::Parameter::Parameter(int, char*, void*, bool, ParaType)'
/home/work/wlh01/searchsuggest/git-suggest/suggest/scf/snlp_client/snlpservice_TokenizerService_ClientInterface.cpp:32: undefined reference to `null_aic'
/home/work/wlh01/searchsuggest/git-suggest/suggest/scf/snlp_client/snlpservice_TokenizerService_ClientInterface.cpp:32: undefined reference to `scf::ProxyStandard::invoke(char*, scf::Parameter**, int, int*, AsynInvokeContext&)'
/home/work/wlh01/searchsuggest/git-suggest/suggest/scf/snlp_client/snlpservice_TokenizerService_ClientInterface.cpp:35: undefined reference to `scf_free'
...
../../lib/libsnlp_client.a(TokenizerService_Common_ClientInterface.o): In function `registerAll()':
/home/work/wlh01/searchsuggest/git-suggest/suggest/scf/snlp_client/./TokenizerService_Common_ScfEntity.h:9: undefined reference to `INFO_REGISTRED'
collect2: error: ld returned 1 exit status
make: *** [snlp_demo] 错误 1

问题分析:
make的链接选项为:

g++  -o ../bin/demo demo.o -Wl,-dn -lclient -lprotocol -lserialize -ljsonparser -lutils -lthreadpool -ltinyxpath -lsnlp_client -Wl,-dy -llog4cxx -lpthread -lz -lpcre -lboost_system

snlp_client.a依赖libprotocol.a和libclient.a,但是libsnlp_client.a在这两个库之后,先链接libprotocol.a和libclient.a时,snlp_client.a需要的符号不会被链接进来(链接静态库时的默认配置,以减小最终程序的大小)。等到链接snlp_client.a时,就会出现找不到snlp_client.a需要的符号问题了。

解决办法:

  1. 将 -lsnlp_client 的位置挪到 -lclient 之前。
展开阅读全文

没有更多推荐了,返回首页