Template instantiation & inheritance

John Graham johngavingraham@googlemail.com
Sat Feb 27 22:41:00 GMT 2010


Hi there,

I'm having some trouble with explicit template instantiation and
inheritance using two instances of g++, and was wondering if someone a
bit more wise could help me out. I read the fine explanation at
http://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html, but
that doesn't seem to cover subclassing, which is where my problem
(apparently) is.

I'm building a library that includes an RTP component and an
application that uses that library. One of the classes in the public
interface of library has a member from the GNU ccRTP library:


#include <ccrtp/rtp.h>

class RTPAudioPort
	: public AudioPort
{
...
	private:
		ost::SingleThreadRTPSession<ost::RTPBaseUDPIPv4Socket,
ost::RTPBaseUDPIPv4Socket, ost::AVPQueue> *rtpSession;
...
};


Now, I want to compile with -fno-implicit-templates if at all
possible, because we're also compiling this library on Windows (visual
c++ 2008), and it requires explicit template instantiation (from what
searching I've done). I'm compiling with two different gcc toolchains,
one native (i486-linux-gnu, g++ version 4.4.1) and one cross
(arm-926ejs-linux-gnueabi, g++ version 4.3.4).

So, I can compile the library just fine without template
instantiation, and then when I try and compile an application that
uses it, I get the expected sorts of errors from both linkers:


undefined reference to `vtable for
ost::SingleThreadRTPSession<ost::RTPBaseUDPIPv4Socket,
ost::RTPBaseUDPIPv4Socket, ost::AVPQueue>'
(and more of similar)


My problem is exactly how/where I should instantiate my templates so
that my RTPAudioPort class can be used by my application. I can keep
the native-toolchain happy by placing my template instantiation (i.e.
the line "template class
ost::SingleThreadRTPSession<ost::RTPBaseUDPIPv4Socket,
ost::RTPBaseUDPIPv4Socket, ost::AVPQueue>;") in that class's header
file - that way, my library builds on the native toolchain and my
application can also compile and link successfully.

However, when I do this, the cross-toolchain complains when compiling
the library (correctly, I think...?) because of multiple definitions:


multiple definition of `typeinfo for
ost::SingleThreadRTPSession<ost::RTPBaseUDPIPv4Socket,
ost::RTPBaseUDPIPv4Socket, ost::AVPQueue>'
(etc.)


Alright, I probably shouldn't be putting that in a header file, so I
try putting the instantiation in the source file. Now, both libraries
compile, but when I try and use the library from my application, both
toolchains complain of:


undefined reference to `typeinfo for
ost::TRTPSessionBase<ost::RTPBaseUDPIPv4Socket,
ost::RTPBaseUDPIPv4Socket, ost::AVPQueue>'
undefined reference to `VTT for
ost::TRTPSessionBase<ost::RTPBaseUDPIPv4Socket,
ost::RTPBaseUDPIPv4Socket, ost::AVPQueue>'
undefined reference to `vtable for
ost::TRTPSessionBase<ost::RTPBaseUDPIPv4Socket,
ost::RTPBaseUDPIPv4Socket, ost::AVPQueue>'


It turns out that ost::SingleThreadRTPSession is a direct subclass of
ost::TRTPSessionBase (see
http://gnu.gds.tuwien.ac.at/software/ccrtp/doc/refman/html/class_single_thread_r_t_p_session.html
in the library's api). I notice the linker doesn't seem to be missing
any function definitions or anything, just the vtable/VTT/typeinfo (or
maybe it just doesn't get to needing them before it exits due to this
error?).

And that's basically where I'm at. I'd have thought that instantiating
the subclass would also cause gcc to emit the necessary superclass
info, as the native toolchain seems to do when I instantiate it in the
header file.

Can anybody point me to what I'm missing?

Many thanks for reading!

John G



More information about the Gcc-help mailing list