我正在用Cython包裝C++庫,我想將它作爲Python包分發。我一直在使用this tutorial作爲指導。使用Python包捆綁Cython模塊
以下是事情的組織方式。
.
├── inc
│ └── Rectangle.h
├── rect
│ ├── __init__.py
│ └── wrapper.pyx
├── setup.py
└── src
└── Rectangle.cpp
我貼這些文件的內容在文章的底部,以及在此GitHub repo。
我沒有問題編譯和安裝與python setup.py install
,我可以import rect
從解釋器沒有問題。但它似乎是一個空的類:我無法用以下任何一種方法創建一個Rectangle
對象。 - Rectangle
- rect.Rectangle
- wrapper.Rectangle
- rect.wrapper.Rectangle
我在做什麼錯在這裏?
Rectangle.h
的內容,從本教程複製並粘貼。
namespace shapes {
class Rectangle {
public:
int x0, y0, x1, y1;
Rectangle();
Rectangle(int x0, int y0, int x1, int y1);
~Rectangle();
int getArea();
void getSize(int* width, int* height);
void move(int dx, int dy);
};
}
Rectangle.cpp
的內容。
#include "Rectangle.h"
namespace shapes {
Rectangle::Rectangle() { }
Rectangle::Rectangle(int X0, int Y0, int X1, int Y1) {
x0 = X0;
y0 = Y0;
x1 = X1;
y1 = Y1;
}
Rectangle::~Rectangle() { }
int Rectangle::getArea() {
return (x1 - x0) * (y1 - y0);
}
void Rectangle::getSize(int *width, int *height) {
(*width) = x1 - x0;
(*height) = y1 - y0;
}
void Rectangle::move(int dx, int dy) {
x0 += dx;
y0 += dy;
x1 += dx;
y1 += dy;
}
}
Cython包裝代碼wrapper.pyx
。
# distutils: language = c++
# distutils: sources = src/Rectangle.cpp
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
Rectangle() except +
Rectangle(int, int, int, int) except +
int x0, y0, x1, y1
int getArea()
void getSize(int* width, int* height)
void move(int, int)
cdef class PyRectangle:
cdef Rectangle c_rect # hold a C++ instance which we're wrapping
def __cinit__(self, int x0, int y0, int x1, int y1):
self.c_rect = Rectangle(x0, y0, x1, y1)
def get_area(self):
return self.c_rect.getArea()
def get_size(self):
cdef int width, height
self.c_rect.getSize(&width, &height)
return width, height
def move(self, dx, dy):
self.c_rect.move(dx, dy)
這個setup.py
腳本我已經適應這個文件組織。
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(
name='rect',
packages=['rect'],
ext_modules=cythonize(Extension(
'Rectangle',
sources=['rect/wrapper.pyx', 'src/Rectangle.cpp'],
include_dirs=['inc/'],
language='c++',
extra_compile_args=['--std=c++11'],
extra_link_args=['--std=c++11']
)),
)
你看過[Thearn的簡單的cython示例](https://github.com/thearn/simple-cython-example)。部分看起來像你的'setup.py'可能有問題,因爲你的'Extension'爲'Rectangle',導致在'python/site-packages'中創建了'Rectangle.so'。你也會想改變你的'__init __。py',這樣它就可以從你的'Rectangle.so/wrapper'中導入你的類,並且將這個擴展名稱作爲'rect.Rectangle'或者其他的東西。 –