如何使用 GCC pragma 启用 -Werror?

L29*_*9Ah 5 c c++ gcc

我有几个文件,我想在其中严格警告警告,并且我使用 GCC 来构建我的项目。

我已经#pragma GCC diagnostic error "-Wall"按照https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Diagnostic-Pragmas.html尝试过,但它没有考虑到其他一些启用的警告类型:

foo.c:666:6: warning: passing argument 2 of 'bar' from incompatible pointer type [-Wincompatible-pointer-types]
Run Code Online (Sandbox Code Playgroud)

有没有办法为文件启用 -Werror 就像它是从命令行提供的一样(或者,至少,对于隐式启用的警告集),所以任何警告都会触发错误?

Ctx*_*Ctx 1

对于这种情况,您可以使用

#pragma GCC diagnostic error "-Wincompatible-pointer-types"
Run Code Online (Sandbox Code Playgroud)

例如在

#pragma GCC diagnostic error "-Wincompatible-pointer-types"
void foo(int * a)
{
}

void bar() {
        foo("foo");
}
Run Code Online (Sandbox Code Playgroud)

不支持将-Wall与此编译指令一起使用。仅支持诊断选项,显示为-fdiagnostics-show-option(无论如何,这是今天的默认设置),如上面的示例警告所示。