如何禁用特定的 gcc 迂腐警告?
How can I disable a specific gcc pedantic warning?
我有 googled 和 googled,要么 google 让我失望了,要么你不能这样做。当您打开 -Wpedantic
...
时会出现此警告
ISO C++ forbids zero-size array ‘variable’ [-Wpedantic]
我想关闭这一个警告,而不是所有迂腐的警告。
通常,我只是添加 -Wno-xyz
但我找不到与该警告相关的标志名称。它只是没有在任何地方列出。
这些迂腐的警告是否特别,因为您不能单独删除它们?
好消息:您可以做到这一点。坏消息:您不能使用任何命令行选项。诊断结束时的 [-Wpedantic]
告诉你 -Wno-pedantic
是最窄的选项,它将禁用诊断,如果你想保留所有,这对你没有用
其他迂腐的诊断。
您必须根据实际情况使用 pragma 进行处理。
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];
^
好吧,你可以使用 pragma 来禁用它,但如果你想便携,你可以使用零大小 std::array
代替:
#include <array>
//...
std::array<int, 0> ar;
无论如何,建议在普通数组上使用 std::array
。
我有 googled 和 googled,要么 google 让我失望了,要么你不能这样做。当您打开 -Wpedantic
...
ISO C++ forbids zero-size array ‘variable’ [-Wpedantic]
我想关闭这一个警告,而不是所有迂腐的警告。
通常,我只是添加 -Wno-xyz
但我找不到与该警告相关的标志名称。它只是没有在任何地方列出。
这些迂腐的警告是否特别,因为您不能单独删除它们?
好消息:您可以做到这一点。坏消息:您不能使用任何命令行选项。诊断结束时的 [-Wpedantic]
告诉你 -Wno-pedantic
是最窄的选项,它将禁用诊断,如果你想保留所有,这对你没有用
其他迂腐的诊断。
您必须根据实际情况使用 pragma 进行处理。
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];
^
好吧,你可以使用 pragma 来禁用它,但如果你想便携,你可以使用零大小 std::array
代替:
#include <array>
//...
std::array<int, 0> ar;
无论如何,建议在普通数组上使用 std::array
。