Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

1) When linking these files to produce an executable, does the order matter?

This question has an answer that suggests the order does not matter.

This site explicitly agrees, giving an example using GCC.

2) If the order does matter, how does an IDE like Visual Studio determine the appropriate link order?

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. However, I may be misremembering linking library files.

like image 278
Scott Oliver Avatar asked Oct 15 '25 07:10

Scott Oliver


1 Answers

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.

like image 178
MSalters Avatar answered Oct 17 '25 20:10

MSalters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!