C++ 如何禁用特定包含文件的警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6321839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable warnings for particular include files?
提问by Johannes Schaub - litb
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").
我想禁用特定包含文件直接或间接包含的所有文件的特定警告。例如,我想针对所有文件或 a 包含的文件所包含的文件禁用警告“您正在为 char* 分配字符串文字” #include <bar/*>
(在我的情况下,星号表示“任何东西都可能在这里”)。
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.
原因是,我必须与之一起编程的一些人不能使用“const”,所以最后我收到了很多关于该特定字符串文字滥用的警告。我想忽略来自他们代码的数千个警告,这样我就可以专注于自己代码中的错误并修复它们。
I use Intel C++ and GCC. Some of my buddies use clang, so I would be glad to hear solutions for that too.
我使用英特尔 C++ 和 GCC。我的一些朋友使用 clang,所以我也很高兴听到解决方案。
回答by Konrad Rudolph
When using GCC you can use the -isystem
flag instead of the -I
flag to disable warnings from that location.
使用 GCC 时,您可以使用-isystem
标志而不是-I
标志来禁用来自该位置的警告。
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.
反而。不幸的是,这不是一个特别细粒度的控件。我不知道更有针对性的机制。
回答by Brad
A better GCC solution: use #pragma.
更好的 GCC 解决方案:使用 #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
回答by Matteo Italia
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
.
当我使用g++
并且我有第三方标头时,这些标头会以我通常的默认值-Wall -Wextra
& co生成大量警告。我倾向于将它们分组在单独的包含中,指定.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.
[...] GCC 对系统头文件中的代码进行了特殊处理。
#warning
在 GCC 处理系统标头时,除由(请参阅诊断)生成的警告之外的所有警告都将被抑制。系统标头中定义的宏在扩展时不受一些警告的影响。当我们发现警告由于系统标头中定义的宏中的代码而产生大量误报时,这种豁免是临时授予的。[...]
还有一个指令,
#pragma GCC system_header
,它告诉 GCC 将当前包含文件的其余部分视为系统头,无论它在哪里找到。#pragma
文件中之前的代码不会受到影响。#pragma GCC system_header
对主要源文件没有影响。
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.
我更喜欢这个解决方案,-isystem
因为它更细粒度,我可以直接将它放在源代码中,而不会过多地使用命令行参数和包含目录。
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
回答by Gilad Naor
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.
只需更新您的 makefile 以使用此脚本,而不是直接调用编译器。