Cython编译错误C库教程 [英] Cython compile error C-Libraries Tutorial
问题描述
我目前正在尝试学习Cython,并从阅读其教程开始。 (正在运行Debian 8)
我在使用C库部分。
I am currently trying to learn Cython, and started by going through their Tutorial. (Running Debian 8) I am running into problems in the Using C libraries part.
这是我的setup.py
Here are my setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules = cythonize([
Extension("queue", ["que.pyx"],
libraries=["calg"])
])
setup(
ext_modules=ext_modules )
que.pxd
cdef extern from "libcalg/queue.h":
ctypedef struct Queue:
pass
ctypedef void* QueueValue
Queue* queue_new()
void queue_free(Queue* queue)
int queue_push_head(Queue* queue, QueueValue data)
QueueValue queue_pop_head(Queue* queue)
QueueValue queue_peek_head(Queue* queue)
int queue_push_tail(Queue* queue, QueueValue data)
QueueValue queue_pop_tail(Queue* queue)
QueueValue queue_peek_tail(Queue* queue)
bint queue_is_empty(Queue* queue)
和que.pyx
cimport que
cdef class Queue:
cdef que.Queue* _c_queue
def __cinit__(self):
self._c_queue = que.cqueue_new()
if self._c_queue is NULL:
raise MemoryError()
def __dealloc__(self):
if self._c_queue is not NULL:
cqueue.queue_free(self._c_queue)
执行 python setup.py build_ext -i
它会告诉我以下错误消息:
编译que.pyx,因为它已更改。
When I try to do python setup.py build_ext -i
it tells me the following error messages:
Compiling que.pyx because it changed.
[1/1] Cythonizing que.pyx
Error compiling Cython file:
------------------------------------------------------------
...
cimport que
cdef class Queue:
^
------------------------------------------------------------
que.pyx:3:5: 'Queue' redeclared
Error compiling Cython file:
------------------------------------------------------------
...
cimport que
cdef class Queue:
cdef que.Queue* _c_queue
^
------------------------------------------------------------
que.pyx:4:18: Pointer base type cannot be a Python object
Error compiling Cython file:
------------------------------------------------------------
...
cimport que
cdef class Queue:
cdef que.Queue* _c_queue
def __cinit__(self):
self._c_queue = que.cqueue_new()
^
------------------------------------------------------------
que.pyx:6:38: Cannot convert Python object to 'Queue *'
Error compiling Cython file:
------------------------------------------------------------
...
cimport que
cdef class Queue:
cdef que.Queue* _c_queue
def __cinit__(self):
self._c_queue = que.cqueue_new()
^
------------------------------------------------------------
que.pyx:6:38: Storing unsafe C derivative of temporary Python reference
Error compiling Cython file:
------------------------------------------------------------
...
self._c_queue = que.cqueue_new()
if self._c_queue is NULL:
raise MemoryError()
def __dealloc__(self):
if self._c_queue is not NULL:
^
------------------------------------------------------------
que.pyx:11:21: Invalid types for 'is_not' (Python object, void *)
Error compiling Cython file:
------------------------------------------------------------
...
if self._c_queue is NULL:
raise MemoryError()
def __dealloc__(self):
if self._c_queue is not NULL:
cqueue.queue_free(self._c_queue)
^
------------------------------------------------------------
que.pyx:12:14: undeclared name not builtin: cqueue
Traceback (most recent call last):
File "setup.py", line 7, in <module>
libraries=["calg"])
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 877, in cythonize
cythonize_one(*args)
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 997, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: que.pyx
在我看来像主要问题一样,我需要重新声明一些内容并且不能将指针转换为python对象,但是我真的不知道确切的问题是什么。
To me it seems like the main problems are that I redeclare some things and that a pointer cannot be converted into a python object, but I really do not know what the exact problem is.
有人可以帮助我弄清错误吗?
Can somebody help me make sense of the errors?
推荐答案
que.pyx
和 que.pxd
共享一个名称空间(如果pxd文件与pyx文件同名,则它会自动导入到pyx文件中)。因此,Cython认为 Queue
既是C结构定义,也是您的cdef类。 (在这种情况下,您不需要执行 cimport que
)
que.pyx
and que.pxd
share a namespace (if a pxd file has the same name as a pyx file then it's automatically cimported into the pyx file). Therefore Cython believes Queue
to be both the C structure definition and your cdef class. (Under these circumstances you don't need to do cimport que
)
您已经(至少) 3个选项:
You've got (at least) 3 options:
- 重命名其中一个文件(因此不会自动导入pyx名称空间)。
- 重命名您的cdef类。
- 重命名您的C结构。请注意,您只需要在Cython中执行此操作,就可以将名称保留在C:
第3点的代码
ctypedef struct c_Queue "Queue": # called c_Queue in Cython but Queue in C
pass
然后您会收到一堆小错误消息:
You then end up with a bunch of minor error messages:
que.pyx:6:38:无法将Python对象转换为'Queue *'
检查 que.cqueue_new()
que.pyx:11:21:'is_not'的类型无效(Python对象,void *)
对于C指针,不能使用不是
。使用!=
You can't use is not
for C pointers. Use !=
que.pyx:12: 14:未声明的名称未内置:队列
检查拼写。
这篇关于Cython编译错误C库教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!