compiling templates with g++

llewelly@198.dsl.xmission.com llewelly@198.dsl.xmission.com
Tue Dec 21 12:41:00 GMT 1999


Put the definitions of your template functions in your header file.

If this causes problems, run 'info gcc' and then type 'gTemplate Instantiation'
  (info will tab-complete, like bash and tcsh).

gcc does not yet support having a definition of a templated function in a
  compilation unit separate from where it is used. 

ANSI C++ requires a definition of a templated function to be declared
  'export' for it to be availible to other compiliation units, but, as I
  said before, gcc doesn't support that yet.

On Tue, 21 Dec 1999, Amit Mehrotra wrote:

> Please forgive me if this is obvious but I am new to C++.
> 
> I am having trouble compiling .cc files with templates. The thing
> compiles but the .o file does not have the function code. When I link
> it with a test program, it says that the function is not declared. The
> funny thing is that when I merge .h and .cc into one .h, it compiles
> and runs without any problem!  Pragmas did not help either. The files
> and error messages are shown below:
> 
> I have two files quickSort.h and quickSort.cc:
> ----------------------------------quickSort.h------------------------------
> #ifndef QUICKSORT_H
> #define QUICKSORT_H
> #include "vector.h" // decaration of Subscript type and Vector template class
> 
> template <class Type>
> int quickSort(Vector<Type>&, Subscript, Subscript);
> template <class Type>
> int quickSort(Vector<Type>&, Vector<Type>&, Subscript, Subscript);
> 
> #endif // QUICKSORT_H
> 
> ----------------------------------quickSort.cc-----------------------------
> #include "quickSort.h"
> template <class Type>
> int quickSort(Vector<Type>& oof, Subscript poof, Subscript goof) {
> // the implementation
> }
> 
> template <class Type>
> int quickSort(Vector<Type>& oof, Vector<Type>& poof, Subscript goof, 
>         Subscript woof) {
> // the implementation
> }
> ---------------------------------error message----------------------------
> /tmp/ccJjCAn0.o: In function `test(void)':
> /tmp/ccJjCAn0.o(.text+0x416): undefined reference to `int quickSort<unsigned int>(Vector<unsigned int> &, unsigned int, unsigned int)'
> collect2: ld returned 1 exit status
> 



More information about the Gcc-help mailing list