Explicit instantiation of function templates in libraries
Sigurd Saue
sigu-sa@online.no
Wed Apr 14 13:16:00 GMT 2004
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?
Thanks for any help.
Sigurd Saue
More information about the Gcc-help
mailing list