🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2211

Re: "extern" and function declarations

#2211
From: Tanmoy Bhattacharya
Date: Fri, 08 Mar 1996 23:04
46 lines
2210 bytes
In article <4hmslm$b3l@solutions.solon.com>
Guillaume Laurent <glaurent@nissart.lagaude.ibm.com> writes:

GL: In article <4hdqjq$8pr@solutions.solon.com>
fred@genesis.demon.co.uk (Lawrence Kirby) writes: 
<snip>
GL: I've got a question here : what if the same above declaration is made
GL: in two (or more) files, compiled seperately then linked together ?
GL: 
GL: ie, in file a.c :
GL: 
GL: extern int i = 1;
GL: 
GL: ...
GL: 
GL: and in file b.c :
GL: 
GL: extern int i = 2;
GL: 
GL: If I understand correctly what you've said, both of these declarations
GL: will reserve space, and give i a value. My question is, what decides
GL: which value will actually be "used", and what happens to the "duplicate"
GL: space ?

It leads to undefined behaviour: the compiler/linker can do anything
it pleases. There must be exactly one definition for every object. 

GL: 
GL: >From what I've tried, the final effective value of i depends on the
GL: order of linking of the objects :
GL: 
GL: cc a.o b.o -o foo  ---> i=1
GL: cc b.o a.o -o foo  ---> i=2
GL: 
GL: But I'm still wondering about the "duplicate" space...

This is a `peculiarity' of your linker. There are linkers which will
complain about `duplicate symbols'

--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
Technical Details
Message-ID: <4hqee8$1n3@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4hdqjq$8pr@solutions.solon.com> <4hmslm$b3l@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
Newsgroups: comp.lang.c.moderated
Subject: Re: "extern" and function declarations
Date: 8 Mar 1996 17:04:40 -0600
Organization: Los Alamos National Laboratory
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hqee8$1n3@solutions.solon.com>
References: <4hdqjq$8pr@solutions.solon.com> <4hmslm$b3l@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com