28

I am seeking for a way to suppress all possible warnings that i may get with Gcc with pragma directives. I had made some guard macros that help me silence 3rd party headers from warnings, and for now they work like charm for msvc and clang. I am still missing the correct way to use Gcc diagnostic pragmas in order to suppress every warning in a section. Let me give you some examples:

In msvc we can do this:

#pragma warning(push, 0) // Code that produces warnings... #pragma warning(pop) 

And in clang we can do this:

#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wall" #pragma clang diagnostic ignored "-Wextra" // Code that produces warnings... #pragma clang diagnostic pop 

And the code that is in the middle is now being silenced from warnings for good.

And in Gcc we also have similar pragma directives with clang and i thought i could try something like this:

#pragma GCC diagnostic push #pramga GCC diagnostic ignored "-Wall" #pragma GCC diagnostic ignored "-Wextra" // Code that produces warnings... #pramga GCC diagnostic pop 

But passing -Wall and -Wextra in diagnostic ignored pragma in GCC does not work like clang, and does not disable all the possible warnings. Instead of this passing a specific warning to disable works:

#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" void foo (int x) // No longer getting "unused parameter 'x'" warning { } #pragma GCC diagnostic pop 

So the only workaround i can think so far is to make a long list with all the GCC warning flags and use them like above. Is there a more elegant solution? If not where i can get the complete Gcc warning flag list (favorably in a plain list)?

8
  • Why why why why why why why do you want to disable warnings?? :( :( Commented Aug 17, 2015 at 11:51
  • 43
    Because my code is warning free and 3rd party headers as i said sometimes contain warnings. Commented Aug 17, 2015 at 12:04
  • 1
    "System" headers are not supposed to have their diagnostics leak, for this very reason. If you're pulling in their include path manually with -I, use -isystem instead. Problem solved. :) Then you can turn your warnings back on (plus -Werror!) to catch bugs you introduce into your code in future. Your code may be warning free now but the entire purpose of warnings is to catch when you break it in the future... Commented Aug 17, 2015 at 12:47
  • 1
    There's a command line option -w (specifically lowercase w; -W does something else) that disables all warnings, but it does not seem to be accepted as an argument to #pragma GCC diagnostic, sorry. Commented Nov 21, 2017 at 18:09
  • 2
    I'm having exactly the same issue right now. As this question is already a few years old (and back then the answer seemed to be "not possible"), did anything change in the meantime? Commented Jan 28, 2021 at 10:07

1 Answer 1

2

Documentation says:

At the moment only warnings (normally controlled by ‘-W...’) can be controlled, and not all of them. Use -fdiagnostics-show-option to determine which diagnostics are controllable and which option controls them.

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

5 Comments

How do i use this flag? Can i pass it to gcc somehow to get a list of the options?
Yes, you build your files with this option, and it adds something like [-Wunused-variable] to each warning.
But it still is not helpful, as my purpose is either to find a way to suppress all warnings with some clever directive, or gather a list with all the controlled warnings.
@TheArtist: Something wrong with the GCC documentation? It lists all the warning switches quite clearly and thoroughly...
When i say all the controlled warnings i do no mean the specific translation unit, but ALL the controlled warnings that Gcc offers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.