Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable warnings for particular include files?

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

The reason is, some of the people I have to program with just can't use "const", so in the end I get lots of warnings about that particular string literal abuse. I would like to ignore those thousands of warnings coming from their code, so I can concentrate on the mistakes in my own code and fix them.

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

like image 693
Johannes Schaub - litb Avatar asked Jun 12 '11 12:06

Johannes Schaub - litb


People also ask

How do I turn off error warnings?

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 you supress a warning in C++?

To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.


2 Answers

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

So if you’re currently using

gcc -Iparent/path/of/bar … 

use

gcc -isystem parent/path/of/bar … 

instead. Unfortunately, this isn’t a particularly fine-grained control. I’m not aware of a more targeted mechanism.

like image 168
Konrad Rudolph Avatar answered Sep 18 '22 13:09

Konrad Rudolph


A better GCC solution: use #pragma.

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

for example:

#pragma GCC diagnostic push  #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QtXmlPatterns> #pragma GCC diagnostic pop 
like image 33
Brad Avatar answered Sep 18 '22 13:09

Brad