cc1plus:无法识别的命令行选项警告任何其他警告

cc1plus: unrecognized command line option warning on any other warning

我有一个奇怪的 g++ 行为,它会在显示任何其他警告时显示有关无法识别的命令行选项的警告。

示例:

struct Foo{virtual int bar() = 0;};
struct Bar:public Foo{int bar() {return 0;} };

int main(){}

编译 g++-5 -Wsuggest-override -Wno-c99-extensions -std=c++11 a.cpp 甚至 g++-5 -Wsuggest-override -Wno-c99-extensions a.cpp 显示:

a.cpp:2:27: warning: ‘virtual int Bar::bar()’ can be marked override [-Wsuggest-override]
 struct Bar:public Foo{int bar() {return 0;} };
                           ^
cc1plus: warning: unrecognized command line option ‘-Wno-c99-extensions’

添加:当我使用 g++-5 -Wno-c99-extensions a.cpp 编译时没有 warning/error 因此该选项通过使用 CHECK_CXX_COMPILER_FLAG

的 CMAKE 检查

这让我很困扰,因为我们使用 Werror,但通过 Wno-error=... 例外,然后当 "unrecognized command line option" 显示任何(非错误)警告时,它就会退出

这是known/expected吗?如何预防?

如果您不想收到有关无法识别的命令行选项的警告,请不要使用无法识别的命令行选项:-Wno-c99-extensions 从来都不是有效的 GCC 选项(我认为这是一种陈词滥调)。只需将其从构建命令中删除即可。

至于为什么只有当你有 另一个 警告时才发出警告,这种行为似乎违反直觉,但 it is actually deliberate and documented:

The warning "unrecognized command-line option" is not given for -Wno-foo

Since GCC 4.4, and as explained in the GCC manual: when an unrecognized warning option is requested (-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. (See PR28322 for the history of this change)

This might break configure tests that check for -Wno-foo options. The solution is to either test for the positive form (-Wfoo) or test using a testcase that triggers some other warning.

事实上,在 Google 的邮件列表线程中有一些结果表明软件开发人员遇到了这个问题 "problem",特别是 CMake,并对他们的构建脚本进行了微不足道的修复 "fix"它。