简体   繁体   中英

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. I use Makefile that used in Eclipse project.

How can I do it?

Update:

Seems Diagnostic Pragmas used for that:

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. If not, similar techniques work for Visual Studio and other compilers too.

If you have a Makefile that you can easily modify, you can supply different compiler flags to build each file. Disabling a particular warning is as easy as adding a -Wno-<warning-name> to the build line, for example: -Wno-unused-local-typedefs .

If you can't easily modify your makefile, you can place #pragma s into your source code directly. For example, you can add a line like this to the top of the C++ file:

#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:

# Makefile

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

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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