templating in multiple files

Eagle trusteagle@tiscali.it
Thu Nov 4 15:40:00 GMT 2004


Hi all,
As my understanding this should be the correct mailing list for this issue.
I'm porting from VC6 to gcc a set of numerical software, and I did came 
across an issue
which I reproduced in the following simple test case:

file Main.cpp
#include "template.h"

int main(){
   int joe = 2;
   int boe = 3;
   joe::parse(joe,boe);
   double doe= 2.3e14;
   joe::parse(joe,doe);
   return 0;
}

file template.cpp:

#include "template.h"
#include <iostream>

using namespace std;

template <class T> void joe::parse(int joe, T value){
   cout << "<" << joe << "><" << value << ">"<<endl;
}


file template.h:

namespace joe {
   template <class T> void parse(int joe,T value);
}

When I compile it I do have the following:

[diego@persefone t2]$ g++ template.cpp Main.cpp
/tmp/ccEZpSk7.o: In function `main':
/tmp/ccEZpSk7.o(.text+0x38): undefined reference to `void 
joe::parse<int>(int, int)'
/tmp/ccEZpSk7.o(.text+0x53): undefined reference to `void 
joe::parse<double>(int, double)'
collect2: ld returned 1 exit status
[diego@persefone t2]$

The fact is that if all the code is sticked into one file it works fine.

Questions are:
To me it looks perfectly standard ANSI, as it works in one file It should 
work in multiple files, correct ?
Is there any specific compiler option I have to use ?
Is there any compiler solicitation code I have to use ? for example in 
MSVC6 there is an explicit declaration of the types used  as:
namespace joe {
         template <class T> void parse(int joe,T value);
         template void parse(int joe , int value);
}



Thanks a lot,
Diego




More information about the Libstdc++ mailing list