Is there an gcc/Xcode pragma to suppress warnings?
-
19-09-2019 - |
Question
Is there a #pragma
to have gcc/Xcode suppress specific warnings, similar to Java's @SuppressWarning
annotation?
I compile with -Wall
as a rule, but there are some situations where I'd like to just ignore a specific warning (e.g. while writing some quick/dirty code just to help debug something).
I'm not looking for "fix the code" answers.
Solution
Here's a viable solution. Use #pragma GCC system_header
to let GCC handle your code in a very special way, thus suppressing any non fatal #warning
.
Remeber you're just fooling your preprocessor, not the real compiler. Suppressing warnings could be harmful most of times.
OTHER TIPS
In gcc4.6 and later you can use pragma's to suppress specific warnings and do that suppression only to a specific block of code, i.e. :
#pragma GCC diagnostic
push #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// Code that causes warning goes here
#pragma GCC diagnostic pop
The push/pop are used to preserve the diagnostic options that were in place before your code was processed.
This would be a much better approach than using "#pragma GCC system_header
" to suppress all warnings. (Of course, in older gcc you may be "stuck" with the #pragma GCC system_header
approach!)
Here's a nice reference on suppressing gcc warnings: http://www.dbp-consulting.com/tutorials/SuppressingGCCWarnings.html
This page also describes how to use -fdiagnostics-show-option
to find out what option controls a particular warning.
Of course, as others mention, it's generally far preferable to fix the root cause of all warnings than to suppress them! However, sometimes that is not possible.