Re: extern



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



Relevant Pages

  • Re: Hello,Where is declared the "end" var present in arch/arm/boot/compressed/misc.c ?
    ... > extern int end; ... > I'd like to know where this var comes from. ... Since it's extern, I guess ... (assuming you're at the top of the source tree when start this) ...
    (comp.os.linux.misc)
  • Re: Pointer to extern char?
    ... How can there be a pointer to extern char? ... that the function has external linkage? ... static int a, *b, c; ... linkage when a declaration occurs at file scope, ...
    (comp.lang.c)
  • Re: how to use the keyword extern in c?
    ... > The extern keyword specifies that the identifier being declared has ... > external linkage. ... > includes) and referred to by name from code in other translation ... what's the difference when the keyword "extern" appears and when it doest appear? ...
    (comp.lang.c)
  • Re: (Why) Cant char* template parameters be const?
    ... >> Not sure why template parameters must have external linkage though. ... > Imagine you have two source files. ... When I made it extern, ...
    (comp.lang.cpp)
  • Re: extern
    ... >> 6.2.2p4 fully defines the meaning of the extern keyword, ... >> declaration with the extern keyword does not reverse the original ... Declared extern usually implies external linkage, ...
    (comp.lang.c)