CPython:动态模块未定义模块导出函数错误

CPython: Dynamic module does not define module export function error

我刚刚成功地为 C++ 类 编译了我的 Python 包装器。但是,当我尝试将我的模块加载到 Python(通过 import cell)时收到以下消息:

ImportError: dynamic module does not define module export function (PyInit_cell)

我检查过系统在所有情况下都使用 Python3,所以这不是 Python 版本问题。
下面是我的 setup.py 文件:

from distutils.core import setup, Extension
from Cython.Build import cythonize

setup(ext_modules = cythonize(Extension(
           "cell",                                
           sources=["cell.pyx", "cell.cc"],     
           language="c++",                       
           extra_compile_args=["-std=c++11"],
      )))

下面是生成的 .so 文件的转储:

0000000000201020 B __bss_start
0000000000201020 b completed.7594
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000000530 t deregister_tm_clones
00000000000005c0 t __do_global_dtors_aux
0000000000200de8 t __do_global_dtors_aux_fini_array_entry
0000000000201018 d __dso_handle
0000000000200df8 d _DYNAMIC
0000000000201020 D _edata
0000000000201028 B _end
0000000000000630 T _fini
0000000000000600 t frame_dummy
0000000000200de0 t __frame_dummy_init_array_entry
0000000000000640 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
00000000000004e8 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000200df0 d __JCR_END__
0000000000200df0 d __JCR_LIST__
                 w _Jv_RegisterClasses
0000000000000570 t register_tm_clones
0000000000201020 d __TMC_END__

我真的不明白为什么模块没有加载到python,因为在构建过程中没有错误

如有任何帮助,我们将不胜感激!

你不应该叫你的 extension/module cell.pyx,叫法不同 - 例如 cycell.pyx.

为什么?构建扩展时执行以下步骤

  1. Cython 从 cell.pyx.
  2. 生成文件 cell.cpp
  3. 编译器将cell.cpp编译成目标文件cell.o
  4. 编译器将 cell.cc 编译为目标文件 cell.o 并覆盖从 cell.pyx 创建的目标文件。
  5. 链接器链接两个 cell.o 文件(但实际上只有一个)- 结果没有在 cell.pyx/cell.cpp 中定义的内容,特别是 PyInit_cell.

通过重命名 Cython 文件,您可以避免目标文件被覆盖。

显然,另一种选择是重命名您的 c++ 文件。