如何手动编译使用C++的Cython代码?

6

我已经完全复制了Cython文档中关于封装C++类的示例代码。我可以通过使用distutilscythonize()方法成功构建并导入rect.so扩展,即:

  1. Putting the following directives at the top of rect.pyx:

    # distutils: language = c++
    # distutils: sources = Rectangle.cpp
    
  2. Writing a setup.py file that contains this:

    from distutils.core import setup
    from Cython.Build import cythonize
    
    setup(
        name = "rectangleapp",
        ext_modules = cythonize('*.pyx'),
    )
    
  3. Calling

    $ python setup.py build_ext --inplace
    

然而,当我在Cython中包装C代码时,我经常发现手动从命令行编译单个扩展更加方便,即:

  1. Generate the .c code using the command line Cython compiler

    $ cython foo.pyx
    
  2. Manually compile it using gcc:

    $ 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代码的正确步骤?

1个回答

7
这里的问题在于你说过会提供名为“矩形(Rectangle)”的类的定义,但是示例代码中没有。请注意,示例代码中只是使用了这个类名字而已。
cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
        ...

然而,当您编译库时,您没有提供Rectangle的代码或包含它的库,因此rect.so不知道在哪里找到这个Rectangle类。

要运行您的代码,您必须首先创建Rectangle对象文件。

gcc -c Rectangle.cpp # creates a file called Rectangle.o

现在你可以创建一个库来动态链接,或者将对象文件静态链接到rect.so中。我将先讲解静态链接,因为它最简单。
gcc -shared -fPIC -I /usr/include/python2.7 rect.cpp Rectangle.o -o rect.so

请注意,我没有包含Python库。这是因为您期望您的库由Python解释器加载,因此当加载您的库时,进程将已经加载Python库。除了提供rect.cpp作为源代码外,我还提供Rectangle.o。现在让我们尝试使用您的模块运行程序。 run.py
import rect
print(rect.PyRectangle(0, 0, 1, 2).getLength())

很不幸,这会产生另一个错误:
ImportError: /home/user/rectangle/rect.so undefined symbol: _ZTINSt8ios_base7failureE

这是因为Cython需要C++标准库,但Python没有加载它。您可以通过将C++标准库添加到rect.so所需的库中来解决此问题。
gcc -shared -fPIC -I/usr/include/python2.7 rect.cpp Rectangle.o -lstdc++ \
     -o rect.so

再次运行run.py,一切应该都能正常工作。然而,rect.so的代码比它实际需要的要大,特别是当您生成多个依赖于相同代码的库时。您可以将矩形代码动态链接,使其成为一个库。

gcc -shared -fPIC Rectangle.o -o libRectangle.so
gcc -shared -fPIC -I/usr/include/python2.7 -L. rect.cpp -lRectangle -lstdc++ \
     -o rect.so

我们将矩形代码编译成一个共享库,并放置在当前目录中,提供-L.参数以便gcc知道在当前目录中查找库文件,同时提供-lRectangle参数以便gcc知道要查找矩形库。最后,在运行代码之前,您必须告诉Python矩形库的位置。在运行Python之前,请输入以下命令:
export LD_LIBRARY_PATH="/home/user/rectangle" # where libRectangle.so lives

你可以使用shell脚本来确保每次在运行程序之前都完成这个步骤,但会使事情变得更加混乱。最好还是坚持使用静态链接Rectangle。

我正在尝试在Windows上重复这些步骤,但无法使其工作。这是我的步骤:
  1. cython --cplus rect.pyx -> rect.cpp
  2. gcc -c Rectangle.cpp -> Rectangle.o
  3. gcc -shared -fPIC -IC:\Users\python37\include rect.cpp Rectangle.o -lstdc++ -o -rect.so
- A.P.
但是第三步失败了,并出现了许多形如“undefined reference to”的消息,例如“undefined reference to `__imp_PyDict_Size'”。有任何想法吗? - A.P.
1
我相信Windows DLL和Unix so文件的创建方式不同。因此,您应添加标志“-L C:\ Users \ python37 \ libs -l python3”。请参见https://docs.python.org/3/extending/windows.html#differences-between-unix-and-windows。不要引用我说的话,我很少在Windows上使用C / C ++。 - Dunes
通过添加您的建议,使用以下命令编译成功且无错误: gcc -shared -fPIC -IC:\Users\python37\include -LC:\Users\python37\libs rect.cpp Rectangle.o -lstdc++ -o rect.pyd -lpython37 但是,当我在Python中执行import rect时,现在出现了ImportError: DLL load failed: The specified procedure could not be found.。有任何想法吗? - A.P.
不确定为什么,如果我在编译之前停用我的Python环境,那么一切都正常,但是如果我留在我的Python环境中,则在导入时会出现错误消息“ImportError:DLL加载失败:找不到指定的过程。”至少上面的命令可以工作。 - A.P.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接