Re: static declaration follows non-static one

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 06/29/2016 05:48 AM, Andrew Makhorin wrote:
On Tue, 2016-06-28 at 10:52 -0600, Martin Sebor wrote:
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?

Thank you for your response.


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.

In my example 'foo' has internal ("the same") linkage.

Kalle pointed me out TC1 that describes some changes in the Standard.
In fact, if the static declaration precedes extern one, no error is
reported.

Yes, that's valid because an extern declaration that follows
a visible static declaration of the same symbol gives the symbol
internal linkage.  The rule doesn't go the other way (as in the
original example) and so a static declaration cannot be followed
by an extern declaration in a strictly conforming program.

Martin



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux