From owner-freebsd-questions Thu Sep 7 18:48:11 1995 Return-Path: questions-owner Received: (from majordom@localhost) by freefall.freebsd.org (8.6.12/8.6.6) id SAA16353 for questions-outgoing; Thu, 7 Sep 1995 18:48:11 -0700 Received: from bubba.tribe.com ([205.184.207.7]) by freefall.freebsd.org (8.6.12/8.6.6) with ESMTP id SAA16347 for ; Thu, 7 Sep 1995 18:48:09 -0700 Received: (from archie@localhost) by bubba.tribe.com (8.6.11/8.6.9) id SAA00274 for questions@freebsd.org; Thu, 7 Sep 1995 18:47:38 -0700 From: Archie Cobbs Message-Id: <199509080147.SAA00274@bubba.tribe.com> Subject: Re: multiple variable definitions in object files To: questions@freebsd.org Date: Thu, 7 Sep 1995 18:47:38 -0700 (PDT) X-Mailer: ELM [version 2.4 PL24] Content-Type: text Content-Length: 1486 Sender: questions-owner@freebsd.org Precedence: bulk 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