如何禁用特定包含文件的警告?

我希望禁用特定包含文件直接或间接包含的所有文件的特定警告。例如,对于 #include <bar/*>包含的文件所包含的所有文件,我想禁用警告“你正在为 char * 分配一个字符串文字”(在我的例子中,星号表示“任何东西都可能在这里”)。

原因是,我必须编程的一些人就是不能使用“ const”,所以最后我得到了很多关于这个字符串字面滥用的警告。我希望忽略来自他们代码的成千上万的警告,这样我就可以专注于我自己代码中的错误并修复它们。

我使用英特尔 C + + 和海湾合作委员会。我的一些朋友使用叮当,所以我会很高兴听到的解决方案太。

40572 次浏览

I guess the simplest solution would be write a simple script that will call the compiler, compile, and strip the undesired output, based on the filename and warning type. You can use different scripts for each compiler.

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

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.

When I use g++ and I have third party headers that generate tons of warnings with my usual defaults of -Wall -Wextra & co. I tend to group them in separate includes, specifying the system_header #pragma.

[...] GCC gives code found in system headers special treatment. All warnings, other than those generated by #warning (see Diagnostics), are suppressed while GCC is processing a system header. Macros defined in a system header are immune to a few warnings wherever they are expanded. This immunity is granted on an ad-hoc basis, when we find that a warning generates lots of false positives because of code in macros defined in system headers.

[...]

There is also a directive, #pragma GCC system_header, which tells GCC to consider the rest of the current include file a system header, no matter where it was found. Code that comes before the #pragma in the file will not be affected. #pragma GCC system_header has no effect in the primary source file.

I prefer this solution to the -isystem one because it's more fine-grained and I can put it directly in the sources, without messing too much with command line arguments and include directories.

Example with the hideous 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

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

Copying my answer from a duplicate thread.

You could use suppression pragmas.

This is supported for GCC and VC++ compilers, and it looks like this:

#pragma warning(push)
#pragma warning(disable : 4244)
#pragma warning(disable : 4127)
#pragma warning(disable : 4512)
#include <boost/python.hpp>
#pragma warning(pop)

Here are the detailed specs: