尝试使用 GCC 编译 Cython 输出时出错

Error while trying to compile Cython output with GCC

我正在尝试使用 GCC 将 Cython3 文件编译成可执行文件。 目前我仍然坚持使用简单的 "hello world" :

# -*- coding: utf-8 -*-
if __name__ == "__main__":
    print("Hello World !")

这是我为编译这个简单程序而尝试执行的命令:

cython3 test.pyx
gcc -I/usr/include/python3.4 test.c

第一个命令 运行 正确,但这是我输入第二个命令时得到的结果:

cython.c:422:14: error: conflicting types for ‘PyTypeObject’
 typedef void PyTypeObject;
              ^
In file included from /usr/include/python3.4/pytime.h:6:0,
                 from /usr/include/python3.4/Python.h:65,
                 from cython.c:16:
/usr/include/python3.4/object.h:422:3: note: previous declaration of ‘PyTypeObject’ was here
 } PyTypeObject;
   ^
cython.c: In function ‘__Pyx_PyObject_GetAttrStr’:
cython.c:488:18: warning: dereferencing ‘void *’ pointer
     if (likely(tp->tp_getattro))
                  ^
cython.c:399:43: note: in definition of macro ‘likely’
   #define likely(x)   __builtin_expect(!!(x), 1)
                                           ^
cython.c:488:18: error: request for member ‘tp_getattro’ in something not a structure or union
     if (likely(tp->tp_getattro))
                  ^
cython.c:399:43: note: in definition of macro ‘likely’
   #define likely(x)   __builtin_expect(!!(x), 1)
                                           ^
cython.c:489:18: warning: dereferencing ‘void *’ pointer
         return tp->tp_getattro(obj, attr_name);
                  ^
cython.c:489:18: error: request for member ‘tp_getattro’ in something not a structure or union

我目前正在 运行 进行 Debian 测试,因此我有以下版本的 Python 和 Cython :

Python: 3.4.2-2
Cython: 0.21.1-1

使用以下命令解决了问题:

cython3 test.pyx
gcc -I/usr/include/python3.4m test.c -lpython3.4m

我怀疑你的回答是否能解决问题。

您最初的问题是,该扩展名为 cython.pyx(考虑到 this post)。

但是,不允许将您的 cython 模块命名为 "cython",因为它是 Cython 的特殊名称,并且会导致生成的 c 文件无法编译(无论出于何种原因虚假 typedef void PyTypeObject; 被插入)。不幸的是,cython 没有报告这种情况的错误。

将 pyx-file/extension 从 cython.pyx 重命名为 test.pyx 解决了问题。