Explicit instantiation of function templates in libraries
llewelly@xmission.com
llewelly@xmission.com
Thu Apr 15 14:01:00 GMT 2004
"Sigurd Saue" <sigu-sa@online.no> writes:
> Could someone please explain how to do explicit instantiation of function
> templates in a static library? I get "undefined reference" linking errors
> when doing it like this:
>
> ////////////////////////////////////////
> //vvEigenSystem.h - template declaration
>
> template <class T> bool jacobi(vnl_matrix<T>& input, vnl_matrix<T>& vectors,
> vnl_vector<T>& values);
>
>
> /////////////////////////////////////////
> //vvEigenSystem.cpp - template definition and explicit instantiation
>
> #include "vvEigenSystem.h"
>
> // Definition
> template <class T> bool jacobi(vnl_matrix<T>& input, vnl_matrix<T>& vectors,
> vnl_vector<T>& values) { ... }
>
> // Explicit instantiation
> template bool jacobi(vnl_matrix<float>& input, vnl_matrix<float>& vectors,
> vnl_vector<float>& values);
>
> /////////////////////////////////////////
> //vvCalculations.cpp - using the template function
>
> #include "vvEigenSystem.h"
>
> void calculate()
> {
> ...
> jacobi(m_eigProblem, m_eigVectors, m_eigValues);
> ...
> }
>
> If vvCalculations.cpp is in the same translations unit (same static library)
> as vvEigenSystem.* then this works fine, no errors. If they're in two
> different static libraries the linker reports an "undefined reference" on
> the jacobi-function. I have not used any pragmas or compiler options so far,
> since the information give on this is very confusing ("deprecated",
> "shouldn't be used", "makes no difference", etc.). We use gcc 3.3.2. What's
> the right way to do this?
gcc does not yet support any form of seperate compiliation for
templates. So the full definition of every template must be
#included in every translation unit which instantiates it.
More information about the Gcc-help
mailing list