MSC v5.1 bug; variable not made public

Eelco van Asperen evas at euraiv1.UUCP
Wed Nov 2 09:35:03 AEST 1988


Hi,

I've just found a nice bug in MSC v5.1; if you declare an unitialized
variable in a module that has no other publics and put that module in
a library, the variable will _not_ be found by LINK. Here's a small
example;

File alfa.c:
	char	alfa[1000];

	proc_alfa() {}

File beta.c:
	extern	char	alfa[];

	main()
	{
		strcpy(alfa,"This is a bug !");
	#ifdef CALL_ALFA
		proc_alfa();
	#endif
		puts(alfa);
		exit(0);
	}

compile this;
	cl -c alfa.c beta.c
create a library;
	lib demo +alfa;
and try to link;
	link /noi beta,,,demo;
and bingo;
	Microsoft (R) Overlay Linker  Version 3.65   
	Copyright (C) Microsoft Corp 1983-1988.  All rights reserved.

	LINK : error L2029: Unresolved externals:

	_alfa in file(s):
	 BETA.OBJ(beta.c)

	There was 1 error detected

Now, I did not insert that #ifdef in beta.c for nothing; let's recompile
with -DCALL_ALFA;

	cl -DCALL_ALFA -c beta.c
	link /noi beta,,,demo;

and everything is fine.
To determine what caused the error, I did the same for TurboC v1.5;

	tcc -c alfa.c beta.c
	lib demo +alfa;
	link \tc\lib\c0s beta,beta,,demo \tc\lib\cs;

and hey, no errors !
I then used OBJSNOOP to peek in the OBJ-files to see what publics they
contained and sure enough the TurboC version contained _alfa while the
MSC version did not;
TurboC:
	---------- alfa.obj

	public _alfa
	public _proc_alfa
MSC:
	---------- alfa.obj

	extrn  __acrtused
	extrn  __chkstk
	extrn  _proc_alfa
	public _proc_alfa


The same thing happens if 'alfa' is an int; however, if you initialize
'alfa', then everything will work ok. For example;

	char	alfa[] = "Just a test!";

Peeking with OBJSNOOP at ALFA.OBJ shows;

	---------- alfa.obj

	extrn  __acrtused
	extrn  __chkstk
	extrn  _proc_alfa
	public _alfa
	public _proc_alfa

I realize that this is a pathetic example but I was bitten by it and 
it took me an hour to find why the *****y linker could not find two 
global arrays I had declared in a separate file with no other entries.
(and then another half hour to document it, try alternatives and 
write this ;-)

-- 
Eelco van Asperen.		            EUR/Rotterdam/Netherlands
uucp: evas at eurtrx / mcvax!eurtrx!evas	 earn/bitnet: asperen at hroeur5
"We'd like to know a little bit about you for our files"
                                    - Mrs.Robinson, Simon & Garfunkel     



More information about the Comp.lang.c mailing list