> 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
答案 0 :(得分:3)
这是设计问题,解释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.
换句话说,假设您有foo.cc
,并且GCC-4.9警告某些内容(我们称之为foobar
),但您相信使用foobar
是安全的
由于您希望将所有警告视为错误(使用-Werror
),因此您可以尽职地将-Wno-foobar
添加到Makefile
。
现在有人尝试使用GCC-4.8构建您的代码。如上所述,这会产生 no 警告并且他成功。
如果这确实产生了警告,那么您将无法同时使用foobar
构造,并且只有一个Makefile
可用于GCC-4.8和GCC-4.9。