Petr Skocik
Petr Skocik

Reputation: 60067

Eliciting an "unrecognized command line option" from gcc

I'm using gcc 7.3. If I compile e.g., int main(){ } with gcc main.c -Wno-foo, the compilation succeeds and I get no error output.

Changing the source to int main(){ int a[-1]; /*uncompilable*/ } while maintaining the build command gets me:

main.c: In function ‘main’:
main.c:3:6: error: size of array ‘a’ is negative
  int a[-1];
      ^
main.c: At top level:
cc1: warning: unrecognized command line option ‘-Wno-foo’

Can I force the unrecognized command line option ‘-Wno-foo’ warning/error message without introducing a compilation error into the source?

-Wfoo (without the no-) gets me a warning message right away, but I'd rather keep the no- in there. (This is for compiler feature testing, which I do as part of my build.)

Upvotes: 3

Views: 4529

Answers (1)

Wouter Verhelst
Wouter Verhelst

Reputation: 1292

This is apparently a feature in gcc, not a bug. From the GCC manual:

When an unrecognized warning option is requested (e.g., '-Wunknown-warning'), GCC emits a diagnostic stating that the option is not recognized. However, if the '-Wno-' form is used, the behavior is slightly different: no diagnostic is produced for '-Wno-unknown-warning' unless other diagnostics are being produced. This allows the use of new '-Wno-' options with old compilers, but if something goes wrong, the compiler warns that an unrecognized option is present.

So, if you need to know whether the option is present, you need to use it without the no- in there. Do that first; if you discover that it is supported, do whatever test you were going to do that required the -Wno-foo argument.

Upvotes: 4

Related Questions