问题
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.
回答1:
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.
回答2:
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")
^^^^^^
回答3:
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
回答4:
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.
回答5:
#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.
回答6:
You can try using precompiled headers. Warnings won't go away but at least the won't show up in your main compilation.
回答7:
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
回答8:
Putting the following
#pragma GCC system_header
will turn off GCC warnings for all following code in this file.
回答9:
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.
来源:https://stackoverflow.com/questions/1867065/how-to-suppress-gcc-warnings-from-library-headers