Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Link errors when deriving from iostream (Linux only)

4 views
Skip to first unread message

James Kanze

unread,
Apr 2, 2005, 7:28:45 AM4/2/05
to
I have an object which derives from std::iostream.

When I try and link it, I get undefined symbols for the
destructor of iostream. Only under Linux, curiously, the exact
same code works under Solaris, so I suppose that it is either a
problem with my installation, or some flags that I missed that
are necessary under Linux, but not elsewhere.

FWIW:

- Whether the object file with my class is in a library or
specified directly doesn't seem to change anything.

- An nm -p -C on the object file with my class reveles a
number of U entries for std::iostream! This sort of
surprises me; I could almost understand
std::basic_iostream<char> (which is what I get when I
specify the template in full when deriving), although I
really don't know what to expect -- I was under the
impression that g++ instantiated the templates in every file
which used them.

I might add that if I change the code to derive from just
std::istream (instead of std::iostream), I still get U
entries for std::istream, but the code links and works as
expected. So I don't know what to think.

Anyway, does anyone know of any options which might affect this,
either when building the compiler, or when compiling. Or is
this a compiler bug, which just shows up on certain systems?

(Just for the record: a minimal example :

---------- MyClass.hh ------------

#ifndef GB_MyClass_hh
#define GB_MyClass_hh

#include <istream>
#include <ostream>

struct MyClass : public std::iostream
{
MyClass() ;
~MyClass() ;
} ;
#endif

---------- MyClass.cc ------------

#include "MyClass.hh"

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

---------- main.cc ------------

#include "MyClass.hh"

int
main()
{
MyClass c ;
return 0 ;
}

---------- command ------------
> g++ main.cc MyClass.cc
/home/kanze/tmp/ccSi0VUa.o(.text+0x117): In function `MyClass::MyClass()':
: undefined reference to `std::basic_iostream<char,
std::char_traits<char> >::basic_iostream()'
/home/kanze/tmp/ccSi0VUa.o(.text+0x18e): In function `MyClass::MyClass()':
: undefined reference to `std::basic_iostream<char,
std::char_traits<char> >::basic_iostream()'
/home/kanze/tmp/ccSi0VUa.o(.text+0x22d): In function `MyClass::~MyClass()':
: undefined reference to `std::basic_iostream<char,
std::char_traits<char> >::~basic_iostream()'
/home/kanze/tmp/ccSi0VUa.o(.text+0x2de): In function `MyClass::~MyClass()':
: undefined reference to `std::basic_iostream<char,
std::char_traits<char> >::~basic_iostream()'
/home/kanze/tmp/ccSi0VUa.o(.text+0x390): In function `MyClass::~MyClass()':
: undefined reference to `std::basic_iostream<char,
std::char_traits<char> >::~basic_iostream()'
/home/kanze/tmp/ccSi0VUa.o(.gnu.linkonce.r._ZTC7MyClass0_Sd+0xc):
undefined reference to `std::basic_iostream<char, std::char_traits<char>
>::~basic_iostream()'
/home/kanze/tmp/ccSi0VUa.o(.gnu.linkonce.r._ZTC7MyClass0_Sd+0x10):
undefined reference to `std::basic_iostream<char, std::char_traits<char>
>::~basic_iostream()'
collect2: ld returned 1 exit status

----------------------------------

(I know that the above program isn't legal, since it calls the
destructor of ios without ever having invoked ios::init(). But
it should compile and link without problems. I just simplified
to a maximum.)


--
James Kanze mailto: james...@free.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

0 new messages