c++ - valid - How do I disable a gcc warning which has no command line switch?
gcc warnings (3)
There're hundreds of instances of this warning and they flood the compiler output. Hard to ignore.
If this is external library there is way to reduce this warning to single warring report. I'm suspecting that you can live with single warning message.
Wrap this library API with your own functions/methods. You can name them 1:1 using different namespace to avoid complex modification of own code where this API is used. This way this warning will be reported only when source including problematic header file is included. Aim is to include problematic header file only once.
Depending how this API looks like it may be harder to do.
Anyway if this is third party library then this approach will make it easier to mock that library and write test for your code.
I get the following warning:
warning: 'X' is initialized and declared 'extern'
and it looks like it's no big deal and I could disable it. Changing the code is not really a good idea in my case because I have no control over the code, I just need to compile it. So I want to disable the warning.
If it had a
key next to it then I could use a
command line switch but it looks like there's no distinct switch for this warning.
How do I disable such warning?
For a direct answer to the posed question:
As can be seen from GCC's source code, there is no (semi-)specific switch to disable this warning. It seems to only be disabled by disabling all warning (
) or including the offending code as system header via
, both of which are non-specific in the suppressed warnings.
If you don't want to use the two non-specific suppression mechanisms, then you probably will have to patch in an additional flag to GCC or wait for the bugs to be worked on in order to disable this specific warning.
The code producing this warning is
not valid C
and should be fixed.
The C language requires issuing "diagnostics" (warnings or errors) for constraint violations
such as this
, and does not mandate the existence of any way to disable them. I believed (and I suspect many others do) that
was a constraint violation with an initializer, since in normal usage
only provides a declaration, not a definition. However, per 6.9.2 ¶1:
If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier.
Being that the grammar seems to allow
with an initializer, this is indeed a valid definition.
GCC has a
option that will probably shut the warning off, but at the expense of disabling all warnings with no ability to override and turn some back on. This would make sense if it were a constraint violation and would be a sign to fix the invalid code; however, the code is valid and GCC absolutely should be providing a mechanism not to produce a spurious warning about it.
As noted by Eljay in a comment:
Warnings come in several categories: by the standard required diagnostic message; lint-like static analysis of common accidental language abuse/misuse; well-meaning but still stylistic opinion (e.g.,
-Weffc++); possibly too pedantic and/or minutia (e.g.
-pedantic). The latter categories ought to have "opt-out" ways to disable the specific warning, such as in the OP's case.
GCC generally tries to honor this ought , most of the time, and I think the absence of a way to disable this one would be worth reporting to the GCC bug tracker .