[net.unix] 4.2 extern a DISASTER

kempf@hplabsc.UUCP (Jim Kempf) (07/30/85)

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@hplabs

chris@umcp-cs.UUCP (Chris Torek) (08/01/85)

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

This means that any number of these declarations may be given---in
other words, "ld" won't complain.  However, all such declarations
*refer to the same storage area*.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland

henry@utzoo.UUCP (Henry Spencer) (08/02/85)

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

We do not want the ANSI committee being inventive; we want them to
standardize the language we've got, not invent a new one!  One can
argue that they have already gone too far in this direction.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry