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

时间:2018-06-06 11:17:10

标签: python cython cpython

我刚刚成功编译了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中,因为在构建过程中没有错误。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

您不应该调用您的扩展程序/模块cell.pyx,而是以不同方式调用它 - 例如cycell.pyx

为什么呢?在构建扩展时采取以下步骤

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

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

相关问题