30

In the software project I'm working on, we use certain 3rd party libraries which, sadly, produce annoying gcc warnings. We are striving to clean all code of warnings, and want to enable the treat-warnings-as-errors (-Werror) flag in GCC. Is there a way to make these 3rd party generated warnings, which we cannot fix, to disappear?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Michael
  • 2,826
  • 4
  • 25
  • 18

4 Answers4

47

Use -isystem Example:

gcc -I./src/ -isystem /usr/include/boost/ -c file.c -o obj/file.o

With -isystem NO warning about boost :D

ImmortalPC
  • 1,650
  • 1
  • 13
  • 17
22

If you're using CMake, you can achieve this by adding SYSTEM to include_directories:

include_directories(SYSTEM "${LIB_DIR}/Include")
                    ^^^^^^
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 2
    I tested this under GCC and it works great, but it doesn't do anything under MSVC. Not unexpected because MSVC doesn't seem to have any way of specifying system header directories (i.e. GCC's -isystem), but something to keep in mind if you need MSVC support. – Kevin Jun 12 '15 at 16:24
  • Doesn't seem to do anything for me on Xcode/Clang either. – winduptoy Jul 19 '16 at 21:04
  • 1
    @Kevin: Microsoft appears to have addressed this finally, regarding to [this blog post](https://devblogs.microsoft.com/cppblog/broken-warnings-theory/). You can now specify `/external:I` before an include. Using CMake, you can `SET(CMAKE_INCLUDE_SYSTEM_FLAG_CXX "/external:I ")` for MSVC > VS 15.6. You also might have to add `/experimental:external` to `CMAKE_CXX_FLAGS`. For earlier VS versions, you have to use pragmas, however. – Carsten Mar 24 '19 at 11:40
18

I presume you are talking about the warnings coming from the 3rd party library headers.

The GCC specific solution would be to create another wrapper header file which has essentially the two lines:

#pragma GCC system_header
#include "real_3rd_party_header.h"

And use the wrapper instead of the original 3rd party header.

Check another SO response detailing the pragma. It essentially tells GCC that this (with recursively included files) is a system header, and no warning messages should be generated.

Otherwise, I'm not aware how one can disable warnings coming from the 3rd party code. Except by the brute force of course: in the build system configure the files to be built with warnings off.

Community
  • 1
  • 1
Dummy00001
  • 16,630
  • 5
  • 41
  • 63
3

http://www.artima.com/cppsource/codestandards.html

Example 1: A third-party header file. A library header file that you cannot change could contain a construct that causes (probably benign) warnings. Then wrap the file with your own version that #includes the original header and selectively turns off the noisy warnings for that scope only, and then #include your wrapper throughout the rest of your project.

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217