4.2 extern a DISASTER (Flame)

Jim Kempf kempf at hplabsc.UUCP
Thu Jul 25 00:41:38 AEST 1985


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



More information about the Comp.lang.c mailing list