c++ - Syntax Error in Cython Example -
i trying build cython example @ this page.
i know post similar 1 other question. however, generated totally different error messages.
here code:
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
# distutils: language = c++ # distutils: sources = rectangle.cpp 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 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 have admit had same mistake missing following lines in rectangle.pyx @ first place.
# distutils: language = c++ # distutils: sources = rectangle.cpp after read through post @ here, realized , fixed it.
however, when use following statement compile c++ class,
python rectangle.pyx i had following error message:
file "rectangle.pyx", line 4 cdef extern "rectangle.h" namespace "shapes": ^ syntaxerror: invalid syntax why error pops up? may know how fix it?
many thanks. :)
===================================================
ps: when try run setup.py, had g++ error:
i ran:
python setup.py build_ext and g++ error
error: command 'g++' failed exit status 1
according @bakuriu 's advice, found following procedure works:
suppose using command prompt
- cd directory contains
.pyx,setupfiles. using cython build extension, such
cython -a rect.pyx --cplususing python setup extension, such as
python setup.py build_ext --inplace
when using extension, can:
append directory of .pyd file system path
import syssys.path.append("c:\\yourdirectory")using extension :)
import rectangler = rectangle.pyrectangle(1,2,3,4)
Comments
Post a Comment