How do we enable or disable specific compiler warnings or errors?
For gcc/g++ and clang compilers, try:
# To disable -Werror just for warning -Wwhatever
-Wno-error=whatever
# To **enable** errors just for this one -Wwhatever warning
-Werror=whatever
For the clang compiler, you'd use -Wno-error=macro-redefined, to disable that error for the -Wmacro-redefined warning only, and you'd use -Werror=macro-redefined to enable an error only for that warning.
See here: https://clang.llvm.org/docs/DiagnosticsReference.html#wmacro-redefined. Thanks for the comment under the question, @user3386109!
See the list of all possible warnings and errors here:
- For the gcc compiler: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
- For the clang compiler (which is gcc-compatible, by design): https://clang.llvm.org/docs/DiagnosticsReference.html
For example, from the gcc link above:
-Werror=
Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.
The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)
Note that specifying -Werror=foo automatically implies -Wfoo. However, -Wno-error=foo does not imply anything.
Other References:
- The comment by @HolyBlackCat
- [my Q&A] How can I disable a C/C++
-Werror build error in Bazel? (AKA: how to turn OFF specific warnings already turned on by -Wall -Werror)
Related:
- In case you ever need to control just a few lines of code, this is incredibly useful too: How to disable GCC warnings for a few lines of code