2013-08-09 11 views
5

Próbuję zbudować na przykładzie "przy użyciu C++ w cython" na the Cython C++ page, ale instalacja wydaje się nie rozpoznać języka, C++.C++ na przykładzie C++ nie rozpoznaje C++, dlaczego?

Pliki, wzięte z tej samej stronie są:

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 from "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 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 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 
from 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 
    )) 

staram się budować to z:

python setup.py --build_ext --inplace 

Ale to się nie powiedzie, z błędem "Operacja dozwolona tylko w C++", co sugeruje, że opcja language = "C++" nie jest poprawnie przekazywana.

cython rectangle.pyx --cplus 

tworzy plik C++, ale miałem nadzieję, że uda mi się uruchomić metodę setup.py.

Cały tekst błędu:

Cythonizing rectangle.pyx 

Error compiling Cython file: 
------------------------------------------------------------ 
... 
     void move(int, int) 

cdef class PyRectangle: 
    cdef 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) 
        ^
------------------------------------------------------------ 

rectangle.pyx:13:27: Operation only allowed in c++ 

Error compiling Cython file: 
------------------------------------------------------------ 
... 
cdef class PyRectangle: 
    cdef 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 self.thisptr 
    ^
------------------------------------------------------------ 

rectangle.pyx:15:8: Operation only 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 
+0

Na marginesie, naprawdę nie jest dobrym pomysłem wywołanie jednego pliku 'Rectangle.cpp', a następnie nadanie innego pliku, aby skompilował się do' rectangle.cpp'. Nie sądzę, że to jest twój problem, ale z pewnością sprawia, że ​​rzeczy stają się bardziej zagmatwane, a przynajmniej stwarza potencjalne problemy (szczególnie, jeśli chcesz pracować w systemie Windows). – abarnert

+0

Zgadzam się, ale wziąłem te pliki dokładnie słowo w słowo z tutoriala C++ C++, myśląc, że będą one gwarantowane. Właśnie jestem na etapie ustalania, jak skompilować Cythona. –

+0

Nie, tutorial Cython C++ ma właśnie 'rect.pyx', a nie' rectangle.pyx', właśnie z tego powodu. Ponadto, gdy jesteśmy przy tym, twój moduł Cython ma w sobie wyjątek wcięcia (cppclass musi być wcięty pod zewnętrzną wersją); czy mogę założyć, że twój prawdziwy kod nie działa? – abarnert

Odpowiedz

9

Idąc za przykładem dokładnie, w bardzo podobny system do Ciebie, wszystko działa dobrze dla mnie.

Jednak, wraz z kilkoma innymi różnice (AN IndentationError, nazywając plik rectangle.pyx zamiast rect.pyx ...), który okazał się nie być odpowiednie do tego problemu, można również skończył to u góry pliku Cython:

# distutils: language = c++ 
# distutils: sources = Rectangle.cpp 

I jeśli zostawiam to, otrzymam ten sam błąd.

Powiązane problemy