我已經完全複製了Cython documentation for wrapping C++ classes中給出的示例代碼。我可以成功地建立和導入使用distutils
和cythonize()
方法rect.so
延伸,通過即:如何手動編譯使用C++的Cython代碼?
# distutils: language = c++
# distutils: sources = Rectangle.cpp
寫setup.py
文件,其中包含:
在
rect.pyx
頂部把下面的指令這個:from distutils.core import setup from Cython.Build import cythonize setup( name = "rectangleapp", ext_modules = cythonize('*.pyx'), )
打電話
$ python setup.py build_ext --inplace
然而,當我包裝C代碼用Cython我經常發現它更方便手動命令行編譯單個擴展,即:
生成
.c
代碼使用命令行Cython編譯器$ cython foo.pyx
使用手動編譯它:
$ gcc -shared -fPIC -O3 -I /usr/lib/python2.7 -L /usr/lib/python2.7 \ foo.c -lpython2.7 -o foo.so
我已經嘗試採用同樣的工藝打造的rect.so
上面的例子:
$ cython --cplus rect.pyx
$ g++ -shared -fPIC -O3 -I /usr/lib/python2.7 -L /usr/lib/python2.7 \
rect.cpp -lpython2.7 -o rect.so
無論是用Cython和g ++編譯步驟似乎成功 - 我不明白任何命令行輸出,並在最後我有一個rect.so
內置。然而,當我再嘗試導入模塊我得到一個錯誤undefined symbol
:
In [1]: import rect
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-ba16f97c2145> in <module>()
----> 1 import rect
ImportError: ./rect.so: undefined symbol: _ZN6shapes9Rectangle9getLengthEv
什麼是手動編譯一個封裝C++類用Cython代碼正確的程序?