1

Compilation of Z.cpp fails. I modify a private member of class Z in file Z.h. I rerun make. Whereupon Make compiles all files that depend on Z.h: A.cpp, B.cpp, ..., no human would be so stupid and pedantic, ..., X.cpp, Y.cpp, before it finally arrives at Z.cpp, and fails again because of a trivial typo in my edit of Z.h ...

Is there a way to make Make adaptive? To let it start compilation with targets that did not compile lately?

[PS] Let me clarify: Of course I am aware that any modification of Z.h may break any of A.cpp, ..., Y.cpp. Of course those sources need to be recompiled too. This question is about a heuristic improvement of the compilation order.

I do not expect Make to learn anything about C++. It would be fully sufficient for me, and immensely helpful for many, if Make could be taught to reason as follows: On my last attempt to compile A, .., Z, everything passed except Z. Even if my user worked hard to fix Z, Z is still more likely to be broken than any other single source file. Therefore I will now compile the sources in the order Z, A, ..., Y. Thereby, on average, my user will have to wait less for me to terminate with error.

Joachim W
  • 7,290
  • 5
  • 31
  • 59
  • 4
    What if your header "fix" fixed Z but broke everything else? Make can't guess what you're doing. If you want to build Z.o, ask make to do that. – Mat Apr 04 '18 at 07:36
  • Probably some scripting around your `make` call can help. See e.g. [here](https://stackoverflow.com/questions/31348508/save-and-reprint-warnings-for-successfully-compiled-files-on-subsequent-builds) – Florian Apr 04 '18 at 07:47
  • If you ask yourself why private properties of classes are physically intermingled with public ones, you arrive at the right conclusions for your problem. And they will not be about make. – Vroomfondel Apr 04 '18 at 08:54
  • @Vroomfondel: abandon C++? Use piml idiom throughout? What other solution are you thinking of? Any link to "the right solution" would be appreciated. – Joachim W Apr 04 '18 at 09:54
  • `make Z.o` or the moral equivalent for your project? – Stephen Newell Apr 04 '18 at 10:14
  • I said "conclusion", not solution. My conclusion would be that a interface (class) would remove that particular problem (maybe at the cost of new problems). YMMV. – Vroomfondel Apr 04 '18 at 10:46
  • What your last paragraph suggests is IMHO a wonderful task for an add-on or first-stage of a particular build chain which is purposed for a range of specific projects. It is not(also IMHO) a good task for make itself - make is a Unix core tool and should not introduce nondeterministic behavior. You wouldn't want grep to somehow guess from the last invocation what it should search now, would you? – Vroomfondel Apr 05 '18 at 19:11

1 Answers1

0

Make doesn't really understand C++, or even C. It understands file dependencies as a directed graph, in abstract terms. A.o depends on Z.h, and so does Z.o.

Also, the fact that you changed a private member (even if make would magically know) does not mean that A.o is unaffected. A lot of C++ code is inlined, and private members can be accessed by inlined code. private checks if access is from within the same class Z, not within the same file Z.o.

Usually the better fix is make -j 8. Why build only a single file at a time?

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    I think OP doesn't mean to **not** recompile `A.o`, he just wants `make` to **start** by recompiling the missing `Z.o` and *then* proceed with A.o. Although this sounds like an XY issue, as running `make` and watching output isn't the most effective way to develop an application. – rustyx Apr 04 '18 at 08:29
  • 1
    @rustyx: Agree with that last sentiment; that's why you have IDE's. – MSalters Apr 04 '18 at 09:03
  • Could you please elaborate this? The IDEs I know are running `make`. Where is the difference from running `make` from the command line? – Joachim W Apr 04 '18 at 10:14
  • @JoachimW: It depends a bit on whether the IDE generated the `makefile`. If so, it knows exactly what `make` would do. But "trivial typo's" such as getting the parentheses wrong will be caught by a decent IDE immediately without going to `make`. – MSalters Apr 04 '18 at 10:19