且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多个文件编译错误时停止GCC

更新时间:2022-11-07 21:37:43

据我所知或可以确定,没有,GCC没有这样的选择。如果它确实有,我希望找到它记录为 Overall Option ,或者可能在请求或禁止警告的选项(其中 -Wfatal-errors 已记录)。两个部分都不会描述此类选项,整个选项摘要列出了任何看起来合适的选项,超出了问题中已讨论和拒绝的选项。



请注意, GCC 6.2.0的文档,是本文写作的最新版本。我认为假定如果问题寻求的选项存在于早期版本中,那么它将被结转,但是我没有执行GCC文档的详尽搜索,这是需要证明这一点是相当安全的。



对我来说最有希望的替代方法是简单地通过单独的 gcc 命令编译每个翻译单元,然后在单独的步骤中将它们链接在一起。 Makefile通常以这种方式工作,并且GNU Make将在没有构建任何目标后停止,前提是它的 -k / - keep -going 选项未启用。


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 stops at the first error of test1.c but it starts compiling test2.c.

I guess it is because 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"?

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.