From: warsaw@anthem.nlm.nih.gov (Barry A. Warsaw)
Subject: g++ doesn't always generate symbol defs
Reply-To: warsaw@nlm.nih.gov (Barry A. Warsaw)
Organization: GNUs Not Usenet
Distribution: gnu
Date: Tue, 1 Sep 1992 20:43:08 GMT

FTR, g++ 2.2.2, Sun SS2, SunOS 4.1.1

It appears as if g++ does not always correctly generate symbols for
static members.  Yes, I know all about declaration vs. definition
(note I'm not saying that I didn't goof up some where ;-).  Anyway, in
the following example, the class in question has a data member which
is static const & const.  If the definition of this member is in file
containing main, it works, if not I'll get an undefined symbol.

To recreate, unshar what follows my siggy, and type "make nobug".
This should correctly generate an a.out file.  Then type "make bug"
and you'll get:

warsaw@anthem[169]% make bug
g++ -v main.cc single.cc
Reading specs from /depot/gnu/plat/lib/gcc-lib/sparc/2.2.2/specs
gcc version 2.2.2
 /depot/gnu/plat/lib/gcc-lib/sparc/2.2.2/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ -D__sparc -D__sun -D__unix main.cc /usr/tmp/cca27725.i
GNU CPP version 2.2.2 (sparc)
 /depot/gnu/plat/lib/gcc-lib/sparc/2.2.2/cc1plus /usr/tmp/cca27725.i -quiet -dumpbase main.cc -version -o /usr/tmp/cca27725.s
GNU C++ version 2.2.2 (sparc) compiled by GNU C version 2.2.2.
 as -o main.o /usr/tmp/cca27725.s
 /depot/gnu/plat/lib/gcc-lib/sparc/2.2.2/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ -D__sparc -D__sun -D__unix single.cc /usr/tmp/cca27725.i
GNU CPP version 2.2.2 (sparc)
 /depot/gnu/plat/lib/gcc-lib/sparc/2.2.2/cc1plus /usr/tmp/cca27725.i -quiet -dumpbase single.cc -version -o /usr/tmp/cca27725.s
GNU C++ version 2.2.2 (sparc) compiled by GNU C version 2.2.2.
 as -o single.o /usr/tmp/cca27725.s
 /depot/gnu/plat/lib/gcc-lib/sparc/2.2.2/ld -e start -dc -dp /lib/crt0.o -L/depot/gnu/plat/lib/gcc-lib/sparc/2.2.2 -L/depot/gnu/plat/lib main.o single.o -lg++ -lgcc -lc -lgcc
ld: Undefined symbol 
   __9Singleton$OnlyOne 
collect: /usr/bin/ld returned 2 exit status
*** Error code 1
make: Fatal error: Command failed for target `bug'


The only difference being that in the nobug case, the static member is
instantiated and assigned in the file containing main, in the bug case
it is instantiated and assigned in a file *not* containing main.
There may be more to it than that.  If its me and not gcc, I
apologize.

Thanks,
-Barry

-------------------- snip snip --------------------
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  makefile single.cc single.hh main.cc
# Wrapped by warsaw@anthem on Tue Sep  1 15:41:50 1992
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'makefile'\"
else
echo shar: Extracting \"'makefile'\" \(159 characters\)
sed "s/^X//" >'makefile' <<'END_OF_FILE'
XCC = g++ -v
X
Xbug:
X	$(CC) main.cc single.cc
X
Xnobug:
X	$(CC) -DDEFINE_IN_MAIN single.cc main.cc
X
Xshar:
X	shar -o gcc.bug.shar makefile single.cc single.hh main.cc
END_OF_FILE
if test 159 -ne `wc -c <'makefile'`; then
    echo shar: \"'makefile'\" unpacked with wrong size!
fi
# end of 'makefile'
fi
if test -f 'single.cc' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'single.cc'\"
else
echo shar: Extracting \"'single.cc'\" \(585 characters\)
sed "s/^X//" >'single.cc' <<'END_OF_FILE'
X#include "single.hh"
X
XSingleton::Singleton( char* name )
X	: xname( name )
X{
X	cout << "Singleton ctor..." << endl;
X};
X
X
XSingleton::~Singleton( void )
X{
X	cout << "Singleton dtor..." << endl;
X};
X
X
XSingletonBuilder::SingletonBuilder( void )
X	: Singleton( "built from SingletonBuilder" )
X{
X	cout << "SingletonBuilder ctor..." << endl;
X};
X
X
XSingletonBuilder::~SingletonBuilder( void )
X{
X	cout << "SingletonBuilder dtor..." << endl;
X};
X
X
X#ifndef DEFINE_IN_MAIN
XSingletonBuilder secretlyBuiltByLibrary;
Xconst Singleton& const Singleton::OnlyOne = secretlyBuiltByLibrary;
X#endif DEFINE_IN_MAIN
END_OF_FILE
if test 585 -ne `wc -c <'single.cc'`; then
    echo shar: \"'single.cc'\" unpacked with wrong size!
fi
# end of 'single.cc'
fi
if test -f 'single.hh' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'single.hh'\"
else
echo shar: Extracting \"'single.hh'\" \(417 characters\)
sed "s/^X//" >'single.hh' <<'END_OF_FILE'
X#ifndef __SINGLETON_HH__
X#define __SINGLETON_HH__
X
X#include <iostream.h>
X
Xclass Singleton
X{
Xpublic:
X	static const Singleton& const OnlyOne;
X
X	char* name( void ) const
X	{
X		return( xname );
X	};
X
Xprotected:
X	Singleton( char* name );
X	~Singleton( void );
X
Xprivate:
X	char* xname;
X};
X
X
X
Xclass SingletonBuilder : public Singleton
X{
Xpublic:
X	SingletonBuilder( void );
X	~SingletonBuilder( void );
X};
X
X#endif __SINGLETON_HH__
END_OF_FILE
if test 417 -ne `wc -c <'single.hh'`; then
    echo shar: \"'single.hh'\" unpacked with wrong size!
fi
# end of 'single.hh'
fi
if test -f 'main.cc' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'main.cc'\"
else
echo shar: Extracting \"'main.cc'\" \(358 characters\)
sed "s/^X//" >'main.cc' <<'END_OF_FILE'
X#include "single.hh"
X
X#ifdef DEFINE_IN_MAIN
XSingletonBuilder secretlyBuiltByLibrary;
Xconst Singleton& const Singleton::OnlyOne = secretlyBuiltByLibrary;
X#endif DEFINE_IN_MAIN
X
X
Xint main( int, char** )
X{
X	cout << "main entered." << endl;
X	cout << "OnlyOne's name is: " << Singleton::OnlyOne.name() << endl;
X	cout << "exiting main." << endl;
X
X	return( 0 );
X};
END_OF_FILE
if test 358 -ne `wc -c <'main.cc'`; then
    echo shar: \"'main.cc'\" unpacked with wrong size!
fi
# end of 'main.cc'
fi
echo shar: End of shell archive.
exit 0

Newsgroups: gnu.g++.bug