简体   繁体   English

C ++:obj文件链接的顺序重要吗?

[英]C++: Does the order that obj files are linked matter?

Taking the classic example of a header file and implementation file that declare and define a simple function, and a second implementation file that contains a main() that calls the function, a compiler will generate two object files. 以声明和定义简单函数的头文件和实现文件以及包含调用该函数的main()的第二个实现文件的经典示例为例,编译器将生成两个目标文件。

1) When linking these files to produce an executable, does the order matter? 1)链接这些文件以生成可执行文件时,顺序重要吗?

This question has an answer that suggests the order does not matter. 这个问题的答案表明顺序无关紧要。

This site explicitly agrees, giving an example using GCC. 该站点明确同意,并给出了使用GCC的示例。

2) If the order does matter, how does an IDE like Visual Studio determine the appropriate link order? 2)如果顺序很重要,那么类似Visual Studio的IDE如何确定适当的链接顺序?

I have distinct memories of encountering unresolved symbol errors when building with gcc/g++ and needing to alter the order of object files in the makefile to fix this. 我用gcc / g ++编译时遇到无法解决的符号错误,并且需要更改makefile中目标文件的顺序以解决此问题,这让我记忆犹新。 However, I may be misremembering linking library files. 但是,我可能记错了链接文件。

The order in which object files are linked does not matter. 对象文件的链接顺序无关紧要。 Library order indeed does matter, and that is the responsibility of the developer. 图书馆的顺序确实很重要,这是开发人员的责任。

To be honest, linkers are pretty antique. 老实说,链接器非常古老。 Modern languages don't have linkers, but GCC in particular goes to great lengths to stay compatible with the past. 现代语言没有链接器,但是特别是GCC竭尽全力与过去保持兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM