suspicious error 'static declaration follows non-static declaration'
Tadeus Prastowo
tadeus.prastowo@unitn.it
Sun Feb 18 03:50:00 GMT 2018
Hi,
After perusing the draft of C99 [1], which is the closest to the
default -std in GCC 4.7.2 (I believe the default is C90), I think your
code does not compile because it has an undefined behavior, which
according to section 3.4.3 paragraph 2 can result in a compilation
error. To quote the said paragraph, possible undefined behavior
ranges from ignoring the situation completely with unpredictable
results, to behaving during translation or program execution in a
documented manner characteristic of the environment (with or without
the issuance of a diagnostic message), to terminating a translation or
execution (with the issuance of a diagnostic message). End quote.
The undefined behavior is due to function bar having both an external
linkage as per section 6.2.2 paragraph 4 and an internal linkage as
per section 6.2.2 paragraph 3. To quote section 6.2.2 paragraph 7,
if, within a translation unit, the same identifier appears with both
internal and external linkage, the behavior is undefined. End quote.
[1] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
--
Best regards,
Tadeus
On Fri, Feb 16, 2018 at 4:14 PM, Tadeus Prastowo
<tadeus.prastowo@unitn.it> wrote:
> Hi,
>
> I think you are referring to C89 standard. Newer version of GCC might
> have switched to newer version of the C standard. So, try to compile
> your code with -std=c89?
>
> --
> Best regards,
> Tadeus
>
>
> On Fri, Feb 16, 2018 at 3:41 PM, Andrew Makhorin <mao@gnu.org> 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.
>>
>> 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.
>>
>>
>> Thanks,
>>
>> Andrew Makhorin
>>
>>
More information about the Gcc-help
mailing list