This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Template Instantiation Bug?(inheritance, virtual function, duplicate symbols for constructor)
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Template Instantiation Bug?(inheritance, virtual function, duplicate symbols for constructor)
- From: Mike Rosellini <m at icopyright dot com>
- Date: 07 Sep 2000 13:53:23 -0700
Hello. I must beg your attention for a moment, for I believe I have
located a bug in the GNU C++ compiler or the C++ friendly part of the
GNU linker. This bug hurts me. Ow.
First, I will summarize. Template B derives from template A.
Template A has at least one virtual function. Template B has a
constructor. Two separate object files (.o) contain instantiations of
template B with the same template argument(s). These two object files
are put in a room together for linking into a single dynamic shared
object (.so). The linker scowls and complains furiously that it has
encountered a duplicate symbol for the instantiated B's constructor.
Now, I will offer details. I am using GCC 2.95.3. I am using ld
2.9.5. I am using RedHat Linux 6.0 on an Intel machine. I found this
gcc inside the RPM gcc-2.95.3-0.20000503.1. I found the C++ part
inside the RPM gcc-c++-2.95.3-0.20000503.1. I found my ld inside the
RPM binutils-2.9.5.0.22-6. My cpp and libc++ are also version 2.95.3
and also came from RPMs. I found my RPMs at ftp.valinux.org and
(perhaps obviously) have made no modifications to the source code used
to create the executables which I am using.
Next, some lovely source code which does not compile with 2.95.3 (but
which, I might note, does compile with egcs 2.91.66 and ld 2.9.1).
---- a.h ------------------------------------------------------------
template<class X>
class A {
public:
virtual void f () = 0;
};
---- /a.h -----------------------------------------------------------
---- b.h ------------------------------------------------------------
#include "a.h"
template<class X>
class B : public virtual A<X> {
public:
B ();
void f ();
};
template<class X>
B<X>::B ()
{
}
template<class X>
void B<X>::f ()
{
}
---- /b.h -----------------------------------------------------------
---- x.C ------------------------------------------------------------
#include "b.h"
void x ()
{
B<int> b;
}
---- /x.C -----------------------------------------------------------
---- y.C ------------------------------------------------------------
#include "b.h"
void y ()
{
B<int> b;
}
---- /y.C -----------------------------------------------------------
Please excuse me for not sending preprocessed output as is requested
in the (rather outdated) Texinfo documentation that I have; it did not
seem prudent.
If you have created files by the names that I gave them, you may issue
the following commands to attempt to reproduce the linking error that
I am receiving.
g++ -g -c -o x.o x.C
g++ -g -c -o y.o y.C
g++ -shared -o liblib.so x.o y.o
If you succeed in reproducing the linking error, it might look
something like this.
y.o: In function `B<int> type_info function':
/.../y.C(.text+0x0): multiple definition of `B<int>::B(int)'
x.o(.text+0x0):/.../x.C: first defined here
collect2: ld returned 1 exit status
I believe that there is no particular need for me to point out the
behavior that I find to be in error, but I'll do it anyhow! It
doesn't seem to me as though the linker ought to complain about this
duplicate symbol. It is for those of you who may feel that this begs
the question, "Why not?" and who would make some comment about GCC
policy on template instantiation that I point out the following: this
didn't happen in 2.91.66 w/ ld 2.9.1; this doesn't happen without the
presence of a virtual function and inheritance; and, finally, it's
just plain silly!
Please help. Ow. Ow! It hurts.
Finally, I must make a terrible admission: in addition to posting to
this list, I have already submitted an entry into GNATS. I understand
that this represents an almost unspeakable breach of protocol. Please
don't hit me. Dire circumstances lead to extreme measures. The GNATS
bug number is c++/513.
Thank you.
--
mikeroselliniicopyrightdotcomrentonwaphone4254304555x614fax4254308878