Proper use of "extern" ...

Chris Torek chris at umcp-cs.UUCP
Tue Jun 17 04:52:44 AEST 1986


>In article <132 at danews.UUCP> lvc at danews.UUCP (Larry Cipriani) writes:
>>... extra, redundant, or unnecessary externs are allowed and don't
>>matter, at least on my C compiler.

In article <714 at wjvax.wjvax.UUCP> brett at wjvax.UUCP (Brett Galloway) writes:
>Harbison & Steele say only that this property is true of the 4.2bsd C
>compiler. ...
>
>	"If the identifier so declared is never actually used, the compiler
>	will not issue an external reference to the linker" (H & S, p. 82)
>
>... However, if the above feature is not present, each external
>reference in the include file causes the subroutine to be linked in,
>with the result that anybody that uses my include file and my library
>gets the ENTIRE library linked in ....
>
>... how general is this feature?  ... Is it true of ... Other
>C compilers?

I submit that any compiler that does not have this `feature' is,
if not broken, at least being rather dumb.  Consider: all a compiler
need do is note at an `extern' declaration that the variable or
function is presently unused, but semantically valid.  At the first
actual reference to the variable, the compiler can generate whatever
linker commands (if any) are required to make the variable or
function available at run-time.

In this manner, a C source file consisting entirely of `extern'
declarations:

	extern int i;
	extern double d;
	extern char *cf();

would generate no code whatsoever.

For some reason, not all compiler writers seem to have realised
this; in such cases, I grit my teeth and fiddle with the source
code, but also complain vociferously.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list