Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable all warnings using pragma directives in GCC

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)?

like image 498
Fr0stBit Avatar asked Aug 17 '15 11:08

Fr0stBit


People also ask

How do I turn off warnings in GCC?

-w is the GCC-wide option to disable warning messages.

How do I stop compiler warnings?

Choose Build, and go to the Errors and warnings subsection. In the Suppress warnings or Suppress specific warnings box, specify the error codes of the warnings that you want to suppress, separated by semicolons. For a list and descriptions of warning codes, see C# Compiler Messages.

How do you suppress warnings in C++?

To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.

How do I ignore a warning in Makefile?

Maybe you can look for CFLAGS options in Makefile and remove the -Werror flag. The Werror flag will make all warnings into errors. Show activity on this post.


1 Answers

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.

like image 161
Heavy Avatar answered Oct 24 '22 00:10

Heavy