Linker Error when using C++ Templates.

L

Lord Labakudas

Hi,

I have the following simple template implementation:

// -------------- b.h ----------------- //
template <class t>
class b
{
public:
b() ;
~b() ;
} ;


// -------------- b.cpp --------------- //
#include "b.h"
template <class t>
b<t>::b()
{
}
template <class t>
b<t>::~b()
{
}

// --------------- main.cpp ------------- //
#include "b.h"
main()
{
b<int> bi ;
b <float> bf ;
}

When the compile the above program main.cpp using,

gcc main.cpp b.cpp

I get the following error message.

/tmp/cc6si1GJ.o(.text+0x19): In function `main':
: undefined reference to `b<int>::b[in-charge]()'
/tmp/cc6si1GJ.o(.text+0x28): In function `main':
: undefined reference to `b<float>::b[in-charge]()'
/tmp/cc6si1GJ.o(.text+0x37): In function `main':
: undefined reference to `b<float>::~b [in-charge]()'
/tmp/cc6si1GJ.o(.text+0x4e): In function `main':
: undefined reference to `b<int>::~b [in-charge]()'
/tmp/cc6si1GJ.o(.text+0x6b): In function `main':
: undefined reference to `b<int>::~b [in-charge]()'
/tmp/cc6si1GJ.o(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Am I missing something ?

Thanks,
LL.
 
J

Jonathan Turkanis

Lord Labakudas said:
Hi,

I have the following simple template implementation:

// -------------- b.h ----------------- //
template <class t>

export template said:
class b
{
public:
b() ;
~b() ;
} ;


// -------------- b.cpp --------------- //
#include "b.h"
template <class t>
b<t>::b()
{
}
template <class t>
b<t>::~b()
{
}

// --------------- main.cpp ------------- //
#include "b.h"
main()
{
b<int> bi ;
b <float> bf ;
}

But see also
http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.14

Jonathan
 
G

Greg Comeau

I have the following simple template implementation:

// -------------- b.h ----------------- //
template <class t>
class b
{
public:
b() ;
~b() ;
} ;

// -------------- b.cpp --------------- //
#include "b.h"
template <class t>
b<t>::b()
{
}
template <class t>
b<t>::~b()
{
}

// --------------- main.cpp ------------- //
#include "b.h"
main()
{
b<int> bi ;
b <float> bf ;
}

When the compile the above program main.cpp using,

gcc main.cpp b.cpp

I get the following error message.

/tmp/cc6si1GJ.o(.text+0x19): In function `main':
: undefined reference to `b<int>::b[in-charge]()'
/tmp/cc6si1GJ.o(.text+0x28): In function `main':
: undefined reference to `b<float>::b[in-charge]()'
/tmp/cc6si1GJ.o(.text+0x37): In function `main':
: undefined reference to `b<float>::~b [in-charge]()'
/tmp/cc6si1GJ.o(.text+0x4e): In function `main':
: undefined reference to `b<int>::~b [in-charge]()'
/tmp/cc6si1GJ.o(.text+0x6b): In function `main':
: undefined reference to `b<int>::~b [in-charge]()'
...
Am I missing something ?

Check out http://www.comeaucomputing.com/techtalk/templates/#whylinkerror
 
E

E. Robert Tisdale

Lord Labakudas wrote:

[snip]
#ifndef GUARD_B_H
#define GUARD_B_H 1

// -------------- b.h ----------------- //
template <class t>
class b {
public:
b(void);
~b(void);
};

template <class t>
b<t>::b(void) {
}

template <class t>
b<t>::~b(void) {
}

#endif//GUARD_B_H 1
cat b.cpp
// -------------- b.cpp --------------- //
#include "b.h"

template b<int>::b(void);
template b<int>::~b(void);
template b<float>::b(void);
template b said:
g++ -Wall -ansi -pedantic -frepo -c b.cpp
ls b.* b.cpp b.h b.o b.rpo
expand main.cpp
// --------------- main.cpp ------------- //
#include "b.h"

int main(int argc, char* argv[]) {
b<int> bi;
g++ -Wall -ansi -pedantic -o main main.cpp b.o
./main

The standards don't (and probably can't) specify
how function templates get instantiated.
Hopefully, C++ compiler developers will eventually agree
upon a "convention" that they can all support. Until then,
it is probably safest to instantiate function templates explicitly.

Because function templates don't, by themselves, cause the C++ compiler
to emit any code, they can be included in the [public] header file.
Of course, inline function templates must be include in the header file.

Explicit external template function instantiations, on the other hand,
*always* cause the compiler to emit code and should *never* be
included in the header file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top