SV: Explicit instantiation of function templates in libraries

Sigurd Saue sigu-sa@online.no
Wed Apr 14 16:18:00 GMT 2004


In case someone was starting to apply any brain power on this one, I solved
the problem myself. And it's a bit embarrassing: It had nothing to do with
templates at all, but rather the linking order of libraries.

Sigurd

-----Opprinnelig melding-----
Fra: Sigurd Saue [mailto:sigu-sa@online.no] 
Sendt: 14. april 2004 15:16
Til: 'gcc-help@gcc.gnu.org'
Emne: Explicit instantiation of function templates in libraries

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