如何使 gcc/ld 迭代多个“-l 库”?当使用-static时?
我想静态编译 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这就是您搜索的内容(来自 gnu ld 手册页):
Maybe this is what you search for (from gnu ld manpage):
只要有可能,勾选就是添加对未在同一库的另一个 cpp 文件(或已使用的另一个库)中链接的类(或函数)对象的静态引用。
我遇到这种情况:
我得到使用库 B 中使用 clsA 类的对象时,应用程序中未解析的引用。
将应用程序与库 A 和 B 链接给我错误。由于我使用 CodeLite,因此很难更改库顺序。我只是将一个静态对象放入 foo.cpp 中:
链接器现在看到 clsA 在库 A 中(在 foo.cpp 之间)被引用,并将在应用程序中正确链接,因为 foo.cpp 已经链接。
但即使该对象是在虚拟函数中创建的,从未被调用,该技巧也有效,因此该对象永远不会被分配:
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:
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:
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: