python - Cython c++ example fails to recognize c++, why? -
i'm attempting build example 'using c++ in cython' @ the cython c++ page, setup appears not recognize language, c++.
the files, taken same page are:
rectangle.cpp
#include "rectangle.h" using namespace shapes; rectangle::rectangle(int x0, int y0, int x1, int y1){ x0 = x0; y0 = y0; x1 = x1; y1 = y1; } rectangle::~rectangle() {} int rectangle::getlength() { return (x1 - x0); } int rectangle::getheight() { return (y1 - y0); } int rectangle::getarea() { return (x1 - x0) * (y1 - y0); } void rectangle::move(int dx, int dy) { x0 += dx; y0 += dy; x1 += dx; y1 += dy; }
rectangle.h:
namespace shapes { class rectangle { public: int x0, y0, x1, y1; rectangle(int x0, int y0, int x1, int y1); ~rectangle(); int getlength(); int getheight(); int getarea(); void move(int dx, int dy); }; }
rectangle.pyx
cdef extern "rectangle.h" namespace "shapes": cdef cppclass rectangle: rectangle(int, int, int, int) int x0, y0, x1, y1 int getlength() int getheight() int getarea() void move(int, int) cdef class pyrectangle: cdef rectangle *thisptr # hold c++ instance 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 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)
setup.py
from distutils.core import setup cython.build import cythonize setup(ext_modules = cythonize( "rectangle.pyx", # our cython source sources=["rectangle.cpp"], # additional source file(s) language="c++", # generate c++ code ))
i attempt build with:
python setup.py --build_ext --inplace
but fails, error "operation allowed in c++" suggesting language="c++" option not being passed correctly.
cython rectangle.pyx --cplus
does produce c++ file, hoping able setup.py method work.
the entire error text is:
cythonizing rectangle.pyx error compiling cython file: ------------------------------------------------------------ ... void move(int, int) cdef class pyrectangle: cdef rectangle *thisptr # hold c++ instance we're wrapping def __cinit__(self, int x0, int y0, int x1, int y1): self.thisptr = new rectangle(x0, y0, x1, y1) ^ ------------------------------------------------------------ rectangle.pyx:13:27: operation allowed in c++ error compiling cython file: ------------------------------------------------------------ ... cdef class pyrectangle: cdef rectangle *thisptr # hold c++ instance 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 self.thisptr ^ ------------------------------------------------------------ rectangle.pyx:15:8: operation allowed in c++ traceback (most recent call last): file "setup.py", line 8, in <module> language="c++", # generate c++ code file "/library/python/2.7/site-packages/cython-0.19.1-py2.7-macosx-10.8intel.egg/cython/build/dependencies.py", line 753, in cythonize cythonize_one(*args[1:]) file "/library/python/2.7/site-packages/cython-0.19.1-py2.7-macosx-10.8intel.egg/cython/build/dependencies.py", line 820, in cythonize_one raise compileerror(none, pyx_file) cython.compiler.errors.compileerror: rectangle.pyx
following example exactly, on similar system yours, works fine me.
but, along few other differences (an indentationerror, naming file rectangle.pyx
instead of rect.pyx
, …) turned out not relevant problem, left off @ top of cython file:
# distutils: language = c++ # distutils: sources = rectangle.cpp
and if leave off, same error.
Comments
Post a Comment