0%

静态库链接选项

本文介绍两对 ld 选项

  • –whole-archive
  • –no-whole-archive
  • –start-group
  • –end-group

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

I

  • –whole-archive
  • –no-whole-archive

参数 --whole-archive 来告诉链接器,将后面库中所有符号都链接进来,参数 -no-whole-archive 则是重置,以避免后面库的所有符号被链接进来。

g++ -Wl,--whole-archive -L. -la -Wl,--no-whole-archive main.cpp -o main

将 liba.a 中的所有.o 中的符号都链接进来

II

  • –start-group
  • –end-group

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

ld 可能会遇到 循环链接,这样就需要使用 --start-group--end-group 反复在 group 中进行搜索直到所有的未定义字符都被找到为止,而不是默认的只搜索一次。