Proper use of "extern" ...

brett at wjvax.UUCP brett at wjvax.UUCP
Sat Jun 14 02:41:05 AEST 1986


In article <132 at danews.UUCP> lvc at danews.UUCP (Larry Cipriani) writes:
>My understanding of extern is that it simply declares the
>object in question...
>
>I often put ALL of the declarations in one header file, and include
>it in all the other files.  This is a matter of style, and I recognize
>its weaknesses, but I like it better than all the alternatives.  By the
>way, extra, redundant, or unnecessary externs are allowed and don't matter,
>at least on my C compiler.

Harbison & Steele say only that this property is true of the 4.2bsd C
compiler.  Speaking of its treatment of extern, they say:

	"If the identifier so declared is never actually used, the compiler
	will not issue an external reference to the linker" (H & S, p. 82)

I find this feature very useful, since I often generate a library which one
associated include file that contains external references to all the routines
therein.  This frees the user of my library from having to declare all the
routines himself (at least those that don't return in).  This is a minor
advantage.  A MAJOR advantage is that it also lets me transparently convert
some routines to macros, or back again.  If the user had declared them, I
could not convert a routine to a macro without REMOVING all the user's
declarations.

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,
no matter how many routines he actually used.  This would be annoying.

My question is -- how general is this feature?  Is it true of AT&T SYSV?  Is it
true of some of the common PC C compilers?  Other C compilers?

-------------
Brett Galloway
{pesnta,twg,ios,qubix,turtlevax,tymix,vecpyr,certes,isi}!wjvax!brett



More information about the Comp.lang.c mailing list