views:

241

answers:

5

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?

+3  A: 

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.

Shay Erlichmen
+5  A: 

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

jpalecek
+1  A: 

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.

dmckee
+8  A: 

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.

alexk7
+1  A: 

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.

stsquad