Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Why static forward references Message-ID: <4870@mimsy.UUCP> Date: Sun, 21-Dec-86 13:02:37 EST Article-I.D.: mimsy.4870 Posted: Sun Dec 21 13:02:37 1986 Date-Received: Sun, 21-Dec-86 21:04:39 EST References: <6927@ki4pv.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 64 In article <6927@ki4pv.UUCP> tanner@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@mimsy.umd.edu