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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:37:43  来源:igfitidea点击:

How to suppress specific warnings in g++

c++gccg++

提问by Gilad Naor

I want to suppress specific warnings from g++. I'm aware of the -Wno-XXXflag, 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 未使用的变量