[g++ 2.95.1] linker problem with static variables in classes

Jean-Francois MOINE d3moine@bst.bsf.alcatel.fr
Fri Nov 26 06:29:00 GMT 1999


In the following example, the variables 'cl::a' and 'b' have the same
memory location.
The bug appeared when cross-compiling from Sparc/Solaris to PowerPC.
(it does not exist using g++ and the Sun linker).

The linker map gives:

	 .sbss          0x0187366c        0x4 f1.o
	                                  0x8 (size before relaxing)
	                0x01873670                cl::a
	                0x0187366c                c
	 .sbss          0x01873670        0x4 f2.o
	                0x01873670                b

The problem disappears when either:
	- 'int c;' is not there,
or	- cl::a is initialized ('cl::a = 0;').

--------- f1.cc ---------
/*
 * GNU g++ 2.95.1 - linker problem with static variables in classes.
 *
 *	When a class static variable is not initialized, the linker
 *	'relaxes' it, so many variables are at the same location.
 *
 * This problem did not exist with g++ 2.8.1.
 *
 * Tools versions:
 *	gcc version 2.95.1 19990816 (release)
 *	GNU ld version 2.9 (with BFD 980402)
 *
 * Generation:
 *	g++ -c f1.cc -o f1.o
 *	g++ -c f2.cc -o f2.o
 *	ld f1.o f2.o -o execfile
 */

#include <stdio.h>

class cl {
public:
	static int a;
};

int c;
int cl::a;
extern int b;

int main(int argc, char *argv[])
{
	b = 0;
	printf("d:%d\n", b);
	cl::a = 1;
	printf("d:%d\n", b);
}
-------------------------

--------- f2.cc ---------
int b;
-------------------------

-- 
Ken ar c'hentañ		** Breizh ha Linux atav ! **
		mailto:Jean-Francois.Moine@bst.bsf.alcatel.fr
Jef		(home mailto:moinejf@free.fr )
		http://aww.bst.bsf.alcatel.fr/projects/lsd/dhs3/d3moine/




More information about the Gcc-bugs mailing list