3

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In an ideal world, we should even trat all warning as errors (using -Werror). That's what I do whenever I can. It becomes hard when dealing with multiple platforms, but other than that, warning = error.
Yep, I do -Werror as well. My question was more for temporary code to aid debugging.
Too bad you can't do this in a block. Doing it for rest of file is too dangerous for me.
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.