c++插件机制下链接静态库未使用函数失败

 Linux C/C++ 链接选项之静态库--whole-archive,--no-whole-archive和--start-group, --end-group

这四个都是链接器的选项,所以在编译的时候要用-Wl,[options]来传递给链接器,不然编译器会不认得这个选项。

--whole-archive选项解决的是编译中常遇到的问题。在代码中定义的符号(如函数名)还未使用到之前,链接器并不会把它加入到连接表中。如下面这个例子:

a.cpp:

void func(){printf("I am in a.cpp.\n");}

main.cpp:

extern void func();

int main(){func(); printf("I am in main.cpp"); return 0;}

首先编译g++ -c a.cpp,再打包ar -r liba.a a.o。

如果这么链接g++ -L. -la main.cpp -o main,则链接器会报错,称func()未定义。其实改一下顺序既可以解决g++  main.cpp -L. -la -o main。

或者使用g++ -Wl,--whole-archive -L. -la -Wl,--no-whole-archive main.cpp -o main,将liba.a中的所有.o中的符号都链接进来。

对于C++中使用的Register机制,即动态对象创建会遇到这样的问题。在使用Caffe库的时候也遇到了这样的问题。

对于--start-group和--end-group, gcc liner man中如下描述:

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

这里有一个链接顺序的问题,在command line上前面的库会依赖之后的库,如果碰到循环链接,-lliba -llibb -lliba,这样就需要使用--start-group和--end-group反复在.a中进行搜索直到所有的未定义字符都被找到为止,而不是默认的只搜索一次。MKL库有这样的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值