[Pyrex] The newbie's question about wrapping c++ class with pyrex
Robert Bradshaw
robertwb at math.washington.edu
Fri Apr 18 06:12:58 CEST 2008
On Apr 17, 2008, at 6:47 PM, Yong Sun wrote:
> Hi, experts,
>
> I am following the tutorial at http://wiki.cython.org/
> WrappingCPlusPlus, and found a minor issue,
>
> cdef class Rectangle:
> - c_Rectangle *thisptr # hold a C++ instance which we're wrapping
> + cdef c_Rectangle *thisptr # hold a C++ instance which we're
> wrapping
Thanks. I've made this change on the Cython wiki. I also corrected an
error about operator overloading.
I'd like to add this example to the automated testing infrastructure,
but it seems unclear how to do so with the current runtests.py.
>
> I need the above change to make it successfully compiled by cython.
> While, when I tried to compile it with pyrexc, I met following errors,
>
> $ pyrexc --cplus rect.pyx
> rect.pyx:4:21: Non-extern C function declared but not defined
> rect.pyx:5:21: Non-extern C function declared but not defined
> rect.pyx:6:19: Non-extern C function declared but not defined
> rect.pyx:7:17: Non-extern C function declared but not defined
> rect.pyx:18:27: Object of type 'c_Rectangle' has no attribute
> 'getLength'
> rect.pyx:20:27: Object of type 'c_Rectangle' has no attribute
> 'getHeight'
> rect.pyx:22:27: Object of type 'c_Rectangle' has no attribute
> 'getArea'
> rect.pyx:24:20: Object of type 'c_Rectangle' has no attribute 'move'
>
> BTW, I am using the latest stable version,
>
> $ pyrexc --version
> Pyrex version 0.9.6.4
In Pyrex you need to declare functions as function pointers, i.e.
cdef extern from "Rectangle.h":
ctypedef struct c_Rectangle "Rectangle":
int x0, y0, x1, y1
(int *getLength)()
...
>
> Thank you very much!
>
> Regards,
>
>
>
> cdef extern from "Rectangle.h":
> ctypedef struct c_Rectangle "Rectangle":
> int x0, y0, x1, y1
> int getLength()
> int getHeight()
> int getArea()
> void move(int dx, int dy)
> c_Rectangle *new_Rectangle "new Rectangle" (int x0, int y0, int
> x1, int y1)
> void del_Rectangle "delete" (c_Rectangle *rect)
>
> cdef class Rectangle:
> cdef c_Rectangle *thisptr # hold a C++ instance which
> we're wrapping
> def __cinit__(self, int x0, int y0, int x1, int y1):
> self.thisptr = new_Rectangle(x0, y0, x1, y1)
> def __dealloc__(self):
> del_Rectangle(self.thisptr)
> def getLength(self):
> return self.thisptr.getLength()
> def getHeight(self):
> return self.thisptr.getHeight()
> def getArea(self):
> return self.thisptr.getArea()
> def move(self, dx, dy):
> self.thisptr.move(dx, dy)
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex
More information about the Pyrex
mailing list