37

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?

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
Gilad Naor
  • 20,752
  • 14
  • 46
  • 53
  • 1
    See [this answer](https://stackoverflow.com/questions/3378560/how-to-disable-gcc-warnings-for-a-few-lines-of-code/26003732#26003732) if you want to disable the warnings for `n` lines of code. – Keith Morgan Oct 06 '16 at 10:31
  • In case you are wiliiing to add it to the source code file(s) you can do the following as described here (and probably in other answers as well): https://codeyarns.com/2014/03/11/how-to-selectively-ignore-a-gcc-warning/ – Guy Avraham Jul 18 '18 at 12:39

5 Answers5

25

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.

Edit: GCC supports pushing/popping warnings since 4.6.4 (see changelog)

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
  • This is not correct, you can use the diagnostic pragma just around one line of code by pushing/popping the state and then tweaking the diagnostic within that push/pop area. – Alexis Wilke Jul 08 '21 at 20:05
  • 1
    @AlexisWilke Indeed, this was added in gcc a few years after my answer, let me update it! – Luc Touraille Jul 15 '21 at 08:51
13

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.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
JesperE
  • 63,317
  • 21
  • 138
  • 197
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);
}
Mr.Ree
  • 8,320
  • 27
  • 30
1

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
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Boris Z
  • 19
  • 1
0

When my colleagues are trying to prevent me from writing concise code, they use g++ command-line option

-Werror-parenthesis

And I disable this error in my code:

#pragma GCC diagnostic ignored "-Wparentheses"
Frank Puck
  • 467
  • 1
  • 11