Bug 21056 - Linker errors when deriving from std::iostream
Summary: Linker errors when deriving from std::iostream
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.3
: P2 normal
Target Milestone: 3.4.4
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-04-16 11:33 UTC by James Kanze
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description James Kanze 2005-04-16 11:33:44 UTC
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
Comment 1 Paolo Carlini 2005-04-16 11:53:00 UTC
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.
Comment 2 Andrew Pinski 2005-04-16 13:12:25 UTC
Fixed but really this is binutils bug but oh well.
Comment 3 James Kanze 2005-04-16 21:00:38 UTC
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.)