[comp.lang.c] C linkage query

ark@alice.UUCP (Andrew Koenig) (06/08/89)

Does anyone have a C compiler + linker incapable of handling this:

	extern int foo;

	main() { }

That is: are there any C implementations that turn an
`extern' declaration for a variable that is never actually
referenced into a genuine external reference that inhibits
linking?
-- 
				--Andrew Koenig
				  ark@europa.att.com

spl@mcnc.org (Steve Lamont) (06/08/89)

In article <9449@alice.UUCP< ark@alice.UUCP (Andrew Koenig) writes:
<Does anyone have a C compiler + linker incapable of handling this:
<
<	extern int foo;
<
<	main() { }
<
<That is: are there any C implementations that turn an
<`extern' declaration for a variable that is never actually
<referenced into a genuine external reference that inhibits
<linking?
<-- 
<				--Andrew Koenig
<				  ark@europa.att.com

Works for me.  That is, it compiles and links on our BSD VAX and my SGI
IRIS 4D/120.

Hope this was the answer you were looking for :-).
-- 
							spl
Steve Lamont, sciViGuy			EMail:	spl@ncsc.org
North Carolina Supercomputing Center	Phone: (919) 248-1120
Box 12732/RTP, NC 27709

bumby@math.rutgers.edu (Richard Bumby) (06/09/89)

In article <9449@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes:

> Does anyone have a C compiler + linker incapable of handling this:
> 
> 	extern int foo;
> 
> 	main() { }
> 
> That is: are there any C implementations that turn an
> `extern' declaration for a variable that is never actually
> referenced into a genuine external reference that inhibits
> linking?
> -- 
> 				--Andrew Koenig
> 				  ark@europa.att.com

I'm not sure what you are asking, but I compiled your program with the
following results:

Aztec C 8086 Pass1 4.10c  4-3-88 (C) 1982-1988 by Manx Software
Systems, Inc.
Aztec C 8086 Pass2 4.10c  4-11-88 (C) 1982-1988 by Manx Software
Systems, Inc.
8086 Assembler Vers. 4.10c  3-29-88

ln linktest.o -lc
Aztec C Linker Version 4.10c  3-29-88
Code: 0505  FarData: 0000  Data: 0062  Udata: 010c  Total: 00067e

with file sizes as shown below

LINKTEST C         30
LINKTEST O         90
LINKTEST EXE     1906

The program also runs without complain (doing nothing, of course).
Maybe some time I will look at the result with a debugger to see what
is actually there.
-- 

--R. T. Bumby ** Math ** Rutgers ** New Brunswick **
(in one form or another for all kinds of mail)
[bumby@math.rutgers.edu]

bumby@math.rutgers.edu (Richard Bumby) (06/09/89)

In article <9449@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes:

> Does anyone have a C compiler + linker incapable of handling this:
> 
> 	extern int foo;
> 
> 	main() { }
> 
> That is: are there any C implementations that turn an
> `extern' declaration for a variable that is never actually
> referenced into a genuine external reference that inhibits
> linking?
> -- 
> 				--Andrew Koenig
> 				  ark@europa.att.com

I'm not sure what you are asking, but I compiled your program with the
following results:

Aztec C 8086 Pass1 4.10c  4-3-88 (C) 1982-1988 by Manx Software
Systems, Inc.
Aztec C 8086 Pass2 4.10c  4-11-88 (C) 1982-1988 by Manx Software
Systems, Inc.
8086 Assembler Vers. 4.10c  3-29-88

ln linktest.o -lc
Aztec C Linker Version 4.10c  3-29-88
Code: 0505  FarData: 0000  Data: 0062  Udata: 010c  Total: 00067e

with file sizes as shown below

LINKTEST C         30
LINKTEST O         90
LINKTEST EXE     1906

The program also runs without complain (doing nothing, of course).
Maybe some time I will look at the result with a debugger to see what
is actually there.
-- 

--R. T. Bumby ** Math ** Rutgers ** New Brunswick **
(in one form or another for all kinds of mail)
[bumby@ma@h.rutgers.edu]
#!

diamond@diamond.csl.sony.junet (Norman Diamond) (06/09/89)

In article <9449@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes:

>Does anyone have a C compiler + linker incapable of handling this:
>	extern int foo;
>	main() { }
>That is: are there any C implementations that turn an
>`extern' declaration for a variable that is never actually
>referenced into a genuine external reference that inhibits
>linking?

Does it matter if any news reader actually has one?  Such things are
permitted to exist (or were permitted to; and this looks like the kind
of thing that ANSI didn't dare to touch).  If such things exist, they
are most likely on machines which aren't capable of receiving this
kind of news.  If they don't exist, they still might some day.  You
should never overestimate your opponents either ;-).

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp@relay.cs.net)
 The above opinions are my own.  However, if you see this at Waterloo, Stanford,
 or Anterior, then their administrators must have approved of these opinions.

guy@auspex.auspex.com (Guy Harris) (06/10/89)

>I'm not sure what you are asking,

I think he's asking "does anyone have a C compiler + linker incapable
of handling the given example?"  (in fact, that's what he did ask!)

A bit of (possible) background:

The C compilers that I've used - all UNIX ones - will not blithely
generate code that causes a symbol table entry to be made for symbols to
which no reference is made.  I.e., the mere *appearance* of the line

	extern int foo;

in a program will not change the generated object file.

The question was whether there *is* a compiler in which the mere
appearance of a line like:

	extern int foo;

in a file that contains no references to the "foo" declared by that line
will cause the object file generated by that compiler (we assume here
that the compiler *does* generate object files, which are to be linked
by a linker - as indicated by "C compiler + linker") to contain an
external reference to "foo" in its symbol table, such that said external
reference *must* be satisfied in order to successfully link that object
file into an executable image.

I.e., if you compile a file containing

	extern int foo;

	main() {}

and link that *with no other object files and with no libraries that
define "foo"*, will the link fail?

Several people have given examples where it *doesn't* fail, but I don't
know that those are interesting in this case; the existence of 1 million
compiler/linker pairs in which it doesn't fail doesn't demonstrate that
no compiler/linker pair exists in which it *does* fail. 

boyne@hplvli.HP.COM (Art Boyne) (06/12/89)

ark@alice.UUCP (Andrew Koenig) asks:
>Does anyone have a C compiler + linker incapable of handling this:
>
>	extern int foo;
>	main() { }
>
>That is: are there any C implementations that turn an
>`extern' declaration for a variable that is never actually
>referenced into a genuine external reference that inhibits
>linking?

Unfortunately, yes.  The old 68000 C cross compiler (64819) for the
original HP 64000 uP development system had this problem.  Fortunately,
the newer Unix-based cross compiler (64902) has this fixed.  Of course,
most people aren't concerned with portability *to* this environment.

(However, both the old and new *assemblers* exhibit this problem - so
I can't just translate my C header files for use in assembler and then
link a partial system.)

Art Boyne, boyne@hplvla.hp.com

tainter@cbnewsd.ATT.COM (johnathan.tainter) (06/12/89)

In article <1795@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
>I.e., if you compile a file containing
>	extern int foo;
>	main() {}
>and link that *with no other object files and with no libraries that
>define "foo"*, will the link fail?

>Several people have given examples where it *doesn't* fail, but I don't
>know that those are interesting in this case; the existence of 1 million
>compiler/linker pairs in which it doesn't fail doesn't demonstrate that
>no compiler/linker pair exists in which it *does* fail. 


I have an example where this is mishandled.  The Alcyon Developers Package
for the Atari ST certainly does this.  It makes using interface header files
an unpleasant prospect.

--johnathan.a.tainter--
   att!ihlpb!tainter

ark@alice.UUCP (Andrew Koenig) (06/13/89)

In article <186@cbnewsd.ATT.COM>, tainter@cbnewsd.ATT.COM (johnathan.tainter) writes:
> In article <1795@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
> >I.e., if you compile a file containing
> >	extern int foo;
> >	main() {}
> >and link that *with no other object files and with no libraries that
> >define "foo"*, will the link fail?

I recently asked a similar question.

I have since learned that this is explicitly legal in ANSI C.
The only compilers people have mentioned that DON'T handle
it correctly are on desktop micros of various sorts.
-- 
				--Andrew Koenig
				  ark@europa.att.com

shankar@hpclscu.HP.COM (Shankar Unni) (06/14/89)

> > >I.e., if you compile a file containing
> > >	extern int foo;
> > >	main() {}
> > >and link that *with no other object files and with no libraries that
> > >define "foo"*, will the link fail?
> I have since learned that this is explicitly legal in ANSI C.

True. Section 3.7.1, footnote 75 in the Dec 88 draft.

From the Dec 88 dpANS:

  75. Thus, if an identifier declared with external linkage is not used
      in an expression, there need be no external definition for it.

---
Shankar.