473,382 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Linker Error when using C++ Templates.

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.
Jul 22 '05 #1
3 1795

"Lord Labakudas" <ll********@yahoo.com> wrote in message
news:d6**************************@posting.google.c om...
Hi,

I have the following simple template implementation:

// -------------- b.h ----------------- //
template <class t>
export 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 ;
}


But see also
http://www.parashift.com/c++-faq-lit...html#faq-34.14

Jonathan

Jul 22 '05 #2
In article <d6**************************@posting.google.com >,
Lord Labakudas <ll********@yahoo.com> wrote:
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/techt.../#whylinkerror
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 22 '05 #3
Lord Labakudas wrote:

[snip]
cat b.h #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<float>::~b(void);
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;
b<float> bf;
}
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.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: TR | last post by:
Hi I have this very simple piece of code I can't get running (source code below). I'm using the borland C++ compiler 6.0. The error I'm getting is: Unresolved external 'MyMap<int,...
1
by: Generic Usenet Account | last post by:
Don't you often wish that compiler and linker messages for templates were a little less cryptic and misleading? Consider this: I was getting this error message: undefined reference to `vtable...
1
by: Rebecca Hoffmann | last post by:
Hi, I have a serious problem while compiling a small project (a part of the Modular Flow Scheduling Middleware: ex1): There are 3 linker errors, all from symbols that point to templates: --...
3
by: Grahamo | last post by:
Hi, I have a question that pertains to Templates and link time. This is totally for my own understanding and to correct what's obviously an erroneous view of things on my behalf; Lets say I...
1
by: toton | last post by:
In C++ , when the class is declared in the header & not implemented in the cpp, the linker is not giving a error untill the method gets called. How to ensure that all non pure-virtual method...
5
by: Mark | last post by:
Sorry for creating such a newbish topic, but I just can't seem to figure out what the problem is here. // main.cpp #include <cstdlib> #include <iostream> #include "Vector.h" using namespace...
3
by: little.freaky | last post by:
Hello group, I have a problem with template classes and inheritance. I've searched on the internet to find a solution but all the examples look the same as my code (as far as I can tell) and I...
1
by: giganut | last post by:
Hi, I'm using MS VS.Net2003, and I'm getting a linker error in a project for a templated friend function Specifically: template <class Tclass Vec3 {...
1
by: Deepath G | last post by:
This is deepath.. I am getting some linker error when i am trying to connect Websphere MQ using Borland C++ Builder 2006 using imqi.hpp on windows. Error Message ----------------------- ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.