How is the order of compilation of source files decided?

ぃ、小莉子 提交于 2019-12-22 11:31:21

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/705866/how-is-the-order-of-compilation-of-source-files-decided

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!