extern question

Aseem Rastogi aseem@india.tejasnetworks.com
Tue Jun 6 14:08:00 GMT 2006


interestingly (or at least i think so :-) ), the code snippet that 
Trevis has given compiles and links using gcc BUT fails to link under 
g++ (2 times defined problem). being a C++ programmer, this is what i 
expected but was surprised to see that gcc compiles such a code.

i investigated a bit. if we just do 'gcc -c bar.c' and then 'nm bar.o', 
then larry is shown as "C" symbol which means common (weak) symbol. 
whereas if we do 'g++ -c bar.c' and then 'nm bar.o', then larry is shown 
as "B" symbol which is global initialized data.

could it have something to do with the fact that global variables are 
automaically initialized to their default values in C++ ??

John Love-Jensen wrote:

>Hi Trevis,
>
>>should I even be allowed to declare "larry" in both foo.c and
>>bar.c ?  If so, why is that not seen as a problem?
>>
>
>You can declare larry as many times as you want.  You've declared it in
>main.c and in bar.c.
>
>You can only define larry once.  You've defined it in bar.c.
>
>If you change the declaration of larry in bar.c to a definition, then you've
>defined it too many times (i.e., more than once).
>
>HTH,
>--Eljay
>
>


-- 
Nothing will work unless u do.





More information about the Gcc-help mailing list