Very simple template question

W

Winston Smith

Hi,

I have question about compiling multiple source files with templates
using gcc. It's probably obvious but I'm new to C++. I've tried to
isolate my problem and it came down to this.

Lets say my source files are :

=== foo.h ===

#ifndef __FOO__
#define __FOO__

template <class T>
T foo(T t);

#endif

=== foo.cc ===

#include "foo.h"

template <class T>
T foo(T t) {
return t;
}

=== main.cc ===

#include <iostream>

#include "foo.h"

using namespace std;

int main(void) {
cout << foo(0) << endl;
return 0;
}

=============

When I try to compile them with the following command I get :

$ g++ foo.cc main.cc
/tmp/ccrGrumE.o(.text+0x21): In function `main':
: undefined reference to `int foo<int>(int)'
collect2: ld returned 1 exit status


Does anyone see why ld does not seem to find the implementation of the
foo function in foo.cc?

Thanks in advance for your help.

Winston
 
J

John Harrison

Hi,

I have question about compiling multiple source files with templates
using gcc. It's probably obvious but I'm new to C++. I've tried to
isolate my problem and it came down to this.

Lets say my source files are :

=== foo.h ===

#ifndef __FOO__
#define __FOO__

template <class T>
T foo(T t);

#endif

=== foo.cc ===

#include "foo.h"

template <class T>
T foo(T t) {
return t;
}

=== main.cc ===

#include <iostream>

#include "foo.h"

using namespace std;

int main(void) {
cout << foo(0) << endl;
return 0;
}

=============

When I try to compile them with the following command I get :

$ g++ foo.cc main.cc
/tmp/ccrGrumE.o(.text+0x21): In function `main':
: undefined reference to `int foo<int>(int)'
collect2: ld returned 1 exit status


Does anyone see why ld does not seem to find the implementation of the
foo function in foo.cc?

Yes it's very simple, template definitions should go in header files.
Throw away foo.cc, put all the code in foo.h.

john
 
D

Daniel Mitchell

Hi,

I have question about compiling multiple source files with templates
using gcc. It's probably obvious but I'm new to C++. I've tried to
isolate my problem and it came down to this.

Lets say my source files are :

=== foo.h ===

#ifndef __FOO__
#define __FOO__

template <class T>
T foo(T t);

#endif

=== foo.cc ===

#include "foo.h"

template <class T>
T foo(T t) {
return t;
}

=== main.cc ===

#include <iostream>

#include "foo.h"

using namespace std;

int main(void) {
cout << foo(0) << endl;
return 0;
}

=============

When I try to compile them with the following command I get :

$ g++ foo.cc main.cc
/tmp/ccrGrumE.o(.text+0x21): In function `main':
: undefined reference to `int foo<int>(int)'
collect2: ld returned 1 exit status


Does anyone see why ld does not seem to find the implementation of the
foo function in foo.cc?

Thanks in advance for your help.

Winston

Have a look at
http://www.parashift.com/c++-faq-lite/containers-and-templates.html

Daniel
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top