How to suppress GCC warnings from library headers?

I have a project that uses log4cxx, boost, etc. libraries whose headers generate lots of (repetitive) warnings. Is there a way to suppress warnings from library includes (i.e. #include <some-header.h>) or includes from certain paths? I'd like to use -Wall and/or -Wextra as usual on project code without relevant info being obscured. I currently use grep on make output but I'd like something better.


You may try to include library headers using -isystem instead of -I. This will make them "system headers" and GCC won't report warnings for them.


For those using CMake, you can modify your include_directories directives to include the symbol SYSTEM which suppresses warnings against such headers.

include_directories(SYSTEM "${LIB_DIR}/Include")
                    ^^^^^^

You can use pragmas. For example:

// save diagnostic state
#pragma GCC diagnostic push 

// turn off the specific warning. Can also use "-Wall"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/lexical_cast.hpp>

// turn the warnings back on
#pragma GCC diagnostic pop