How to suppress specific warnings in g++

爱⌒轻易说出口 提交于 2019-12-17 16:24:56

问题


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 some of the warnings in -Weffc++, but not all of them. Something like what you can do with lint - disable specific messages.

Is there a built in way in gcc to do this? Do I have to write a wrapper script?


回答1:


Unfortunately, this feature isn't provided by g++. In VC++, you could use #pragma warning to 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.




回答2:


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.




回答3:


You could just use grep -v on the output.

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'

Whereas this does not:

#define MARKUSED(X)  ((void)(&(X)))

int main()
{
  int i;
  MARKUSED(i);
}



回答4:


pipe standard error to a filter that removes things you don't want to see. For example, this is my make file:

main.o: main.cpp g++ -c -Wall main.cpp 2>&1 | grep -v Wunused-variable



来源:https://stackoverflow.com/questions/487108/how-to-suppress-specific-warnings-in-g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!