Cython c++示例失败了,原因是什么
Cython c++ example fails, why?
当我尝试在http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html,我在mac终端上得到了以下错误:
Error compiling Cython file:
distutils: language = c++
distutils: sources = Rectangle.cpp
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
rect.pyx:5:0: Expected an increase in indentation level
Traceback (most recent call last):
File "setup.py", line 8, in <module>
language="c++", # generate and compile C++ code
File "/usr/local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 877, in cythonize
cythonize_one(*args)
File "/usr/local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 997, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: rect.pyx
我不确定我的代码出了什么问题。你能告诉我为什么收到错误信息吗?
我的机器中有Cython 0.23.4和Python 2.7.10。我运行python/c++代码没有任何问题。我还在电脑上安装了boost和boost python。
简单地说,我创建了以下文件:
1-矩形.h
2-矩形.cpp
3套
4-rect.pyx
然后,我做了"python setup.py build_ext--inplace"
矩形.h
#include <stdio.h>
namespace shapes {
class Rectangle {
public:
int x0, y0, x1, y1;
Rectangle(int x0, int y0, int x1, int y1);
~Rectangle();
int getLength() const;
int getHeight() const;
int getArea() const;
void move(int dx, int dy);
};
}
矩形.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() const {
return (x1 - x0);
}
int Rectangle::getHeight() const {
return (y1 - y0);
}
int Rectangle::getArea() const {
return getLength() * getHeight();
}
void Rectangle::move(int dx, int dy) {
x0 += dx;
y0 += dy;
x1 += dx;
y1 += dy;
}
设置.py
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(ext_modules = cythonize(Extension(
"rect", # the extesion name
sources=["rect.pyx", "Rectangle.cpp"], # the Cython source and
# additional C++ source files
language="c++", # generate and compile C++ code
)))
我也尝试了以下方法:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(
"rect.pyx", # our Cython source
sources=["Rectangle.cpp"], # additional source file(s)
language="c++", # generate C++ code
))
矩形pyx
# distutils: language = c++
# distutils: sources = Rectangle.cpp
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
Rectangle(int, int, int, int) except +
int x0, y0, x1, y1
int getLength() const
int getHeight() const
int getArea() const
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)
非常感谢,swenzel!现在,它起作用了!
矩形pyx
# distutils: language = c++
# distutils: sources = Rectangle.cpp
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
Rectangle(int, int, int, int) except +
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)
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- 指示 Qi 变换属性失败的适当方法是什么?
- 显式实例化失败的原因是什么
- QtDCM 编译失败的可能原因是什么?
- 错误 opencv 错误断言失败 (p [-1] <= 2) 是什么意思以及如何处理它?
- 当我尝试caputre〔this〕时,用于回调函数的lambda表达式失败,原因是什么
- 打破gdb中失败断言的正确方法是什么
- Cython c++示例失败了,原因是什么