public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Explicit instantiation of function templates in libraries
@ 2004-04-14 13:16 Sigurd Saue
  2004-04-15 14:01 ` llewelly
  0 siblings, 1 reply; 2+ messages in thread
From: Sigurd Saue @ 2004-04-14 13:16 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Explicit instantiation of function templates in libraries
  2004-04-14 13:16 Explicit instantiation of function templates in libraries Sigurd Saue
@ 2004-04-15 14:01 ` llewelly
  0 siblings, 0 replies; 2+ messages in thread
From: llewelly @ 2004-04-15 14:01 UTC (permalink / raw)
  To: Sigurd Saue; +Cc: gcc-help

"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.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-04-15 14:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-14 13:16 Explicit instantiation of function templates in libraries Sigurd Saue
2004-04-15 14:01 ` llewelly

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).