C++ cythonc++;示例无法识别c++;,为什么?
我试图构建一个例子,用于“在Cython中使用C++”,但是安装程序似乎不识别语言,C++。p> 取自同一页的文件包括: 矩形.cppC++ cythonc++;示例无法识别c++;,为什么?,c++,python,cython,C++,Python,Cython,我试图构建一个例子,用于“在Cython中使用C++”,但是安装程序似乎不识别语言,C++。p> 取自同一页的文件包括: 矩形.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()
#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
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++文件,但我希望能够使StupU.Py方法工作。< /P> 整个错误文本为:
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
Cythonizing rectangle.pyx
编译Cython文件时出错:
------------------------------------------------------------
...
无效移动(int,int)
cdef类PyRectangle:
CDEF矩形*THPTR占卜持有一个C++实例,我们正在包装
定义(自身、整数x0、整数y0、整数x1、整数y1):
self.thisptr=新矩形(x0,y0,x1,y1)
^
------------------------------------------------------------
矩形:Pyx:13:27:C++中只允许操作
编译Cython文件时出错:
------------------------------------------------------------
...
cdef类PyRectangle:
CDEF矩形*THPTR占卜持有一个C++实例,我们正在包装
定义(自身、整数x0、整数y0、整数x1、整数y1):
self.thisptr=新矩形(x0,y0,x1,y1)
def uu dealoc uu(自我):
del self.thisptr
^
------------------------------------------------------------
矩形:Pyx:15:8:C++中只允许操作
回溯(最近一次呼叫最后一次):
文件“setup.py”,第8行,在
语言=“C++”,生成C++代码
cythonize中的文件“/Library/Python/2.7/site packages/Cython-0.19.1-py2.7-macosx-10.8intel.egg/Cython/Build/Dependencies.py”,第753行
cythonize_one(*args[1:]
文件“/Library/Python/2.7/site packages/Cython-0.19.1-py2.7-macosx-10.8intel.egg/Cython/Build/Dependencies.py”,第820行,在cythonize_one中
raise编译器错误(无,pyx_文件)
Cython.Compiler.Errors.CompileError:rectangle.pyx
完全按照这个例子,在一个与您非常相似的系统上,对我来说一切都很好
但是,除了与此问题无关的一些其他差异(一个缩进错误,将文件命名为rectangle.pyx
,而不是rect.pyx
,…)之外,您还在Cython文件的顶部删除了以下内容:
# distutils: language = c++
# distutils: sources = Rectangle.cpp
如果我不这样做,我会得到同样的错误。作为旁注,将一个文件命名为
Rectangle.cpp
,然后命名另一个文件,这样它将编译为Rectangle.cpp
。我不认为这是你的问题,但它肯定使事情更混乱,至少创造了潜在的问题(特别是如果你想在Windows上工作)。我同意,但我采取了这些文件完全逐字从Cython C++教程,认为他们将保证是正确的。我正处于如何编译Cython的阶段。不,Cython C++教程有<代码> Rec.Pyx,而不是<代码>矩形> Pyx,正是因为这个原因。此外,当我们进行此操作时,您的Cython模块中有一个缩进错误(cppclass需要缩进到extern下);我可以假设您的真实代码没有吗?另外,python setup.py--build\u ext--inplace
不是有效的生成行。我认为您希望使用python setup.py build\u ext--inplace
,将build\u ext
作为设置命令而不是选项。最后,示例在rect.pyx
顶部的注释中设置语言和源代码,而您的代码没有这样做。这似乎是你真正的问题,不像上面所有的其他事情。