On Thu, Nov 27, 2014 at 7:45 PM, Jonathan Hanke <
jonh...@gmail.com> wrote:
> Hi,
>
> I'm trying to wrap a simple templated C++ class example in Cython that uses
> the template to determine the datatype of an underlying vector. I used the
> vector templating code from the example in the Cython documentation (which
> seems important to include in the .pyx file), and tried to do something
> similar.
>
> Here the C++ class is "Thing" and the Cython class wrapping it is
> "PyThingINT". The error appears when I try to import the class into python
> with "from mything import PyThingINT", giving
>
> ImportError:
> dlopen(/Users/jonhanke/Desktop/Cython_template_example__2014-11-27/mything.so,
> 2): Symbol not found: __ZN7MySpace5ThingIiEC1El
> Referenced from:
> /Users/jonhanke/Desktop/Cython_template_example__2014-11-27/mything.so
> Expected in: flat namespace
> in /Users/jonhanke/Desktop/Cython_template_example__2014-11-27/mything.so
>
>
> I've included the files below. Any comments are appreciated. Thanks!
> # distutils: language = c++
> # distutils: sources = ['thing.cpp']
> # distutils: include_dirs = ['/usr/local/include']
> # distutils: libraries = []
> # distutils: library_dirs = ['/usr/local/lib']
>
> from libcpp cimport long
Note that you don't need to cimport long.
> cdef extern from "<vector>" namespace "std":
> cdef cppclass vector[T]:
> cppclass iterator:
> T operator*()
> iterator operator++()
> bint operator==(iterator)
> bint operator!=(iterator)
> vector()
> void push_back(T&)
> T& operator[](int)
> T& at(int)
> iterator begin()
> iterator end()
On the other hand, you don't have to declare this youreself. Just do
from libcpp.vector cimport vector
> cdef extern from "thing.h" namespace "MySpace":
> cdef cppclass Thing[T]:
> Thing(const long num) except +
>
> cdef class PyThingINT:
> cdef Thing[int] *thisptr
> def __cinit__(self, prec):
> self.thisptr = new Thing[int](<long> prec)
> def __dealloc__(self):
> del self.thisptr
The cast to <long> is unneeded, it will be done implicitly due to the
type of the argument.
- Robert