Dynamic libraries and templates

coss

Registered
Dear all,

I'm trying to port a dynamic library in C++ to MAC OS X. This library makes intensive use of templates. The library can be built but I cannot get the programs compile with it. The smaller program that reproduces this effect is the following:

module1.hh:
#include <iostream.h>
template <class T>
class ClassA {
T data;
public:
ClassA() {cout << "Creating A" << endl;}
void metA();
};

module1.cc:
#include "module1.hh"

template <class T>
void ClassA<T>::metA() {cout << "MetA " << endl;}

template <class T>
void instantiateT(T a) {
ClassA<T> A;
A.metA();
}

void instantiate() {
double d; instantiateT(d);
}

main.cc:
#include "module1.hh"
int main() {
ClassA<double> a;
a.metA();
return 0;
}

The library is built as
g++ -fno-common -c module1.cc
g++ -dynamiclib -flat_namespace -undefined suppress -o libModules.dylib module1.o

and the main program compiled as
g++ -c main.cc
g++ -o main1 -L. main.o -lModules

The result is:
ld: Undefined symbols:
ClassA<double>::metA()

Although if I build the program as
g++ -c main.cc
g++ -o main2 main.o module1.o

then there is no problem. It seems to me that I'm not giving the right flags to the program linking or the library construction. Does anybody know what I am doing wrong?

Thanks beforehand, Carlos Oscar
 
Back
Top