[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Undefined reference to '...'



>>>>> 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 .


Reply to: