How to disable warnings for individual include files?

I would like to disable special warnings for all files that are included, directly or indirectly, using separate files. For example, I want to disable the warning "you assign a string literal to char *" for all files or files included in the files included in #include <bar/*> (the star in my case means "something may be here" " )

The reason is that some of the people I have to program with just can't use "const", so in the end I get a lot of warnings about this particular abuse of string literals. I would like to ignore these thousands of warnings coming from their code, so I can focus on the errors in my own code and fix them.

I am using Intel C ++ and GCC. Some of my buddies use clang, so I would be glad to hear solutions for this.

+55
c ++ suppress-warnings
Jun 12. '11 at 12:07 on
source share
4 answers

When using GCC, you can use the -isystem flag instead of the -I flag to disable warnings from this location.

So if you are currently using

 gcc -Iparent/path/of/bar โ€ฆ 

using

 gcc -isystem parent/path/of/bar โ€ฆ 

instead of this. Unfortunately, this is not a particularly fine-grained control. I do not know a more focused mechanism.

+53
Jun 12 '11 at 12:27
source share

When I use g++ and I have third-party headers that generate a lot of warnings with my standard defaults -Wall -Wextra and co. I tend to group them into separate inclusions by specifying system_header #pragma .

[...] GCC provides the code found in the special processing system headers. All warnings, except those generated by #warning (see Diagnostics), are suppressed while GCC processes the system header. Macros defined in the system header are protected from several warnings, wherever they expand. This immunity is granted on a one-time basis when we discover that a warning generates a lot of false positives due to the code in the macros defined in the system headers.

[...]

There is also the #pragma GCC system_header , which tells GCC that the rest of the current include file contains the system header, regardless of where it was found. Code that precedes #pragma in the file will not be affected. #pragma GCC system_header does not affect the primary source file.

I prefer this solution for -isystem because it is finer-grained and I can use it directly in the sources, without messing up too much with command line arguments and including directories.

An example with a disgusting root library:

 #ifndef ROOTHEADERS_HPP_INCLUDED #define ROOTHEADERS_HPP_INCLUDED #ifdef __GNUC__ // Avoid tons of warnings with root code #pragma GCC system_header #endif #include "TH1F.h" #include "TApplication.h" #include "TGraph.h" #include "TGraph2D.h" #include "TCanvas.h" #endif 
+28
Jun 12 '11 at 12:37
source share

Best GCC Solution: Use #pragma.

 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-W<evil-option>" #include <evil_file> #pragma GCC diagnostic pop 

eg:

 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QtXmlPatterns> #pragma GCC diagnostic pop 
+17
May 01 '15 at 20:50
source share

I think the easiest solution would be to write a simple script that the compiler will call, compile and split the unwanted output based on the file name and type of warning. You can use different scripts for each compiler.

Just update your makefile to use this script instead of calling the compiler directly.

0
Jun 12 '11 at 12:23
source share



All Articles