4.2 extern a DISASTER

Jim Kempf kempf at hplabsc.UUCP
Tue Jul 30 12:43:20 AEST 1985


Many thanks to the people who responded to the original basenote.
I apologize for the flame-like tone of the note, but I had
just spent a weekend fighting with the problem. It may, in fact,
turn out to be a bug in ld, as was suggested.

However, in reference to those who either don't believe that
the 4.2 compiler uses two methods of dealing with externs
or believe that things have always been done that way,
I'd like to offer the following quote from "A C Reference
Manual", by Samuel P. Harbison and Guy L. Steele, Prentice Hall,
1984:

on pg. 81:

"It is a well-known deficiency in C that defining and referencing
occurences of external variable declarations are difficult to
distinguish. In general, compilers use one of three complicated
schemes to determine when a top level declaration is a defining
occurence."

>>>>>>> here follows a discussion of the three alternatives <<<<<<<<<<

Roughly they are:

1) Any referencing declaration must include extern, one and
only one may have no extern and that is the defining one.

2) Referencing and defining declarations are put into a FORTRAN
like COMMON. Only one may have an initialization.

3) An initializer on the declaration signals a defining occurence.

on pg. 82:

"The Berkeley 4.2BSD UNIX C compiler for the VAX has a mixed
strategy:

1. If extern is omitted, and an initializer is present, a 
definition for the symbol is emitted. Having two or more such
definitions among all the files comprising a program results in
an error at link time or before.

2. If extern is omitted, and no initializer is present, a "common"
definition (a la FORTRAN) is emitted. Any number of "common"
declarations of the same identifier may coexist(!!!!-!mine)

3. If extern is present, the declaration is taken to be a
reference to a name defined elsewhere. It is illegal for such
a declaration to have an initializer. If the identifier so declared
is never actually used, the compiler will not issue an external
reference to the linker"

Presuming H & S are right, it seems as if features of so-called
"dying" languages like FORTRAN have indeed crept into a
"progressive" language like C. Maybe FORTRAN file record structure
should be next? If Classic Coke is a success, maybe FORTRAN
should be repackaged as Classic C?  ( :-) ).

Seriously, the ANSI standards document seems to be perpetuating
the problem rather than getting to the bottom of it-namely 
defining a syntatic construct which will allow the compiler
to more easily distinguish between defining and referencing
occurences for externs. At least, that was the impression
I got from glancing through the material on externs (which
is meager at best, and scattered about the document in
various places).

At any rate, I hope this clears up any questions about my
initial basenote. 

	jim kempf kempf at hplabs



More information about the Comp.unix mailing list