How to suppress GCC warnings from library headers?

GccWarningsSuppress WarningsGcc Warning

Gcc Problem Overview


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.

Gcc Solutions


Solution 1 - Gcc

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.

Solution 2 - Gcc

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")
                    ^^^^^^

Solution 3 - Gcc

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

Solution 4 - Gcc

I found the trick. For library includes, instead of -Idir use -isystem dir in the makefile. GCC then treats boost etc. as system includes and ignores any warnings from them.

Solution 5 - Gcc

#pragma are instructions to the compiler. you can set something before the #include and disable it after.

You can also do it at the command line.

Another GCC page specifically on disabling warnings.

I would go for the option of using #pragma's within the source code, and then providing a sound reason (as a comment) of why you are disabling the warnings. This would mean reasoning about the headers files.

GCC approaches this by classifying the warning types. You can classify them to be warnings or to be ignored. The previously linked articles will show you which warnings are may be disabled.

Note: you can also massage the source code to prevent certain warnings by using attributes; however, this bind you quite closely to GCC.

Note2: GCC also uses the pop/push interface as used in microsoft's compiler -- Microsoft disables warnings through this interface. I suggest you investigate this further , as I do not know if it is even possible.

Solution 6 - Gcc

You can try using precompiled headers. Warnings won't go away but at least the won't show up in your main compilation.

Solution 7 - Gcc

Putting the following

#pragma GCC system_header

will turn off GCC warnings for all following code in this file.

Solution 8 - Gcc

If you need to explicitly override a system header then you're restricted to pragmas. You can verify which includes you're using via make depend output.

Also see diagnostic push-pop for gcc >= 4.6

Solution 9 - Gcc

Another way to do it is, in the makefile, to tell the compiler to ignore warnings for the specific folder:

$(BUILD_DIR)/libs/%.c.o: CFLAGS += -w

Solution 10 - Gcc

There must be reasons for those warnings. These will either be caused by errors in your code that uses the library, or by errors in the library code itself. In the first case, fix your code. In the second case, either stop using the library or if it is FOSS code, fix it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdSRView Question on Stackoverflow
Solution 1 - GccPhiView Answer on Stackoverflow
Solution 2 - GccDrew NoakesView Answer on Stackoverflow
Solution 3 - GccandrewrjonesView Answer on Stackoverflow
Solution 4 - GccAdSRView Answer on Stackoverflow
Solution 5 - GccHassan SyedView Answer on Stackoverflow
Solution 6 - GccPablo Santa CruzView Answer on Stackoverflow
Solution 7 - GccEvgeniiView Answer on Stackoverflow
Solution 8 - GccsupaflavView Answer on Stackoverflow
Solution 9 - GccDianaNowaView Answer on Stackoverflow
Solution 10 - GccanonView Answer on Stackoverflow