Why static forward references

Chris Torek chris at mimsy.UUCP
Mon Dec 22 04:02:37 AEST 1986


In article <6927 at ki4pv.UUCP> tanner at ki4pv.UUCP (Tanner Andrews) writes:
> /*  start of example */
>extern char *blunge();			/* declare blunge */
>char *gork() { return(blunge(69)); }	/* use blunge */
>static char *blunge(arg) int	arg; { /* body of blunge here */ }
> /*  end of example */

>That "extern" just isn't needed.  Why not leave it off.

Leaving it out works correctly in all Unix compilers.  For that
matter, as far as I know, it works correctly in all IBM PC compilers,
and all Mac C compilers, and so on.

The problem, you see, is that some people want to write a compiler
that grabs the `extern' and generates a link directive:

		.extern	_blunge

They also want to write (or already have) a linker that will complain
if an explicit extern clashes with a later explicit definition:

	_blunge:

Why do they want to do this?  The main reason seems to be laziness.

The Unix compilers emit assembly, and simply omit the `extern's:

	_gork:	.globl	_gork
		...
		call	_blunge
		...
	_blunge:

When _blunge is defined in the same file, this is a conventional
forward reference.  If blunge() is moved away, the assembler just
assumes that _blunge is an external symbol.  If the assembler were
more demanding, the compiler could be educated a bit to do this:

	_gork:	.globl	_gork
		...
		call	_blunge
		...
		# end of source file
		.extern	_blunge
		# and any other as-yet-undefined-but-referenced
		# symbols; uses one bit per symbol table entry

Now, even if the compiler were to generate object code directly,
it could still do this---and even if the compiler were to generate
linkable files in which an `extern' must appear before a use, it
can still do this, even trivially, simply by generating the link
file, then generating the externs file, then concatenating the link
file onto the externs file.

But if the ANSI standard mandates that the code author must use
`extern' or `static' on forward declarations, these compiler writers
can save themselves the trouble of doing things right, or of fixing
their linkers.  And that appears to be the entire motivation.

(Do you detect a tone of disgust?  Then good, for this is indeed
disgusting.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list