c++ - 包含特定文件时可以抑制所有警告吗?

标签 c++ gcc compiler-warnings suppress-warnings

我正在使用最新版本的 gcc,编写 C++ 代码(但我想要一个也适用于 C 的答案)。我启用了各种额外的警告标志。

在我的代码中,我包含了一些流行库的 header ,#include <mylib.h> 。现在,mylib.h触发其中一些编译器警告;但我需要按原样包含它,我无法更改它。

我想在包含 mylib.h 时抑制所有编译器警告。我知道我可以通过以下方式抑制个别警告:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwarning-name-here"
#pragma GCC diagnostic ignored "-Wanother-warning-name-here"

// my code goes here

#pragma GCC diagnostic pop

但我想抑制所有警告。我可以这样做吗?请注意,如果有帮助,我愿意调整编译器命令行。

注意:这个related question表明,在 2015 年,人们无法使用编译指示来做到这一点。

最佳答案

来自 GCC 手册:

2.7 System Headers

The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, 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.

Normally, only the headers found in specific directories are considered system headers. These directories are determined when GCC is compiled. There are, however, two ways to make normal headers into system headers.

The -isystem command line option adds its argument to the list of directories to search for headers, just like -I. Any headers found in that directory will be considered system headers.

All directories named by -isystem are searched after all directories named by -I, no matter what their order was on the command line. If the same directory is named by both -I and -isystem, the -I option is ignored. GCC provides an informative message when this occurs if -v is used.

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.


how to make this happen in CMake?

来自 CMake 手册:

include_directories

Add include directories to the build.

include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])

...

If the SYSTEM option is given, the compiler will be told the directories are meant as system include directories on some platforms. Signalling this setting might achieve effects such as the compiler skipping warnings, or these fixed-install system files not being considered in dependency calculations - see compiler docs.

关于c++ - 包含特定文件时可以抑制所有警告吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72072374/

相关文章:

java - 忽略返回对象时的编译时警告

Java Class.cast() 与强制转换运算符

c++ - MATLAB 在 mex_compile Linux 中找不到支持的编译器或 SDK

c - 为什么 GCC 会将字复制到返回寄存器而不是字节?

c - 为什么 C 程序使用 libmath 的 .so 比使用 libmath 的 .a 显示更多的内存?

c++ - 不识别键盘输入

c++ - Sun Studio C++ "is not terminated with a newline"警告 - 如何抑制?

c++ - Eclipse 忽略环境中的 LD_LIBRARY_PATH

c++ - 当两者都是变量时如何将 float 除以整数?

c++ - 为什么 case 语句只接受常量?