I have code:
#ifdef Q_OS_LINUX
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcomment"
#include "header.h"
#pragma GCC diagnostic pop
#endif
And I want to supress GCC warning messages related to header.h and all headers included from header.h. But I still have '-Wcomment' warnings related to headers included from header.h. How can I avoid that? Thanks
gcc 4.8.2
edit: The Warning I get looks like this:
/------ Set Analog Output for 8022/8026 --------- /Exp8K WORD
CALLBACK AnalogOutHex_8K(DWORD dwBuf[], float fBuf[], warning: "/"
within comment [-Wcomment] No other pragmas surely. -Wall doesn't work
If you can modify header.h, you could define it to be a system header using #pragma GCC system_header. Otherwise, you could add it to your gcc command line using -isystem.
All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header.
GCC warnings that are emitted by the preprocessor cannot be suppressed with any pragma when compiling C++, they can only be suppressed by pragmas when compiling C. You're compiling as C++ (and shouldn't have tagged your question as C too). Here's a simple test case:
#pragma GCC diagnostic ignored "-Wcomment"
/* /* */
This warns in C++ mode, but not in C mode.
Given that pragmas just won't work, you should take some other approach. If you can modify the header, just change the comment. If you cannot change the header, you can mark the specific directory the header is in as a system header directory (use the -isystem command-line option).
You have not included your full cpp file. My guess is that an earlier include is already including some of the header files. Those header files will probably have header guards which prevent the header file being included more than once. And therefore the #pragma is not really doing anything.
You best bet is to move the #pragmas and include to the top of your header file before you include anything else. Remember you can also push and pop diagnostic pragmas.
Related
I'm working with a recent version of gcc, writing C++ code (but I would like an answer which would work for C as well). I have various extra warning flags enabled.
In my code, I am including a header from some popular library, #include <mylib.h>. Now, mylib.h triggers some of these compiler warnings; but I need to include it as-is, I can't alter it.
I want to suppress all compiler warnings when including mylib.h. I know I can suppress individual warnings with:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwarning-name-here"
#pragma GCC diagnostic ignored "-Wanother-warning-name-here"
// my code goes here
#pragma GCC diagnostic pop
but I want to suppress all of the warnings. Can I do that? Note that I'm willing to tweak the compiler command-line if that helps.
Note: This related question indicates that, in 2015, one could not do this using pragmas.
From GCC manual:
2.7 System Headers
The header files declaring interfaces to the operating system and
runtime libraries often cannot be written in strictly conforming C.
Therefore, GCC gives code found in system headers special treatment.
All warnings, other than those generated by `#warning' (see
Diagnostics), are suppressed while GCC is processing a system header.
Macros defined in a system header are immune to a few warnings
wherever they are expanded. This immunity is granted on an ad-hoc
basis, when we find that a warning generates lots of false positives
because of code in macros defined in system headers.
Normally, only the headers found in specific directories are
considered system headers. These directories are determined when GCC
is compiled. There are, however, two ways to make normal headers into
system headers.
The -isystem command line option adds its argument to the list of
directories to search for headers, just like -I. Any headers found in
that directory will be considered system headers.
All directories named by -isystem are searched after all directories
named by -I, no matter what their order was on the command line. If
the same directory is named by both -I and -isystem, the -I option is
ignored. GCC provides an informative message when this occurs if -v is
used.
There is also a directive, #pragma GCC system_header, which tells GCC
to consider the rest of the current include file a system header, no
matter where it was found. Code that comes before the `#pragma' in the
file will not be affected. #pragma GCC system_header has no effect in
the primary source file.
how to make this happen in CMake?
From CMake manual:
include_directories
Add include directories to the build.
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])
...
If the SYSTEM option is given, the compiler will be told the directories are meant as system include directories on some platforms. Signalling this setting might achieve effects such as the compiler skipping warnings, or these fixed-install system files not being considered in dependency calculations - see compiler docs.
Today I learned that most compilers offer static analysis like
SET (CMAKE_CXX_FLAGS "-std=c++1y -Werror -Weverything -Wno-documentation -Wno-c++98-compat -Wno-padded")
Which I think is really cool but I am also using some templated libraries which of course are header only.
Now I just can't use those flags anymore because those libraries are full of "errors/warnings". I could try to find all those flags and disable them but would really like to have those errors/warnings in my code.
Is there a workaround for this? I thought maybe I could try to find all template instantiations, compile the header file with no compiler warnings and then link the precompiled header with my code.
Do you think that could be possible?
Both gcc and clang support Diagnostic Pragmas which will allow you to ignore specific diagnostic messages for a section of code. For example to disable -Wunused-variable like so:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
// include headers with warnings
#pragma GCC diagnostic pop
this is very useful for third part libraries where you don't have control of the code.
I am familiar with warning suppressing pragmas for GCC and Keil (they are different, but the usage is pretty much the same).
For a third-party headers I can do something like this:
#pragma push
#pragma suppress warning
#include "whatever.h"
#pragma pop
But how can I suppress warnings from third-party sources? Both Eclipse+GCC and Keil generate them.
The only solution I came up is making whapper .c-file, which will include other .c files, which seems to be very dirty trick.
Are there any other solutions?
with gcc , while compiling you can use -w option to suppress warnings.
-w : Inhibit all warning messages.
Example:
gcc -w third_party_sourcefile.c
You may want to use -isystem instead of -Idir third party headers. See GCC manual.
If you're ok to edit third party source files, you can use #pragma GCC diagnostic ignored "-Wwarning-to-disable" see GCC manual.
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
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.
I have some Motif code that also uses widgets from the Xmt widget set.
It include both "Xm/XmStrDefs.h" and "Xmt/ComboBox.h".
However, there are some macros that are defined in both files:
// XmStrDefs.h:
#define XmNarrowSize "arrowSize"
// ComboBox.h:
#define XmNarrowSize "arrowSize"
These are system header files that I'm not allow to change. I've heard that gcc has the ability to ignore warnings in system headers.
How can I tell gcc that these headers are system headers?
Alternatively, how do I tell gcc to ignore redefinition errors?
Basically, how can I suppress this warning without changing these headers?
How can I tell gcc that these headers are system headers?
Use the -isystem switch. See http://gcc.gnu.org/onlinedocs/cpp/System-Headers.html for detail.
gcc -isystem Xm -I <rest of the nonsystem includes> ...