如何使 gcc/ld 迭代多个“-l 库”?当使用-static时?

发布于 2024-12-27 20:02:03 字数 574 浏览 5 评论 0原文

我想静态编译 pdf2svg 以便我能够在稳定的 Debian 中使用最新版本。 ./configure 没有提供 --enable-static 选项,因此我在 Makefile 中手动添加 -static链接器的选项。

不幸的是结果并不像我想象的那样。该链接给我带来了大量的未定义的引用错误。经过一番谷歌搜索后,我发现问题是由 -lsome_lib 的错误顺序引起的。当 Gcc 链接器第一次看到每个库时,它会尝试在每个库中静态链接一次 - 信息和Stackoverflow问题:为什么顺序链接库有时会导致 GCC 中出现错误?

是否有可能使链接器多次遍历库列表?

I want to compile statically pdf2svg so I will be able to use newest version in stable Debian. The ./configure doesn't give --enable-static option so I added manually in Makefile -static option for linker.

Unfortunately the result wasn't quite as I suspected. The linking gave me enormous amounts of undefined reference errors. After some googling I figured out that the problem is caused by wrong order of -lsome_lib. Gcc linker tries to statically link in each library once, when it first sees it - info and Stackoverflow question: Why does the order in which libraries are linked sometimes cause errors in GCC?.

Is there a possibility of making linker make multiple passes through the list of libraries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不语却知心 2025-01-03 20:02:03

也许这就是您搜索的内容(来自 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.

Maybe this is what you search for (from gnu ld manpage):

   -( 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.
寄人书 2025-01-03 20:02:03

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

我遇到这种情况:

  • clsA.cpp 中带有类 clsA 的库 A 给出了错误
  • 带有 foo.cpp 的库 A 没有给出引用错误
  • 使用类 clsA 的库 B
  • 应用程序使用这两个库并使用 foo.cpp 中的类/函数

我得到使用库 B 中使用 clsA 类的对象时,应用程序中未解析的引用。

将应用程序与库 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;
}

A tick is, whenever possible, to add a static reference to an object of the class (or to the function) that were not linked in another cpp file of the same library (or in another library already used).

I have this situation:

  • library A with class clsA in clsA.cpp that gives the error
  • library A with foo.cpp that gives no reference errors
  • library B that uses class clsA
  • Application uses both libraries and uses classes/functions from foo.cpp

I get the unresolved reference in Application while using the object in library B that uses the clsA class.

Linking Application with library A and B give me the error. Since i use CodeLite, it's hard to change library order. I simply put a static object in foo.cpp:

#include "clsA.h"
clsA objA;

The linker now see that clsA are referenced in library A (between foo.cpp) and will link correctly in application because foo.cpp were already linked.

But the trick works even if the object were created in a dummy function, never called, so the object would never been allocated:

// foo.cpp
#include "clsA.h"
void dummyf()
{
   clsA objA;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文