使用 -static 时如何让 gcc/ld 遍历多个'-l library'?
How to make gcc/ld iterate over many '-l library' when using -static?
我想静态编译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;
}
相关文章:
- 有什么方法可以遍历结构吗
- 在循环中按顺序遍历成员变量
- 遍历模板参数
- 在遍历处理程序的向量时注册和注销处理程序
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 遍历并行数组以确定C++中的最大数字
- 遍历顺序由 std::文件系统directory_iterator给出
- 遍历链表时的无限循环
- 遍历unordered_map向量
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 如何正确地推回然后遍历堆中对象的向量?
- 遍历二维数组的所有子数组
- 如何在可变参数模板函数中遍历可变参数元组?
- 避免在遍历 IShellItemArray 时出现代码重复
- 从特定键开始遍历地图
- 关于链表遍历和调试的困惑
- 将树节点添加到向量向量中的 n 元树遍历的平均和最坏情况时间复杂度是多少?
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 使用 -static 时如何让 gcc/ld 遍历多个'-l library'?