简体   繁体   English

使用g ++构建时如何抑制某些文件中的警告?

[英]How to supress warnings from some files when building with g++?

For example I'm building project which consist of several *.cpp files and I need to supress warning only from some of this files. 例如,我正在构建包含多个* .cpp文件的项目,我只需要从这些文件中提取警告。 I use Makefile that used in Eclipse project. 我使用Eclipse项目中使用的Makefile。

How can I do it? 我该怎么做?

Update: 更新:

Seems Diagnostic Pragmas used for that: 似乎诊断编译用于此:

https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html

It depends how you're building your project, and which compiler. 这取决于您构建项目的方式以及编译器。 I'm assuming you're on Linux and you're using GCC. 我假设你在Linux上并且你正在使用GCC。 If not, similar techniques work for Visual Studio and other compilers too. 如果没有,类似的技术也适用于Visual Studio和其他编译器。

If you have a Makefile that you can easily modify, you can supply different compiler flags to build each file. 如果您有一个可以轻松修改的Makefile ,则可以提供不同的编译器标志来构建每个文件。 Disabling a particular warning is as easy as adding a -Wno-<warning-name> to the build line, for example: -Wno-unused-local-typedefs . 禁用特定警告就像在构建行中添加-Wno-<warning-name>一样简单,例如: -Wno-unused-local-typedefs

If you can't easily modify your makefile, you can place #pragma s into your source code directly. 如果您无法轻松修改makefile,可以直接将#pragma s放入源代码中。 For example, you can add a line like this to the top of the C++ file: 例如,您可以将这样的行添加到C ++文件的顶部:

#pragma GCC diagnostic ignored "-Wno-unused-local-typedefs"

Of course, the real solution is to fix the warnings. 当然,真正的解决方案是修复警告。 There are very few warnings that aren't worth heeding: Often ignoring warnings will cause you more pain in the long run! 很少有警告不值得注意:从长远来看,经常忽略警告会让你更加痛苦!

To disable all warnings you can 要禁用所有警告

# Makefile

# disable all warnings
CXXFLAGS += -w

To disable warnings for a specific *.cpp file, try assigning target-specific variables for the relevant object file: 要禁用特定*.cpp文件的警告,请尝试为相关目标文件分配特定目标的变量

# Makefile

# disable warnings on foobar.cpp
foobar.o: CXXFLAGS += -w

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM