Given the attached source file, GCC (powerpc-eabi-gcc.exe (Sourcery G++ Lite
4.4-79) 4.4.1), invoked as:
powerpc-eabi-gcc -te500v1 -mcall-sysv-noeabi -MMD -Wno-parentheses -c -o
external.c:3: warning: 'i' initialized and declared 'extern'
The relevant lines in the file are:
# if 1
static int i;
extern int i = 3;
The warning occurs irrespectively whether the # if 1 is changed to # if 0.
Standard C gives the above code a well defined meaning. The following
reference is from the C89 specification:
188.8.131.52, Linkage of Identifiers:
If the declaration of an identifier for an object or a function contains the
storage-class specifier <b>extern</b>, the identifier has the same linkage as
any visible declaration of the identifier with file scope. If there is no
visible declaration with file scope, the identifier has external linkage.
Thus, the primary meaning of "extern" for external definitions (that is,
definitions with file scope) is to use whatever linkage specification is
already in force. Only if no linkage specification has been specified
previously does "extern" mean assign external linkage.
This is in contrast with "static", which means assign internal linkage, and no
storage-class specifier, which means external linkage for objects (but means
the same thing as "extern" for functions, i.e., use existing linkage if
184.108.40.206 goes on to specify that only one type of linkage may be specified for a
n identifier in a translation unit.
Consider a code generation scenario using the C preprocessor (include
directives), where a generic code generation component is being employed.
Standard C allows a design where the (documented) definitions supplied by the
component are marked "extern", giving the user of the component the ability to
override the linkage by providing a tentative definition marked "static" before
including the component's header file.
The warning emitted by GCC undermines this design.
So what I'd like is for:
* this warning to be turned off by default
* (possibly) enable this warning with -Wextra
--- Comment #1 from joseph at codesourcery dot com <joseph at codesourcery dot com> 2010-10-12 11:32:10 UTC ---
This is a coding style warning - the code is valid, but extremely
unidiomatic for C since "extern" is generally expected to mean that the
declaration is not providing a definition of the object. Following static
by extern, though valid, is also a C feature of doubtful value.
--- Comment #2 from Konrad Schwarz <konrad.schwarz at siemens dot com> 2010-10-12 15:27:15 UTC ---
(In reply to comment #1)
> This is a coding style warning - the code is valid, but extremely
> unidiomatic for C since "extern" is generally expected to mean that the
> declaration is not providing a definition of the object. Following static
> by extern, though valid, is also a C feature of doubtful value.
I see the value of following static by extern -- the bug report provides an
example. To restate, using extern in a definition allows overriding an
object's or function's linkage, which can be useful in a translation unit
consisting of files a user can change and files a user cannot.
Whether or not this is idiomatic usage, or corresponds to what is generally
expected, is not sufficient grounds for a warning.
To the contrary, this warning promulgates incorrect assumptions about "extern".
At the very least, there must be a way of turning this warning off.
Thadeu Lima de Souza Cascardo <cascardo at holoscopio dot com> changed:
What |Removed |Added
CC| |cascardo at holoscopio dot
--- Comment #4 from Thadeu Lima de Souza Cascardo <cascardo at holoscopio dot com> 2011-05-09 03:49:25 UTC ---
What seemed really strange to me is that the warning would be emitted even when
the static declaration was inside #if 0/#endif pair, since the pre-processor
would have removed the code entirely.
And reading the warning also told me the problem was assigning a value in the
extern int i = 3;
is not fine. While
static int i = 3;
extern int i;
is perfectly OK. That is, without any warning on/off flags, there is no
warning. Should there be such a warning as Joseph says? -Wall -Wextra emits no
warning for static int i; extern int i; case. In fact, there is
-Wredundant-decls, but it only works if there is no initialization in the
I can turn off this warning using -w, but there is no particular flag for this
warning. Should one of the existing flags be used or a new one be created?
Better yet, should gcc not warn when this initialization happens in this
particular case, when the variable has already been declared as static?
Eric Gallager <egallager at gcc dot gnu.org> changed:
What |Removed |Added
CC| |egallager at gcc dot gnu.org
Summary|"warning: 'i' initialized |"warning: 'i' initialized
|and declared 'extern'" is |and declared 'extern'"
|spurious |could use a separate
| |warning flag controlling it
--- Comment #5 from Eric Gallager <egallager at gcc dot gnu.org> ---
Confirming on the basis that a separate warning flag here would be nice