Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 7 Sep 1995 18:47:38 -0700 (PDT)
From:      Archie Cobbs <archie@tribe.com>
To:        questions@freebsd.org
Subject:   Re: multiple variable definitions in object files
Message-ID:  <199509080147.SAA00274@bubba.tribe.com>

next in thread | raw e-mail | index | archive | help

Hello again.

I sent the mail below a few days ago and got no response.

I can't tell if that means nobody has an answer or if the answer is
so trivial that the question shouldn't have been asked in the first
place.

Sorry to pester, but for some reason I'm really curious...

Thanks,
-Archie

p.s. Quoting from K & R, 2nd edition (ANSI C), p. 81, paragraph 2:

  "There must be only one _definition_ of an external variable
   among all the files that make up the source program; other
   files may contain 'extern' declarations to access it."


> Can someone explain why the linker allows multiple definitions of
> a variable to be linked together without complaining or warning?
> 
> I found this surprising... I thought this was a no-no. At least it
> has always caused errors in the past it seems.
> 
> I didn't see any relevant info in the ld(1) man page.
> 
> For example:
> 
> ======== foo.c ============
> 
>     int global;
> 
>     main()
>     {
>      global = 2;
>      printf("foo: global = %d\n", global);
>      bar();
>     }
> 
> ======== bar.c ============
> 
>     float global;
> 
>     bar()
>     {
>      printf("bar: global = %f\n", global);
>     }
> 
> ======== compile with ==========
> 
>     $ cc -o foobar foo.c bar.c
>     $ foobar
>     foo: global = 2
>     bar: global = 0.000000
>     $

_______________________________________________________________________________
Archie L. Cobbs, archie@tribe.com  *  Tribe Computer Works http://www.tribe.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199509080147.SAA00274>