3

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

mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • How do you build your project? Do you have a `Makefile`? Is it generated? How? Why do you need to suppress warnings, they are only warnings? Why can't you correct the source code to avoid warnings? – Basile Starynkevitch Nov 19 '15 at 14:26
  • @BasileStarynkevitch I use Makefile that used in Eclipse project. – mrgloom Nov 19 '15 at 14:26
  • 1
    Then improve your question to some some of the `Makefile`. But why can't you correct the source code? Pleas **edit your question to *improve* it.** – Basile Starynkevitch Nov 19 '15 at 14:27
  • @BasileStarynkevitch I want to correct warnings only in some files first and then go to other files with warnings. – mrgloom Nov 19 '15 at 14:30
  • You can do it with this approach : http://stackoverflow.com/questions/965093/selectively-disable-gcc-warnings-for-only-part-of-a-translation-unit – nos Nov 19 '15 at 14:31
  • BTW, warnings are only warnings. They don"t stop the compiler (unless you give it `-Werror`) – Basile Starynkevitch Nov 19 '15 at 14:32
  • It is in boost and clang as well. And if I just can spell nounused as no-unused correctly in settings the warning is not shown. :-) – kometen Feb 02 '16 at 07:41
  • Does this answer your question? [How to suppress GCC warnings from library headers?](https://stackoverflow.com/questions/1867065/how-to-suppress-gcc-warnings-from-library-headers) – ggorlen Feb 05 '20 at 20:15

2 Answers2

8

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!

Matt Godbolt
  • 1,381
  • 11
  • 14
  • 1
    Seems when I add `#pragma GCC diagnostic ignored ` to *.h files it's also applies also to *.cpp files where this *.h is included, this is not desired behavior, how to fix it? – mrgloom Feb 08 '16 at 08:11
2

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:

rubicks
  • 4,912
  • 1
  • 30
  • 41