suspicious error 'static declaration follows non-static declaration'
Xi Ruoyao
ryxi@stu.xidian.edu.cn
Fri Feb 16 16:17:00 GMT 2018
On 2018-02-16 17:41 +0300, Andrew Makhorin wrote:
> Hello,
>
> On compiling the following code with gcc (Debian 4.7.2-5) 4.7.2:
>
> Â Â Â int foo(int x, int y)
> Â Â Â {
> Â Â Â Â Â Â Â Â Â extern int bar();
> Â Â Â Â Â Â Â Â Â return bar(x, y);
> Â Â Â }
>
> Â Â Â static int bar(int x, int y)
> Â Â Â {
> Â Â Â Â Â Â Â Â Â return x + y;
> Â Â Â }
>
> I get the following error:
>
> mao@corvax:~/Desktop$ gcc -c test.c
> test.c:7:15: error: static declaration of 'bar' follows non-static
> declaration
> test.c:3:21: note: previous declaration of 'bar' was here
>
> However, the Standard says [6.1.2.2 Linkages of identifiers]:
>
> Â Â Â 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.
The declaration should be visible at the point of new `extern` declaration.
> There is no other way to declare 'bar' as static in block scope,
> because it is a function, so this error confuses me. Could anyone
> explain this? Should it be a warning rather than error? I'd like to note
> that my code is compiled successfully with older versions of gcc and
> with many other C compilers.
Yes you can declare it earlier:
+ static int bar(int, int);
   int foo(int x, int y)
   {
         extern int bar();
         return bar(x, y);
   }
   static int bar(int x, int y)
   {
         return x + y;
   }
--
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University
More information about the Gcc-help
mailing list