Re: extern
- From: Chris Torek <nospam@xxxxxxxxx>
- Date: 22 Jul 2005 00:27:27 GMT
>On Wed, 20 Jul 2005 13:39:13 +0100, in comp.lang.c , Lawrence Kirby
><lknews@xxxxxxxxxxxxxxx> wrote:
>>... both a.c and b.c contain a definition of a so linking them
>>produces undefined behaviour.
In article <hu90e1ho7cdo2681mniuh1rkvckpn6o02b@xxxxxxx>
Mark McIntyre <markmcintyre@xxxxxxxxxxx> wrote:
>Really? The standard makes it pretty clear that in the absence of a
>storage class specifier, a file-scope object is considered to be
>extern.
You are mixing up "linkage" -- which is indeed external -- with
"definition-ness". The extern keyword usually gives you external
linkage, and usually inhibits definitions. Without the keyword,
you still get external linkage, but the definition is not inhibited:
% grep var a.c b.c
a.c:int var;
b.c:extern int var;
Both have external linkage, but the "extern" in b.c inhibits
the definition, so that B.OBJ will contain a reference (in
compilers that use def/ref linkers).
Note that the "extern" keyword is pretty weak. If we modify b.c
to read, e.g.:
% grep var /dev/null b.c
b.c:static int var;
b.c:extern int var;
then the "var" in b.c has internal linkage (only), or if we modify
it to say "extern int var = 3;", it will be a definition (with
value 3), which will conflict with the definition (with value 0)
in A.OBJ using the system with the def/ref-model compiler. (Systems
that use "common model" and thus give the symbol "var" the type
"common block of size sizeof(int)" will see the Fortran COMMON
block -- this is where the name "common model" comes from -- in
a.o, and the data definition in b.o, and discard the COMMON-block
definition in favor of the data-definition, so again you will see
no conflict.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.
- Follow-Ups:
- Re: extern
- From: Mark McIntyre
- Re: extern
- References:
- extern
- From: DevarajA
- Re: extern
- From: Mark McIntyre
- Re: extern
- From: Lawrence Kirby
- Re: extern
- From: Mark McIntyre
- extern
- Prev by Date: Re: World Smallest Program
- Next by Date: Re: The object status of function call returns
- Previous by thread: Re: extern
- Next by thread: Re: extern
- Index(es):
Relevant Pages
|