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
- Q1: Is there a way to specify several options inline?
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
#pragmaas 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 --
pushthe settings, disable warning, include header,popthe settings. This leads to constructs likeHowever, 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.