English 中文(简体)
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

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.

#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.

Putting the following

#pragma GCC system_header

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

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

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

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




相关问题
gcc -fPIC seems to muck with optimization flags

Following along from this question: how-do-i-check-if-gcc-is-performing-tail-recursion-optimization, I noticed that using gcc with -fPIC seems to destroy this optimization. I am creating a shared ...

Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Getting rid of pre-compiled headers

OK, I have old Metrowerks code for Mac and Windows where the previous developer used pre-compiled headers for every project that this code base builds. How does one get rid of Pre-compiled headers, ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

How to compile for Mac OS X 10.5

I d like to compile my application for version 10.5 and forward. Ever since I upgraded to Snow Leopard and installed the latest XCode, gcc defaults to 10.6. I ve tried -isysroot /Developer/SDKs/...

热门标签