> 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
Run Code Online (Sandbox Code Playgroud)
这是设计使然,解释如下:
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.
Run Code Online (Sandbox Code Playgroud)
换句话说,假设您有foo.cc, 并且 GCC-4.9 对其中的某些内容(我们称之为foobar)发出警告,但您相信您的使用foobar是安全的。
由于您希望将所有警告视为错误(使用-Werror),因此您尽职尽责地添加-Wno-foobar到您的Makefile.
现在其他人尝试使用 GCC-4.8 构建您的代码。如上所述,这不会产生任何警告,并且他成功了。
如果这确实产生了警告,您将无法同时使用该foobar构造并拥有一个同时Makefile适用于 GCC-4.8 和 GCC-4.9 的单个构造。