使用 -static 时如何让 gcc/ld 遍历多个'-l library'?

How to make gcc/ld iterate over many '-l library' when using -static?

本文关键字:library 遍历 ld -static gcc 使用      更新时间:2023-10-16

我想静态编译pdf2svg,这样我就可以在稳定的Debian中使用最新版本。./configure没有提供--enable-static选项,所以我手动添加了链接器的Makefile -static选项。

不幸的是,结果并不像我所怀疑的那样。链接给了我大量的undefined reference错误。经过谷歌搜索,我发现问题是由-lsome_lib的错误顺序引起的。Gcc链接器第一次看到它时,会尝试在每个库中静态链接一次-info和Stackoverflow问题:为什么库的链接顺序有时会导致Gcc中的错误?。

是否有可能使链接器多次通过库列表?

也许这就是您要搜索的内容(从gnu-ld手册页):

   -( archives -)
   --start-group archives --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.

只要可能,勾号就可以添加对该类对象(或函数)的静态引用,该对象未链接到同一库的另一个cpp文件中(或已使用的另一库中)。

我有这样的情况:

  • clsA.cpp中具有类clsA的库A,该库给出错误
  • 带有foo.cpp的库A,不提供引用错误
  • 使用类clsA的库B
  • 应用程序同时使用库和foo.cpp中的类/函数

在使用库B中使用clsA类的对象时,我在Application中获得了未解析的引用。

将应用程序与库A和库B链接时出错。由于我使用CodeLite,所以很难更改库的顺序。我只是在foo.cpp中放了一个静态对象:

#include "clsA.h"
clsA objA;

链接器现在看到clsA在库A中被引用(在foo.cpp之间),并且将在应用程序中正确链接,因为foo.cpp已经被链接。

但是,即使对象是在一个伪函数中创建的,从未被调用,这个技巧也能奏效,因此对象永远不会被分配:

// foo.cpp
#include "clsA.h"
void dummyf()
{
   clsA objA;
}