将 c 代码链接到 c ++ 代码时的 g ++ 链接顺序依赖性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3363398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
g++ linking order dependency when linking c code to c++ code
提问by Gearoid Murphy
Prior to today I had always believed that the order that objects and libraries were passed to g++ during the linking stage was unimportant. Then, today, I tried to link from c++ code to c code. I wrapped all the C headers in an extern "C" block but the linker still had difficulties finding symbols which I knew were in the C object archives.
在今天之前,我一直认为对象和库在链接阶段传递给 g++ 的顺序并不重要。然后,今天,我尝试从 c++ 代码链接到 c 代码。我将所有 C 头文件都包装在一个 extern "C" 块中,但链接器仍然难以找到我知道在 C 对象档案中的符号。
Perplexed, I created a relatively simple example to isolate the linking error but much to my surprise, the simpler example linked without any problems.
困惑,我创建了一个相对简单的示例来隔离链接错误,但令我惊讶的是,更简单的示例链接没有任何问题。
After a little trial and error, I found that by emulating the linking pattern used in the simple example, I could get the main code to link OK. The pattern was object code first, object archives second eg:
经过一些尝试和错误,我发现通过模拟简单示例中使用的链接模式,我可以让主代码链接OK。该模式首先是对象代码,其次是对象档案,例如:
g++ -o serverCpp serverCpp.o algoC.o libcrypto.a
g++ -o serverCpp serverCpp.o algoC.o libcrypto.a
Can anyone shed some light on why this might be so?. I've never seen this problem when linking ordinary c++ code.
任何人都可以解释为什么会这样吗?我在链接普通 C++ 代码时从未见过这个问题。
回答by
The order you specify object files and libraries is VERY important in GCC - if you haven't been bitten by this before you have lead a charmed life. The linker searches symbols in the order that they appear, so if you have a source file that contains a call to a library function, you need to put it before the library, or the linker won't know that it has to resolve it. Complex use of libraries can mean that you have to specify the library more than once, which is a royal pain to get right.
您指定目标文件和库的顺序在 GCC 中非常重要 - 如果您在过上迷人的生活之前没有被它咬过的话。链接器按符号出现的顺序搜索符号,因此如果您的源文件包含对库函数的调用,则需要将其放在库之前,否则链接器将不知道它必须解析它。库的复杂使用可能意味着您必须不止一次地指定库,这是一项非常痛苦的事情。
回答by Mark B
The library order pass to gcc/g++ does actually matter. If A
depends on B
, A must be listed first. The reason is that it optimizes out symbols that aren't referenced, so if it sees library B
first, and no one has referenced it at that point then it won't link in anything from it at all.
传递给 gcc/g++ 的库顺序实际上很重要。如果A
取决于B
,则必须首先列出 A。原因是它优化了未被引用的符号,所以如果它B
首先看到库,并且那时没有人引用它,那么它根本不会链接任何来自它的东西。
回答by Mike Seymour
A static library is a collection of object files grouped into an archive. When linking against it, the linker only chooses the objects it needs to resolve any currently undefined symbols. Since the objects are linked in order given on the command line, objects from the library will only be included if the library comes after all the objects that depend on it.
静态库是分组到档案中的目标文件的集合。链接时,链接器只选择解析任何当前未定义符号所需的对象。由于对象是按照命令行上给出的顺序链接的,因此只有当库出现在所有依赖于它的对象之后时,才会包含库中的对象。
So the link order is very important; if you're going to use static libraries, then you need to be careful to keep track of dependencies, and don't introduce cyclic dependencies between libraries.
所以链接顺序很重要;如果你打算使用静态库,那么你需要小心跟踪依赖关系,不要在库之间引入循环依赖。
回答by Hani
You can use --start-group archives --end-groupand write the 2 dependent libraries instead of archives
您可以使用--start-group archives --end-group并编写 2 个依赖库而不是存档
gcc main.o -L. -Wl,--start-group -lobj_A -lobj_b -Wl,--end-group
gcc main.o -L. -Wl,--start-group -lobj_A -lobj_b -Wl,--end-group