why is "cc1plus: warning: unrecognized command line option" for "no-" options only flagged by g++ when there is another warning?

c++, g++

Solution

This is by design, explained here:

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.

In other words, suppose you have `foo.cc`, and GCC-4.9 warns about something (let's call it `foobar`) in it, but you believe that your use of `foobar` is safe.

Since you want to treat all warnings as errors (with `-Werror`), you dutifully add `-Wno-foobar` to your `Makefile`.

Now someone else tries to build your code with GCC-4.8. As stated above, this produces no warning and he succeeds.

If this did produce a warning, you'd be unable to both use the `foobar` construct and have a single `Makefile` that worked with both GCC-4.8 and GCC-4.9.

Problem

``` > cat warning.cpp #pragma foobar > cat no_warning.cpp #pragma message "foobar" > g++ -Wall -Wno-foobar -c warning.cpp warning.cpp:1:0: warning: ignoring #pragma foobar [-Wunknown-pragmas] cc1plus: warning: unrecognized command line option "-Wno-foobar" [enabled by default] > g++ -Wall -Wno-foobar -c no_warning.cpp no_warning.cpp:1:17: note: #pragma message: foobar ```

Original source