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 extern
was a constraint violation with an initializer, since in normal usage extern
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 extern
with an initializer, this is indeed a valid definition.
GCC has a -w
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. -Weverything
or -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.