C++ 如何抑制 g++ 中的特定警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/487108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress specific warnings in g++
提问by Gilad Naor
I want to suppress specific warnings from g++. I'm aware of the -Wno-XXX
flag, but I'm looking for something more specific. I want someof the warnings in -Weffc++
, but not allof them. Something like what you can do with lint - disable specific messages.
我想抑制来自 g++ 的特定警告。我知道这个-Wno-XXX
标志,但我正在寻找更具体的东西。我想要 中的一些警告-Weffc++
,但不是全部。类似于您可以使用 lint 执行的操作 - 禁用特定消息。
Is there a built in way in gcc to do this? Do I have to write a wrapper script?
gcc 中是否有内置的方法来做到这一点?我是否必须编写包装脚本?
采纳答案by Luc Touraille
Unfortunately, this feature isn't provided by g++. In VC++, you could use #pragma warningto disable some specific warnings. In gcc, the closest you can have is diagnostic pragmas, which let you enable/disable certain types of diagnostics for certain files or projects.
不幸的是,这个特性不是由 g++ 提供的。在 VC++ 中,您可以使用#pragma warning禁用某些特定警告。在 gcc 中,您可以拥有的最接近的是诊断编译指示,它允许您启用/禁用某些文件或项目的某些类型的诊断。
回答by JesperE
For some warnings, there is a command line switch to disable them. In order to know which switch to use, pass -fdiagnostics-show-option to gcc.
对于某些警告,有一个命令行开关可以禁用它们。为了知道使用哪个开关,将 -fdiagnostics-show-option 传递给 gcc。
回答by Mr.Ree
You could just use grep -von the output.
您可以在输出上使用grep -v。
Depending on the warning you wish to disable, you can sometimes correct in code. E.g.:
根据您希望禁用的警告,您有时可以在代码中更正。例如:
int main()
{
int i;
}
Generates:foo.cc:4: warning: unused variable 'i'
生成:foo.cc:4:警告:未使用的变量“i”
Whereas this does not:
而这不会:
#define MARKUSED(X) ((void)(&(X)))
int main()
{
int i;
MARKUSED(i);
}
回答by Boris Z
pipe standard error to a filter that removes things you don't want to see. For example, this is my make file:
将标准错误传送到过滤器,以删除您不想看到的内容。例如,这是我的 make 文件:
main.o: main.cpp g++ -c -Wall main.cpp 2>&1 | grep -v Wunused-variable
main.o: main.cpp g++ -c -Wall main.cpp 2>&1 | grep -v 未使用的变量