From: thon AT irp DOT uni-stuttgart DOT de (andreas thon) Subject: Re: Does DJGPP support C++ templates? To: nfluhr1 AT gl DOT umbc DOT edu (Neil L. Fluhr ; BS CMSC) Date: Thu, 16 Mar 1995 11:47:00 +0100 (MET) Cc: djgpp AT sun DOT soe DOT clarkson DOT edu > > I am trying to use a List template class and I keep getting: > > "undefined reference List::Insert()" > "undefined reference List::First()" > > ...and so on for every List function that is called in my client > program. > > I am using my own list implementation (not the one included with DJGPP). I > have my List template class declaration in my header file (list.h) and the > implementation in my list.cc file. My client program is in its separate > file (intlist.cc). > > I have tried the -v switch and the above output only occurs after the linker > is called. > > Any ideas? Please send responses to my e-mail address > as I do not subscribe to the mailing list. > > Thanks in advance! > Neil > I have just found the answer in a FAQ in the gnu.g++.help newsgroup: I get undefined symbols when using templates ============================================ (Thanks to Jason Merrill for this section). g++ does not automatically instantiate templates defined in other files. Because of this, code written for cfront will often produce undefined symbol errors when compiled with g++. You need to tell g++ which template instances you want, by explicitly instantiating them in the file where they are defined. For instance, given the files `templates.h': template class A { public: void f (); T t; }; template void g (T a); `templates.cc': #include "templates.h" template void A::f () { } template void g (T a) { } main.cc: #include "templates.h" main () { A a; a.f (); g (a); } compiling everything with `g++ main.cc templates.cc' will result in undefined symbol errors for `A::f ()' and `g (A)'. To fix these errors, add the lines template class A; template void g (A); to the bottom of `templates.cc' and recompile. Andreas