1

Elsewhere (here on SO, for example) and even my own memory tells me that #error will cause compilation to terminate, but if I compile this:

#ifndef FOO
#  error "FOO not defined."
#endif

int main() {
  return "exit now!!";
}

I get:

g++ -Wall -o /dev/null main.cpp
main.cpp:2:4: error: #error "FOO not defined."
 #  error "FOO not defined."
    ^~~~~
main.cpp: In function 'int main()':
main.cpp:8:10: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
   return "exit!!";
          ^~~~~~~~

This is for g++ versions 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4 20160609) and 7.0.0 (built myself 20161128). (There is a similar question asked here about MS Visual Studio 2015.)

According to the GNU CPP manual:

The directive ‘#error’ causes the preprocessor to report a fatal error. The tokens forming the rest of the line following ‘#error’ are used as the error message.

So, perhaps I just don't know what a "fatal error" is. The gcc option -Wfatal-errors would imply that a fatal error is one that would cause compilation to terminate, but nothing in the manuals ever defines it (unless I missed it). An answer to this question, however, is a bit more guarded and says that:

[#error] renders the translation unit ill-formed (i.e., it causes compilation to fail)

which I don't read as saying the same thing as saying compilation terminates at that point.

So, is this behaviour of gcc correct?

Zorawar
  • 6,505
  • 2
  • 23
  • 41
  • "causes the compilation to fail" doesn't necessarily mean "causes the compilation to fail _right there right then_". Just that it won't succeed. – Mat Jul 27 '17 at 13:48
  • Concretely, if no output file was produced, the compilation failed. If you wanted it to fail quicker, then `-Wfatal-errors` is indeed the option for you. –  Jul 27 '17 at 13:49
  • @Mat: that's exactly what I want clarification on. The manual says fatal error. If I add `#include >` gcc will report: `fatal error: /: No such file or directory` and then terminate compilation at that point. So it's unclear to me exactly what is _supposed_ to happen. – Zorawar Jul 27 '17 at 13:55
  • @WumpusQ.Wumbley: But `-Wfatal-errors` makes errors into fatal ones. But `#error` should be fatal already, right? – Zorawar Jul 27 '17 at 13:57
  • That sentence in the cpp manual probably hasn't been updated since the `-Wfatal-errors` option was added in gcc 4.0. If you didn't know about the `-Wfatal-errors` option (like I didn't 15 minutes ago) then "fatal error" is just a synonym for "error" that only emphasizes the fact that it's not a warning. –  Jul 27 '17 at 14:01
  • @Zorawar: what is supposed to happen is that the compilation fails. That's it. Not really sure what more you want or why you care exactly when it fails. – Mat Jul 27 '17 at 14:06
  • @Mat: what I want is clarity from the manual. You know that that's what `#error` does. Good for you. But that doesn't unambiguously follow from what the manual says, which is why I asked the question. Why I care when it fails is that I want cut down useless compilation time. – Zorawar Jul 27 '17 at 14:15
  • @WumpusQ.Wumbley: probably, but I wanted to ask for sure since other answers on this site suggest compilation terminates at the point that the `#error` directive is encountered. – Zorawar Jul 27 '17 at 14:16
  • I can't find anything in the other questions/answers that does both of these things: 1. mention gcc specifically, and 2. say that `#error` causes immediate halt. –  Jul 27 '17 at 14:25

1 Answers1

0

"Error" and "fatal error" were not separate categories in gcc until fairly recently. People would say "fatal error" just to make sure you understood that the compilation would not produce the expected object file. "warning" and "error" were always separate, but that wasn't obvious to everyone. You might think a warning is a type of error if you weren't told otherwise.

I don't know whether there was ever a version of gcc that actually terminated immediately after the #error, but I doubt it. It's possible that in the era when cpp was actually a separate program, it would have terminated at the end of the preprocessing phase without calling the compiler proper; that would have resulted in less error messages. (But not when -pipe was used to run them simultaneously!)

You could report a bug against the cpp manual. It's using "fatal error" in a way that is no longer consistent with the use of the term in the rest of the gcc manual.