gcc - 如何禁用特定的 gcc 迂腐警告?

标签 gcc compiler-warnings

我在谷歌上搜索和搜索,要么谷歌让我失望,要么你不能这样做。当您打开 -Wpedantic 时会出现此警告...

ISO C++ forbids zero-size array ‘variable’ [-Wpedantic]



我想关闭这个警告,而不是所有迂腐的警告。
通常,我只会添加 -Wno-xyz但我找不到与该警告相关的标志名称。它只是没有在任何地方列出。

迂腐警告是否特别,因为您不能单独删除它们?

最佳答案

好消息:你可以做到这一点。坏消息:您不能使用任何命令行选项。 [-Wpedantic]在诊断结束时
告诉你 -Wno-pedantic是禁用诊断的最窄选项,如果您想保留所有内容,这对您没有用
其他迂腐的诊断方法。

您必须使用编译指示逐案处理。

main.cpp

int main(int argc, char *argv[])
{
    int a[0];
    int b[argc];
    return sizeof(a) + sizeof(b);
}

这个节目激起了两个-Wpedantic诊断:
$ g++ -Wpedantic -c main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:6:12: warning: ISO C++ forbids zero-size array ‘a’ [-Wpedantic]
     int a[0];
            ^
main.cpp:8:15: warning: ISO C++ forbids variable length array ‘b’ [-Wvla]
     int b[argc];
               ^
-Wno-vla将抑制第二个。要压制第一个,你必须
求助于:

main.cpp(修订版)
int main(int argc, char *argv[])
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
    int a[0];
#pragma GCC diagnostic pop
    int b[argc];
    return sizeof(a) + sizeof(b);
}

其中:
$ g++ -Wpedantic -c main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8:15: warning: ISO C++ forbids variable length array ‘b’ [-Wvla]
     int b[argc];
               ^

关于gcc - 如何禁用特定的 gcc 迂腐警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48566174/

相关文章:

c++ - 标准库警告是否正常?

f# - "Uppercase variable identifiers should not generally be used in patterns..."是怎么回事?

c++ - 如何让编译器警告这是一个无效的 boolean 值?

linux - 在服务器上安装第二个 gcc

c++ - 如何避免 "(void)a"cast 导致副作用?

配置:错误:安装或配置 — C 编译器无法创建可执行文件

c++ - 我可以使用 xbuild 在 Linux 上编译 Visual C++ 项目吗?

c++ - "Static member function overrides a virtual function in a base class"被 gcc 和 clang 捕获,但未被 VC++ 捕获

c - 警告 : control reaches end of non-void function

CUDA ptxas 警告(入口的堆栈大小)