Cython 构建 python 脚本

Cython to build a python script

尝试将 cython 升级到 运行,但在遵循快速入门指南后我遇到了这个问题:

$ ~/midt $ ./setup.py build_ext --inplace
running build_ext
building 'proc' extension
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c proc.c -o build/temp.linux-x86_64-2.7/proc.o
proc.c:2511:16: error: ‘initproc’ redeclared as different kind of symbol
 PyMODINIT_FUNC initproc(void); /*proto*/
                ^
In file included from /usr/include/python2.7/Python.h:80:0,
                 from proc.c:16:
/usr/include/python2.7/object.h:320:15: note: previous declaration of ‘initproc’ was here
 typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
               ^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

这是我的版本号和内容:

$ ~/midt $ python --version
Python 2.7.6
$ ~/midt $ cython --version
Cython version 0.23.5
$ ~/midt $ uname -a
Linux xxxxx 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

我认为问题是:

  1. Python 需要初始化 C 模块初始化的函数被称为 init<module_name> (您可以在其中替换您的模块名称)。 (Python 3 使用的形式略有不同)。

  2. 您已调用您的模块 proc

  3. Cython 因此创建了一个名为 initproc 的函数,将在导入模块时定义。

  4. Python 在内部定义了一个名为 initproc 的类型定义,它与用于初始化模块的 Cython 生成的 initproc 冲突。

解决方案是将您的模块命名为 proc 以外的名称。这不是一个理想的解决方案,但没有太多其他选择。


Python 3 选择不同形式 PyInit_<module_name> 的原因之一是 Python 2 形式已知会导致一些冲突(参见 https://www.python.org/dev/peps/pep-3121/#entry-point-name-conflicts)。