[comp.lang.c++] Bug in Zortech C++ 2.0

rozin@unix.cis.pitt.edu (R Rozin) (02/17/90)

It seems that Zortech C++ 2.0 has a bug in the way it
mangles names relating to typedef'd structs and typedef'd
unions.  Consider the following code (3 files):

/*---- u2.h ---*/

typedef union {
	int b;
	char c;
} U2 ;

void f(U2 *);


/*--- u2.c ---*/

#include "u2.h"

void f(U2 *a)
{
	return ;
}


/*--- main.c ---*/

typedef union {
	int a;
	char b;
} U1 ;

#include "u2.h"

main()
{
	U2 u;
	f(&u);
}

/*--------------------------------*/

The problem occurs during linkage, as it cannot find the function
f (in main.c) due to the mangled versions of f in u2.c and main.c.
It looks as if typedef'd unions (and structs) are numbered sequentially
so that order of #inclusion (or lack of it) matters!

In other words, in u2.c the only typedef'd union is U2, while in
main.c we have U1 and U2 as unions.  Thus f in main.c gets mangled to
_f__Np3_C2 and f in u2.c gets mangled to _f__Np3_C1 and the linker croaks.

(It seems that the _C2 in the mangled name for main.c indicates that it
is the second typedef'd union encountered, while the _C1 for u2.c
indicates that it is the first union encountered.)

The following output is generated by the ztc command (in verbose mode):


C:\>ztc -cpp -v main.c u2.c

ZTCPP1 -v -otemp.tmp main.c
ZTCPP1 2.06 Copyright (C) 1985-1989 by Zortech,   written by Walter Bright
main
ZTCPP1 complete. Time: 0.39 seconds

ZTC2 -v temp.tmp -omain
ZTC2 2.05 Copyright (C) 1985-1989 by Zortech,   written by Walter Bright
main
ZTC2 complete. Code: 0x0011 (17) Data: 0x0000 (0) Time: 0.44 seconds

ZTCPP1 -v -otemp.tmp u2.c
ZTCPP1 2.06 Copyright (C) 1985-1989 by Zortech,   written by Walter Bright
f
ZTCPP1 complete. Time: 0.33 seconds

ZTC2 -v temp.tmp -ou2
ZTC2 2.05 Copyright (C) 1985-1989 by Zortech,   written by Walter Bright
f__Np3_C1
ZTC2 complete. Code: 0x000a (10) Data: 0x0000 (0) Time: 0.49 seconds

BLINK main+u2/noi;
BLINK 4.02 Copyright (c) 1986-89 by Zortech, written by Bjorn N. Freeman-Benson
Error: undefined symbols:
    _f__Np3_C2 in main.c (main.OBJ)

BLINK complete. Time: 1.70 seconds

--- errorlevel 1

/*-----------------------------------------*/

The workaround is obviously to declare a typedef'd union before
U2 in u2.c so that the number of typedef'd unions will be the same
(and the unions of import must be in the same position) in the
different files for the compilation to proceed.  Yuck!


--Roman

--
Roman Rozin				    rozin@cs.pitt.edu
Graduate Student			    rozin@PITTVMS.BITNET
University of Pittsburgh

bright@Data-IO.COM (Walter Bright) (02/20/90)

In article <22379@unix.cis.pitt.edu> rozin@unix.cis.pitt.edu (R  Rozin) writes:
<It seems that Zortech C++ 2.0 has a bug in the way it
<mangles names relating to typedef'd structs and typedef'd
<unions.
<	typedef union { ... } U2 ;
<	void f(U2 *);
<The workaround is obviously to declare a typedef'd union before
<U2 in u2.c so that the number of typedef'd unions will be the same
<(and the unions of import must be in the same position) in the
<different files for the compilation to proceed.  Yuck!

No, the workaround is to put a tag name for the union:
	union U2 { ... };

In the absence of a tag name, the compiler generates one. (There'll be
a warning for this in the next version.)