0

Suppose I have a make file with 3 files, A,B,C. B and C are dependent only on A. One possible order of resolving the dependencies is to compile in the order ABC. The other way is ACB. My questions is, do compilers in general optimize the order in which it compiles the files subject to some objective function or would it just randomly chose an ordering?

AspiringMat
  • 2,161
  • 2
  • 21
  • 33
  • What do you mean by "B and C are dependent only on A"? B and C may include A, but this typically means that A is a header file and therefore should not be compiled as translation unit. – user7860670 Jul 13 '17 at 05:51
  • Files B and C cannot compile until A has compiled. – AspiringMat Jul 13 '17 at 05:52
  • And why so? Do you mean that A is a library and B and C are executables or what? I guess you should provide [mcve](https://stackoverflow.com/help/mcve). – user7860670 Jul 13 '17 at 05:54
  • 1
    When compiling either of A, B or C, the compiler does not know anything about other files. It works with a single file only. What you are asking is build system's domain, `make` in your case. Probably, your question is answered [here](https://stackoverflow.com/questions/9159960/order-of-processing-components-in-makefile). Also consider using a make system generator such as `cmake` or something like that. It provides a convenient, platform-agnostic set of abstractions to build your project. – Sergey Jul 13 '17 at 05:55
  • 1
    Compilers don't decide the order in which the compiler is invoked... `make` decides it. – M.M Jul 13 '17 at 05:56
  • 1
    It doesn't matter what A, B and C are. It is the `make` program that determines the order of dependency building. It may even do some in parallel. – juanchopanza Jul 13 '17 at 05:56
  • gmake with `-j2` switch will build B and C in parallel and then build A. A common mistake in writing makefiles is to assume that `A: B C` means `B` will be built before `C`. – M.M Jul 13 '17 at 05:57
  • I see. I was a bit confused. And yes, the linked question answers my question. – AspiringMat Jul 13 '17 at 05:59

0 Answers0