简体   繁体   中英

How is the order of compilation of source files decided?

I have a workspace containing many *.c files, which I compile. (I could use any toolchain say MSVC6.0, or gcc etc)

Which source file is compiled first?

How is the order of the files to be compiled subsequently decided?

The order of compilation is not specified by the C standard.

Since there is no need for the construction of global objects like in C++, there is no situation where the order of compilation is relevant in C.

VC : By project folder, then alphabetically.
GCC : according to the make file order

Why is this important?, the completion order don't meter and doesn't effect the final build result.

Generally, this is not specified anywhere. Especially when using eg. parallel make, the order of compilation is pretty much arbitrary.

With make :

  • The targets are addressed in the order they appear
  • A dependency tree is built for each target and I would guess that the tree is traversed depth-first with post-order evaluation (seems to be the only way it will work, but I can't find anything that specifies this in the documentation)

As jpalecek suggests , concurrent builds may be more complicated.


Some quotes from the GNU make docs:

The double-colon rules for a target are executed in the order they appear in the makefile.

...

If you specify several goals, make processes each of them in turn, in the order you name them.

If it matters then you really need to set dependencies in your makefile to ensure some are built before others. Really you should first ask yourself why it matters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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