public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* extern question
@ 2006-06-06 13:50 Trevis Rothwell
  2006-06-06 14:00 ` John Love-Jensen
  2006-06-06 14:31 ` Andrew Haley
  0 siblings, 2 replies; 6+ messages in thread
From: Trevis Rothwell @ 2006-06-06 13:50 UTC (permalink / raw)
  To: gcc-help

Given the files:

/* main.c */
#include <stdio.h>

extern void foo (void);
extern void bar (void);

extern int larry;

int main (void) {
  printf ("inside main: %d\n", larry);
  foo();
  bar();

  return 0;
}

/* foo.c */
int larry = 42;

extern void foo (void) {
  printf ("inside foo: %d\n", larry);
  return;
}

/* bar.c */
int larry;

extern void bar (void) {
  printf ("inside bar: %d\n", larry);
  return;
}

...

Compiling and linking all of these together generates a program which
gives the following output:

inside main: 42
inside foo: 42
inside bar: 42

...

If I add a variable definition to bar.c (so that "larry" is given a
value in both foo.c and bar.c) then the compiler complains that the
variable is defined more than once, which is true.  But, my question
is:  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?

Thanks!

 -- Trevis Rothwell

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: extern question
  2006-06-06 13:50 extern question Trevis Rothwell
@ 2006-06-06 14:00 ` John Love-Jensen
  2006-06-06 14:05   ` John Love-Jensen
  2006-06-06 14:08   ` Aseem Rastogi
  2006-06-06 14:31 ` Andrew Haley
  1 sibling, 2 replies; 6+ messages in thread
From: John Love-Jensen @ 2006-06-06 14:00 UTC (permalink / raw)
  To: Trevis Rothwell, MSX to GCC

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: extern question
  2006-06-06 14:00 ` John Love-Jensen
@ 2006-06-06 14:05   ` John Love-Jensen
  2006-06-06 14:08   ` Aseem Rastogi
  1 sibling, 0 replies; 6+ messages in thread
From: John Love-Jensen @ 2006-06-06 14:05 UTC (permalink / raw)
  To: Trevis Rothwell, MSX to GCC

Correction:
You can only define larry once.  You've defined it in foo.c.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: extern question
  2006-06-06 14:00 ` John Love-Jensen
  2006-06-06 14:05   ` John Love-Jensen
@ 2006-06-06 14:08   ` Aseem Rastogi
  2006-06-06 14:12     ` John Love-Jensen
  1 sibling, 1 reply; 6+ messages in thread
From: Aseem Rastogi @ 2006-06-06 14:08 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: Trevis Rothwell, MSX to GCC

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.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: extern question
  2006-06-06 14:08   ` Aseem Rastogi
@ 2006-06-06 14:12     ` John Love-Jensen
  0 siblings, 0 replies; 6+ messages in thread
From: John Love-Jensen @ 2006-06-06 14:12 UTC (permalink / raw)
  To: Aseem Rastogi; +Cc: Trevis Rothwell, MSX to GCC

Hi Aseem,

> 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).

C++ has different rules than C about what constitutes a declaration versus a
definition.

(At least C89.  I'm not familiar with C99, which may have slightly different
rules.)

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: extern question
  2006-06-06 13:50 extern question Trevis Rothwell
  2006-06-06 14:00 ` John Love-Jensen
@ 2006-06-06 14:31 ` Andrew Haley
  1 sibling, 0 replies; 6+ messages in thread
From: Andrew Haley @ 2006-06-06 14:31 UTC (permalink / raw)
  To: Trevis Rothwell; +Cc: gcc-help

Trevis Rothwell writes:
 > Given the files:
 > 
 > /* main.c */
 > #include <stdio.h>
 > 
 > extern void foo (void);
 > extern void bar (void);
 > 
 > extern int larry;
 > 
 > int main (void) {
 >   printf ("inside main: %d\n", larry);
 >   foo();
 >   bar();
 > 
 >   return 0;
 > }
 > 
 > /* foo.c */
 > int larry = 42;
 > 
 > extern void foo (void) {
 >   printf ("inside foo: %d\n", larry);
 >   return;
 > }
 > 
 > /* bar.c */
 > int larry;
 > 
 > extern void bar (void) {
 >   printf ("inside bar: %d\n", larry);
 >   return;
 > }
 > 
 > ...
 > 
 > Compiling and linking all of these together generates a program which
 > gives the following output:
 > 
 > inside main: 42
 > inside foo: 42
 > inside bar: 42
 > 
 > ...
 > 
 > If I add a variable definition to bar.c (so that "larry" is given a
 > value in both foo.c and bar.c) then the compiler complains that the
 > variable is defined more than once, which is true.  But, my question
 > is:  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?

This is Section J.5.11 of ISO C. _Common extensions_:

"There may be more than one external definition for the identifier of
an object, with or without the use of extern ... if the definitions
disgree, or more than one is initialized, the behavior is undefined."

Andrew.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-06-06 14:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-06 13:50 extern question Trevis Rothwell
2006-06-06 14:00 ` John Love-Jensen
2006-06-06 14:05   ` John Love-Jensen
2006-06-06 14:08   ` Aseem Rastogi
2006-06-06 14:12     ` John Love-Jensen
2006-06-06 14:31 ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).