Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/17/84; site hplabsc.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!harvard!talcott!panda!genrad!decvax!tektronix!hplabsc!kempf From: kempf@hplabsc.UUCP (Jim Kempf) Newsgroups: net.lang.c Subject: Re: 4.2 extern a DISASTER (Flame) Message-ID: <2656@hplabsc.UUCP> Date: Wed, 24-Jul-85 10:41:38 EDT Article-I.D.: hplabsc.2656 Posted: Wed Jul 24 10:41:38 1985 Date-Received: Sun, 28-Jul-85 05:20:29 EDT References: <2643@hplabsc.UUCP> <1281@eagle.UUCP> <1282@eagle.UUCP> Distribution: net Organization: Hewlett Packard Labs, Palo Alto CA Lines: 69 This is where the problem is occuring: file1.c int foo; file2.c int uses_foo() { extern int foo; ... } According to the K&R standard, any references to foo within uses_foo() should refer to the external variable in file1.c, and the linker should take care of making the connection (unless, of course, there is a static within file2.c's file scope which masks the external definition). Not according to the 4.2 compiler, however. If I try to run this, I get various bus errors, etc. (it links OK). Furthermore, according to Haberson & Steele (and I've verified this by using the verbose mode of ld), if one defines foo as above, a FORTRAN like COMMON block is allocated for foo, and one could declare foo in file2.c *without* declaring the storage class static and *without* declaring foo extern and the linker will *not* make the connection between the two. The only way to get foo declared as a true external is to include an initialization: file1.c int foo=0; file2.c extern int foo; uses_foo() { ... } This will work. Notice that I did NOT declare foo external within the scope of uses_foo(). This, also, does NOT work with the 4.2 compiler. I find this particular point an abomination, since I like to declare all the externs that I use in a particular function at the top of the function, where the locals are declared. This makes it easy for someone reading the program to simply glance at the function header to see how a particular variable is used, rather than having to page backward several pages through the listing for the whole file. My conclusion from all this is that the 4.2 compiler has taken something which was fairly easy and intuitive to use and made it so complicated that it is no longer useful. Unfortunately, the ANSI standard doesn't look like much of an improvement. I do have their Feb. document, and they essentially support the "two external storage classes" model-FORTRAN like COMMON if no initialization is given, and external definition if the variable is initialized. jim kempf hplabs!kempf