Python 尝试Cython C++;例子 编辑:
我现在在macbook上尝试了相同的代码,效果很好。因此,问题仅限于我的LinuxMint17.2系统,其中包含AnacondaPython2.7、Cython0.23.4和GCC4.8。macbook使用的是AnacondaPython 2.7、Cython0.22.1和Apple LLVM版本6.1.0 原职:Python 尝试Cython C++;例子 编辑:,python,c++,cython,Python,C++,Cython,我现在在macbook上尝试了相同的代码,效果很好。因此,问题仅限于我的LinuxMint17.2系统,其中包含AnacondaPython2.7、Cython0.23.4和GCC4.8。macbook使用的是AnacondaPython 2.7、Cython0.22.1和Apple LLVM版本6.1.0 原职: 我试图从这里得到Cython C++示例: 由于有一些解释空间,我的确切代码粘贴在下面。它都在一个目录中,cpp\u示例。在cpp\u示例目录中,我使用以下方法编译: >pyt
我试图从这里得到Cython C++示例:
由于有一些解释空间,我的确切代码粘贴在下面。它都在一个目录中,cpp\u示例
。在cpp\u示例
目录中,我使用以下方法编译:
>python setup.py build_ext --inplace
编译输出以下输出:
Compiling rect.pyx because it changed.
[1/1] Cythonizing rect.pyx
running build_ext
building 'rect' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/jason/anaconda/envs/martian/include/python2.7 -c rect.cpp -o build/temp.linux-x86_64-2.7/rect.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/jason/anaconda/envs/martian/include/python2.7 -c Rectangle.cpp -o build/temp.linux-x86_64-2.7/Rectangle.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
g++ -pthread -shared -L/home/jason/anaconda/envs/martian/lib -Wl,-rpath=/home/jason/anaconda/envs/martian/lib,--no-as-needed build/temp.linux-x86_64-2.7/rect.o build/temp.linux-x86_64-2.7/Rectangle.o -L/home/jason/anaconda/envs/martian/lib -lpython2.7 -o build/lib.linux-x86_64-2.7/rect.so
然后我试着跑:
>python main.py
我得到了这个错误:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import rect
ImportError: /home/jason/projects/martian/workspaces/workspace1/cpp_example/rect.so: undefined symbol: _ZN6shapes9Rectangle9getLengthEv
矩形。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);
};
}
rect.pyx:
# 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 # 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(
"rect.pyx", # our Cython source
language="c++", # generate C++ code
))
main.py:
import rect
print rect.PyRectangle(1,1,2,2)
您可能导入了错误的库。确保
build/lib.linux-x86_64-2.7/rect.so
和/home/jason/projects/martian/workspaces/workspace1/cpp_example/rect.so
是同一件事。运行'nm-C whatever/rect.so'并验证您的函数是否存在。@n.m.成功!我删除了build目录,即in-place.so文件,重新编译,现在它可以工作了。我想我在调用setup.py时一定犯了一些错误,尽管我确信我已经经历了多次删除和重新编译的过程。我的代码现在可以工作了。如果您想添加答案,我很乐意接受。您可能导入了错误的库。确保build/lib.linux-x86_64-2.7/rect.so
和/home/jason/projects/martian/workspaces/workspace1/cpp_example/rect.so
是同一件事。运行'nm-C whatever/rect.so'并验证您的函数是否存在。@n.m.成功!我删除了build目录,即in-place.so文件,重新编译,现在它可以工作了。我想我在调用setup.py时一定犯了一些错误,尽管我确信我已经经历了多次删除和重新编译的过程。我的代码现在可以工作了。如果你想补充一个答案,我很乐意接受。
import rect
print rect.PyRectangle(1,1,2,2)