Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 243
mrgloom Avatar asked Jan 26 '26 10:01

mrgloom


1 Answers

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 #pragmas 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!

like image 134
Matt Godbolt Avatar answered Jan 28 '26 00:01

Matt Godbolt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!