In files being compiled using gcc (several .c and .cpp files) I have currently something like:
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic warning "-Wall"
#endif
The purpose of specifying inline options instead of just using the command line is to control after which points I'm interesting in going more strict (e.g., after the #include
list).
It works fine. I then tried to add -Wextra
with:
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic warning "-Wall -Wextra"
#endif
It fails. Apparently, you can't use more than one option in a single pragma
, and you have to use several lines instead:
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic warning "-Wall"
#pragma GCC diagnostic warning "-Wextra"
#endif
Next, I tried to add -Werror
and -pedantic
, and neither using diagnostic warning
nor diagnostic error
works. Example:
#pragma GCC diagnostic error "-Werror"
fails with: warning: ‘-Werror’ is not an option that controls warnings
. How can I solve this? which other alternatives there's besides diagnostic [warning|error]
? What about -pedantic
or -pedantic-errors
? Because I'm aware pedantic is mostly related with standard compliance rather than diagnostic control and I don't know if I can even specify it inline.
Well, you can't. It's just how it is.
You can turn specific warnings on or off, or turn them into errors, but you can't do everything with #pragma
as you have noticed. Note that "-Wpedantic"
can still be controlled.
In general, I still put most options into the build file, including -Werror
. I don't think it's such a great idea to keep switching around warnings for sections of code.
In the (rare-ish) case that some header gives warnings, I use pragmas to disable them for that header -- push
the settings, disable warning, include header, pop
the settings. This leads to constructs like
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wshadow"
#endif
#include "External/ProtoPuf/message.h"
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
However, I don't use stuff like this for my own code -- instead, I fix the warnings in the code, or remove the warning from the build system if it's too annoying.