static declaration follows non-static one

Martin Sebor msebor@gmail.com
Tue Jun 28 16:52:00 GMT 2016


On 06/27/2016 10:06 PM, Andrew Makhorin wrote:
> Hello,
>
> On compiling the following code with gcc (Debian 4.7.2-5) 4.7.2
>
> 	#include <stdio.h>
>
> 	extern int foo(void);
>
> 	static int foo(void) { return 271828; }
>
> 	int main(void)
> 	{
> 	      printf("foo = %d\n", foo());
> 	      return 0;
> 	}
>
> the compiler reports the error:
>
> 	foo.c:5:12: error: static declaration of ‘foo’ follows non-static
> declaration
> 	foo.c:3:12: note: previous declaration of ‘foo’ was here
>
> However, the Standard says:
>
> 	If the declaration of an identifier for an object or a function
> 	contains the storage-class specifier extern, the identifier has
> 	the same linkage as any visible declaration of the identifier
> 	with file scope.
>
> Is it a gcc bug or I misunderstand something?

Paragraph 7 of the same section (Linkage of Identifiers) should
help explain the error:

   If, within a translation unit, the same identifier appears with
   both internal and external linkage, the behavior is undefined.

Unlike the text quoted above, this paragraph hasn't changed between
C89 and the current C11.

GCC is strict in this case and diagnoses the undefined behavior
by rejecting the program.  (It seems that C could and probably
should require an error here.)

Martin



More information about the Gcc-help mailing list