The following simple program doesn't link: ----- Derived.h ----- #ifndef Derived_hh #define Derived_hh #include <istream> #include <ostream> class D : public std::iostream { public: ~D() ; } ; #endif ----- Derived.cc ----- #include "Derived.hh" D::~D() { } ----- main.cc ----- #include "Derived.hh" int main() { D d ; return 0 ; } ----- ----- The compiler command was simply "g++ main.cc Derived.cc". Output was: ----- compiler output ----- /home/kanze/tmp/ccJrgTHi.o(.gnu.linkonce.t._ZN1DC1Ev+0x29): In function `D::D()': : undefined reference to `std::basic_iostream<char, std::char_traits<char> >::basic_iostream()' /home/kanze/tmp/cctV9drU.o(.text+0x149): In function `D::~D()': : undefined reference to `std::basic_iostream<char, std::char_traits<char> >::~basic_iostream()' /home/kanze/tmp/cctV9drU.o(.text+0x1fa): In function `D::~D()': : undefined reference to `std::basic_iostream<char, std::char_traits<char> >::~basic_iostream()' /home/kanze/tmp/cctV9drU.o(.text+0x2ac): In function `D::~D()': : undefined reference to `std::basic_iostream<char, std::char_traits<char> >::~basic_iostream()' /home/kanze/tmp/cctV9drU.o(.gnu.linkonce.r._ZTC1D0_Sd+0xc): undefined reference to `std::basic_iostream<char, std::char_traits<char> >::~basic_iostream()' /home/kanze/tmp/cctV9drU.o(.gnu.linkonce.r._ZTC1D0_Sd+0x10): undefined reference to `std::basic_iostream<char, std::char_traits<char> >::~basic_iostream()' collect2: ld returned 1 exit status ----- ----- I'm not an expert in your template instantiation strategy, but when I compile Derived.cc separately, then use nm on the object file, I find unresolved externals for symbols like std::iostream::~iostream (which shouldn't exist except as a typedef to a template instantiation, I think). From what little I understand, I would have expected a weak definition for the template instantiation (the missing function in the messages above). The relevant options I used when building the compiler were: --enable-threads --enable-__cxa_atexit --disable-nls (I don't think they have anything to do with the problem, but one never knows.) -- James Kanze
Can you please try either a snapshot of 3.4.4 or 4.0? I'm pretty sure this is already fixed: http://gcc.gnu.org/ml/gcc-patches/2005-02/msg00308.html.
Fixed but really this is binutils bug but oh well.
Subject: Re: Linker errors when deriving from std::iostream pinskia at gcc dot gnu dot org wrote: > ------- Additional Comments From pinskia at gcc dot gnu dot org 2005-04-16 13:12 ------- > Fixed but really this is binutils bug but oh well. Actually, I sort of suspected as much. I wasn't 100% sure, but I think I compiled and linked the same thing on a Sparc Solaris without problems. I hesitated sending it to you, but I didn't know who else to address myself to. (You guys seem serious about quality and usability, which isn't always the case. Even, all too often, when you pay for it. Despite my occasional criticisms, I really appreciate what you're doing.)