Re: Why error "variable previously declared ‘static’ redeclared ‘extern’"?

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

 



Sorry for the repost, Thunderbird decided (or I have found a keyboard shortcut) to remove all your text, so my post had no context!

On 07/07/14 14:04, A A wrote:
The following code gives me an error when trying to build with gcc-4.8:
Good

#include<stdio.h>
#include<stdlib.h>

static int i5 = 30;
There will be "global" i5 int somewhere in the program's memory BUT
the linker will not let (as it cannot see) this i5 symbol from any other object file. That is it has "internal linkage". Yes it says "static" which is an odd choice of keyword
but it does not mean static for all to access, it means internal linkage.
Read Denis Ritchie's famous book, it talks about symbols and linkage.

int main(void)
{
     int i5 = 12;
     printf("i5: %d\n", i5);
     {
         extern int i5;
Why did you write this line? What are you trying to do? What this says is
"make the linkage of i5 external" meaning other objects can have i5 linked into
them. BUT you've already said i5 is internal, thus a contradiction.
         printf("i5: %d\n", i5);
     }

     printf("i5: %d\n", i5);
     exit(0);
}

main.c:11:14: error: variable previously declared ‘static’ redeclared ‘extern’
Exactly. This error is one of the most useful I've seen from GCC in a while
(I get a lot of template onces.... they are long and cryptic)
Look up linkage

However, it compiles with clang 3.0. I don't understand what does GCC
Report the failure to them.
want to tell me with this message. As far I know, `extern' must refer
to a variable defined with an internal or external linkage so it this
case it must refer to static i5, not i5 defined in the first line of
main(). I looked into C standard section 6.2.2 but found nothing that
would justify this error message. I know this is GCC mailing group so
don't take it personally but my current guess is that GCC is simply
overzealous and less smart than Clang, is that right?
Have you heard of "Stackoverflow.com" I think it is, they can help
people like you. I also think you should read http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf cover to cover.

Two last questions, spawned by my curiosity:

Why did you put that extern inside the function body? Are you trying to redefine symbols? The name should really be unique, the name is what identifies a symbol. Look at the symbol tables from objdump, in C code they're not mangled, in C++ they are so functions that have the same name
but take different arguments have UNIQUE link symbols.

To learn a language you should really try and make actual useful things in it that do a task, not one file bits of tat that tests how pedantic a compiler is, and how good the writers of it are to catch the most obscure error only a select few will ever cause.

Alec




[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