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