C++ Cython示例中的语法错误

C++ Cython示例中的语法错误,c++,python,cython,C++,Python,Cython,我正在尝试建立一个Cython的例子 我知道我的帖子很相似。但是,我生成了完全不同的错误消息 这是我的代码: 矩形.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() {} in

我正在尝试建立一个Cython的例子

我知道我的帖子很相似。但是,我生成了完全不同的错误消息

这是我的代码:

矩形.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;
}
矩形.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);
    };
 }
矩形.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
在阅读了上的帖子后,我意识到了这一点并将其修复

但是,当我使用下面的语句编译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

根据@Bakuriu的建议,我发现以下程序有效:

假设您使用的是命令提示符

  • cd到包含
    .pyx
    设置文件的目录
  • 使用Cython构建扩展,例如

    Cython-a rect.pyx--cplus

  • 使用Python设置扩展,例如

    Python setup.py build\u ext--inplace

  • 使用扩展时,您可以:

  • 将.pyd文件的目录附加到系统路径

    导入系统

    sys.path.append(“C:\\yourDirectory”)

  • 使用您喜欢的扩展名:)

    导入矩形

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


  • 根据@Bakuriu的建议,我发现以下程序有效:

    假设您使用的是命令提示符

  • cd到包含
    .pyx
    设置文件的目录
  • 使用Cython构建扩展,例如

    Cython-a rect.pyx--cplus

  • 使用Python设置扩展,例如

    Python setup.py build\u ext--inplace

  • 使用扩展时,您可以:

  • 将.pyd文件的目录附加到系统路径

    导入系统

    sys.path.append(“C:\\yourDirectory”)

  • 使用您喜欢的扩展名:)

    导入矩形

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


  • python rectangle.pyx
    python不知道什么是
    .pyx
    文件。首先必须构建扩展,然后将其导入python程序。如果建筑失败,你应该公布你得到的全部结果。不仅仅是一行。您也可以尝试直接调用cython构建(请参阅)。[我建议阅读该链接,因为它还解释了如何生成一个HTML文件,该文件可以告诉您cython代码的优化位置]
    python rectangle.pyx
    python不知道
    .pyx
    文件是什么。首先必须构建扩展,然后将其导入python程序。如果建筑失败,你应该公布你得到的全部结果。不仅仅是一行。您也可以尝试直接调用cython构建(请参阅)。[我建议阅读该链接,因为它还解释了如何生成一个HTML文件,该文件可以告诉您cython代码的优化位置]