Stop GCC on error with multiple files compilation

2019-09-19 03:47发布

问题:

Trying answering to this SO question I found out that gcc options

  • -Wfatal-errors
  • -fmax-errors=n

works "per compilation unit"

I mean, using 2 source code c file and compiling with command

gcc -Wall -Wextra -pedantic -Wfatal-errors -o test test1.c test2.c

and writing garbage into both files I see that gcc stops at the first error of test1.c but it starts compiling test2.c.

I guess it is because gcc process launches different tasks, one for each source file, so options are applied multiple times.

The question is: is there a compiler option that works "per gcc command"?

回答1:

As far as I know or can determine, no, GCC has no such option. If it did have, I would expect to find it documented either as an "Overall Option", or possibly among the "Options to Request or Suppress Warnings" (where -Wfatal-errors is documented). Neither section describes such an option, nor does the overall option summary list anything that seems appropriate, beyond those options already discussed and rejected in the question.

Note that I've referred to and linked the docs for GCC 6.2.0, the most recent version as of this writing. I think it is reasonably safe to assume that if an option such as the question seeks were present in an earlier version then it would have been carried forward, but I have not performed the exhaustive search of GCC documentation that would be required to prove that.

The alternative that seems most promising to me is simply to compile each translation unit via a separate gcc command, and then link them together in a separate step. Makefiles are often structured to work in this way, and GNU Make will stop after failing to build any target, provided that its -k / --keep-going option is not enabled.