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