Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress specific warnings in g++

Tags:

c++

gcc

g++

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?

like image 878
Gilad Naor Avatar asked Jan 28 '09 10:01

Gilad Naor


People also ask

How do I supress a warning in GCC?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall .

How do I get rid of error warning?

You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).

How do I ignore a warning in Makefile?

Maybe you can look for CFLAGS options in Makefile and remove the -Werror flag. The Werror flag will make all warnings into errors. Show activity on this post. In general, it is not a good idea to ignore warnings from your compiler.


2 Answers

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)

like image 116
Luc Touraille Avatar answered Sep 30 '22 17:09

Luc Touraille


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.

like image 40
JesperE Avatar answered Sep 30 '22 18:09

JesperE