[C++-sig] Help on Template Instantiation

Natsu natsu_mizu_99 at yahoo.co.jp
Thu Feb 26 15:07:40 CET 2004


Dear list members,

I have a question on template instantiation, 
probably it's rather a C++ issue.

My codes are organized as follows.

- 'field.h', a header file. 
- 'field.cpp: c++ codes using templates.
- 'myfield.cpp', boost interface.

After making 'myfield.so' and imported from python module, I got 
undefined symbol error. 

I assume it is related to instantiation of the template, 
for the error dissappears when I add a dummy main() function 
and call the template class field<vect> and its member functions.

Is my understanding correct? 
If it is correct,  what is the best recommended way to instantiate 
those class using templates, or should I modify the code structure?

I use gcc-2.95.3, Boost-1.31.0, Python-2.2.1, on Red Hat 7.2.

Thanks in advance

Natsu

// field.h
#include <vector>

struct vect{
    double x_ ;
    double y_ ;
    double z_ ;
 public:
    vect();
    ~vect(){};
};


template<class T> class field {
 protected:
    T body_;
    unsigned int  nx_;
 public:
    field(int nx);
    ~field(){};
    unsigned int getsize(); 
};
// End of field.h
// field.cpp
#include <vector>
#include "field.h"

template<class T> 
field<T>::field(int nx): body_(T()), nx_(nx){}

template<class T> 
unsigned int field<T>::getsize()  {return nx_;}

vect::vect() : x_(0.0), y_(0.0), z_(0.0) {}
/*
int main() {
    field<vect> f0(0);
    f0.getsize();
    return 0;
}
*/
// End of field.cpp
// myfield.cpp
#include <boost/python.hpp>
#include <boost/cstdint.hpp>

#include <vector>
#include <field.h>

using namespace boost::python;

BOOST_PYTHON_MODULE(myfield)
{
    class_< field<vect> >("field_vect", init< int >())
        .def("getsize", &field<vect>::getsize)
    ;

    class_< vect >("vect", init<  >())
    ;
}
// End of myfield.cpp
// Makefile
INCLUDE = -I. -I/usr/include/python2.2
CFLAGS  = -Wall -O  -ftemplate-depth-100
CFLAG2  = -fPIC -shared
LFLAGS  = -L/usr/local/lib/boost -lboost_python-gcc
CPP     = /usr/local/gcc-2.95.3/bin/g++
TGT     = myfield
OBJ     = field.o

${TGT}.so: ${TGT}.o ${OBJS}
	${CPP} -o $@ ${TGT}.o ${OBJS} ${LFLAGS}

.cpp.o:
	${CPP} ${CFLAGS} ${CFLAG2} ${INCLUDE} -o $@ -c $<

${TGT}.o:  field.h
field.o:   field.h
// End of Makefile


$ python2
Python 2.2.1 (#1, Jan 22 2003, 19:07:20)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myfield
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: ./myfield.so: undefined symbol: getsize__t5field1Z4vect
>>>




__________________________________________________
Do You Yahoo!?
http://bb.yahoo.co.jp/





More information about the Cplusplus-sig mailing list