2013-08-09 17 views
5

私は、 'cthon in cythonを使用する'の例をthe Cython C++ pageに構築しようとしていますが、setupが言語を認識しないようです。CythonのC++の例でC++を認識できない、なぜですか?

同じページから取られたファイルは、以下のとおりです。

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

python setup.py --build_ext --inplace 

しかし、これは失敗します。 "Operation only allowed in C++"は、language = "C++"オプションが正しく渡されないことを示唆しています。

cython rectangle.pyx --cplus 

は、C++ファイルを生成しますが、私はsetup.pyメソッドを動作させたいと考えていました。

全体のエラーテキストがある:

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

ひとつのファイル 'Rectangle.cpp'を呼び出して、別のファイルを' rectangle.cpp'にコンパイルするように名前を付けるのは、本当に良い考えではありません。私はそれがあなたの問題だとは思わないが、明らかに物事を混乱させ、少なくとも問題の可能性を生む(特にWindowsで作業したい場合)。 – abarnert

+0

私は同意しますが、これらのファイルをCythonのC++チュートリアルの単語をそのまま使って、正しいことが保証されていると考えています。私はちょうどCythonをコンパイルする方法を考え出す段階にあります。 –

+0

いいえ、Cython C++チュートリアルには、正確にはこの理由で 'rectanglex 'ではなく' rectangularx'があります。また、CythonモジュールにはIndentationErrorがあります(externの下にインデントされる必要があります)。あなたの本当のコードはそうではないと思いますか? – abarnert

答えて

9

はまさに例に続いて、あなたに非常によく似たシステムで、すべてが私のために正常に動作します。

しかし、この問題に関連しないことが判明した他のいくつかの相違点(IndentationError、 ...の代わりにrectangle.pyxという名前のファイル)と一緒に、Cythonファイルの最上部にこれを残しました。

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

私がオフにしても、同じエラーが発生します。

関連する問題