Re: Undefined reference to '...'
Hi Gilbert and Orn:
Read info gcc:
use the command
control s template instantiation
you will find a big section that explains how to do the template
instantantiation with the template class and implementation in separate
files.
There are pragmas for this, and the technique taht Borland C++ uses for
this works (according to the info files). I use the pragmas.
Of course having the template class and implementation together works as
it always has.
--David
On Tue, 2 Sep 1997, Gilbert Laycock wrote:
> >>>>> Orn E Hansen <oe.hansen@oehansen.pp.se> writes:
>
> > The program compiles without errors, but when the linker is to link the
> > object files, it is persistent that the reference to 'something' within
> > the main program is undefined to 'ancestor<...>'. Every reference to
> > the object 'ancestor<int>' is reported as undefined in the program.
>
> > Does anyone have a clue as to why this occurs, and if there is a cure
> > to this?
>
> I think you have come across a well known bug/limitation in g++ 2.7.x
> Basically g++ fails to instantiate templates properly (or at all) if
> the definition is in a separate file to the instantiation; so the
> linker reports "undefined references".
>
> The work around is to explicitly instantiate the template with
> whatever classes you are are going to use in the same file as the
> definition.
>
> So in your example:
> > template<class T>
> > class ancestor {
> > private:
> > ...
> > public:
> > ancestor()
> > ~ancestor()
> > };
>
> > template<class T>
> > ancestor<T>::ancestor()
> > {
> > ...
> > }
> add this to the end of the file:
> template class ancestor<int>;
>
> And then the linking should work OK.
>
> Of course, this is not much use in many situations...
>
> See the g++ FAQ for a bit more information.
>
> I believe that this (and many other) problems have been fixed for
> gcc/g++ 2.8 which is apparently nearly-but-not-quite-ready for release
> (and has been for some time now). The egcs project
> http://www.cygnus.com/egcs/
> is (I think) using much of the same same code base as gcc 2.8, so if
> it is important to you, you could try building from one of their
> snapshots.
>
>
> --
>
> Gilbert Laycock email: gtl1@mcs.le.ac.uk
> Maths and Computer Science, http://www.mcs.le.ac.uk/~glaycock
> Leicester University phone: (+44) 116 252 3902
>
>
> --
> TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
> debian-user-request@lists.debian.org .
> Trouble? e-mail to templin@bucknell.edu .
>
>
-------------------------------------------------------------------------
LINUX: the FREE 32 bit OS for [345]86 PC's available NOW!
David B Teague | User interface copyrights & software patents make
teague@wcu.edu | programing a dangerous business. Ask me or lpf@lpf.org
spy counter-intelligence wild porno sex gold bullion Soviet Bosnia clipper
National Security Council explosion Treasury terrorist Delta Force bomb Iran
Mossad data encryption munitions Serbian hydrazine ammonium nitrate fuel oil
--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
debian-user-request@lists.debian.org .
Trouble? e-mail to templin@bucknell.edu .
Reply to: