【问题标题】:link cython module in a c++ program在 C++ 程序中链接 cython 模块
【发布时间】:2018-12-05 11:16:07
【问题描述】:

是否可以使用一些 cdef 函数构建一个 cython 模块并将生成的共享库链接到 C++ 程序中?

我尝试了概念验证:

cymod.pyx:

# distutils: language=c++

from libcpp.string cimport string

cdef public string simple_echo(string test_string):
    return test_string

cpp_test.cpp:

#define PyMODINIT_FUNC void
#include <iostream>
#include "cymod.h"

int main(int argc, char const *argv[])
{
    std::cout << simple_echo("test") << std::endl;
    return 0;
}

setup.py:

from setuptools import setup, Extension
from Cython.Build import cythonize

setup(
    name='cymod',
    ext_modules=cythonize(
        Extension(
            "cymod", ["cymod.pyx"],
        ),
    )
)

cython 模块构建良好,但是当我尝试构建将使用 cython 函数的 c++ 代码时,我得到:

$ g++ -L. -l:cymod.so cpp_test.cpp -o cpp_test
/tmp/cc48Vc2z.o: In function `main':
cpp_test.cpp:(.text+0x51): undefined reference to `simple_echo'
collect2: error: ld returned 1 exit status

这很奇怪。生成的头文件有:

cymod.h:

  /* Generated by Cython 0.29.1 */

  #ifndef __PYX_HAVE__cymod
  #define __PYX_HAVE__cymod


  #ifndef __PYX_HAVE_API__cymod

  #ifndef __PYX_EXTERN_C
    #ifdef __cplusplus
      #define __PYX_EXTERN_C extern "C"
    #else
      #define __PYX_EXTERN_C extern
    #endif
  #endif

  #ifndef DL_IMPORT
    #define DL_IMPORT(_T) _T
  #endif

  __PYX_EXTERN_C std::string simple_echo(std::string);

  #endif /* !__PYX_HAVE_API__cymod */

  /* WARNING: the interface of the module init function changed in CPython 3.5. */
  /* It now returns a PyModuleDef instance instead of a PyModule instance. */

  #if PY_MAJOR_VERSION < 3
  PyMODINIT_FUNC initcymod(void);
  #else
  PyMODINIT_FUNC PyInit_cymod(void);
  #endif

  #endif /* !__PYX_HAVE__cymod */

我在cymod.so 中看到了我的函数:

nm cymod.so| grep simple_echo
0000000000001e50 T simple_echo

注意:我意识到要真正让它工作,我需要链接到 python 库并初始化解释器等。我把它留了下来,让它更短一点,但无论哪种方式,我都会遇到同样的错误。

【问题讨论】:

标签: c++ cython


【解决方案1】:

简短的回答是我将-l 参数太早地放在编译命令中。处理库查找路径也很重要。最简单的方法是使用rpath。我将rpath设置为可执行文件所在的目录,即.

此外,需要链接 python 库并设置包含和库路径。这些可以在编译时通过使用python-config 实用程序的输出来确定。这是最终成功的编译命令:

g++ cpp_test.cpp -o cpp_test -L. -l:cymod.so $(python-config --libs) $(python-config --includes) $(python-config --cflags) -Wl,-rpath,"\$ORIGIN"

我还将 c++ 文件更新为 #include "Python.h" 并添加了对 Py_Initialize()Py_Finalize()initcymod() 的调用:

#include <iostream>
#include "Python.h"
#include "cymod.h"

int main(int argc, char *argv[])
{
    Py_Initialize();
    initcymod();
    std::cout << simple_echo("test") << std::endl;
    Py_Finalize();
    return 0;
}

注意:对initcymod() 的调用是必要的,但特定于 python2。在 python3 上,您应该在 Py_Initialize() 之前调用 PyImport_AppendInittab("cymod", PyInit_cymod);cymod 部分是模块名称,替换为您的模块名称。

感谢 @ead 提供有关此主题的文档的信息链接 https://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#using-cython-declarations-from-c 以及他对相关问题 https://*.com/a/45424720/2069572 的回答

在阅读链接的文档时,我遇到了这个:

注意在 Linux 等某些操作系统上,也可以先以通常的方式构建 Cython 扩展,然后像动态库一样链接生成的 .so 文件。请注意,这是不可移植的,因此应避免使用。

所以事实证明你不应该做我想做的事。

相反,我应该做的是运行:

cython --cplus cymod.pyx

然后用生成的cymod.cpp文件编译cpp_test.cpp。 无需链接cython共享库,事实证明这样做不是个好主意。

【讨论】: