Linux C/C++ 链接选项之静态库--whole-archive,--no-whole-archive和--start-group, --end-group
参照这两篇博客:
http://stackoverflow.com/questions/805555/ld-linker-question-the-whole-archive-option
http://codemacro.com/2014/09/15/inside-static-library/
这四个都是链接器的选项,所以在编译的时候要用-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库有这样的问题。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架