2013-10-07 29 views
0

我想在this page上構建一個Cython示例。Cython示例中的語法錯誤

我知道我的帖子非常類似於其他question。但是,我生成完全不同的錯誤消息。

這裏是我的代碼:

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

我不得不承認,我有同樣的錯誤,在第一個地方缺少rectangle.pyx以下行。

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

在閱讀here的帖子後,我意識到並修復了它。

然而,當我用下面的語句來編譯C++類,

python rectangle.pyx 

我以下錯誤消息:

File "rectangle.pyx", line 4 
    cdef extern from "Rectangle.h" namespace "shapes": 
      ^
SyntaxError: invalid syntax 

爲什麼這個錯誤彈出?我可以知道如何解決它嗎?

非常感謝。 :)

============================================ =======

PS:當我嘗試運行setup.py,我有一個g++錯誤:

我跑:

python setup.py build_ext 

g++誤差

error: command 'g++' failed with exit status 1 
+2

'python rectangle.pyx' python不知道'.pyx'文件是什麼。您首先必須構建擴展,然後將其導入到python程序中。如果建築物失效,您應該發佈您獲得的*全部*輸出。不只是一條線。您也可以嘗試直接構建調用cython(請參閱[this](http://docs.cython.org/src/reference/compilation.html#compiling-from-the-command-line))。 [我建議閱讀該鏈接,因爲它還解釋瞭如何生成一個HTML文件,它可以告訴你哪些cython代碼可以優化] – Bakuriu

回答

0

根據@ Bakuriu的建議,我發現了以下程序的工作原理:

假設你正在使用命令提示符

  1. cd到包含.pyxsetup文件的目錄。使用Python設置了擴展使用用Cython構建擴展
  2. ,如

    Cython -a rect.pyx --cplus

  3. ,如

    Python setup.py build_ext --inplace

當使用擴展,可以:

  1. 追加.pyd文件的目錄添加到系統路徑

    import sys

    sys.path.append("C:\\yourDirectory")

  2. 使用擴展,只要你喜歡:)

    import Rectangle

    r = Rectangle.PyRectangle(1,2,3,4)