2017-01-25 47 views
0

現在、私はCythonを学びたいと思っており、チュートリアルを開始しています。 (Debian 8を実行中) Using C librariesの部分に問題があります。ここでCythonのコンパイルエラーCライブラリのチュートリアル

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) 

私のsetup.pyある私はpython setup.py build_ext -iをしようとすると、それは私に次のエラーを通知しますメッセージ: 変更されたためにque.pyxをコンパイルしています。私に

[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オブジェクトに変換することはできませんが、私は本当に正確な問題が何であるかを知らないことをことをしているように思えます。

誰かがエラーの意味を理解できますか?

答えて

1

que.pyxque.pxdは名前空間を共有します(pxdファイルがpyxファイルと同じ名前を持つ場合、自動的にpyxファイルに書き込まれます)。したがって、CythonはQueueがC構造体定義とcdefクラスの両方であると考えています。 (このような状況では、あなたがcimport queを行う必要はありません)

は、あなたが(少なくとも)3つのオプション持っている:

  1. は、ファイルの1の名前を変更します(これPYX名前空間への自動cimporting」はdoesnのtは起こる)。
  2. cdefクラスの名前を変更してください。
  3. C構造体の名前を変更します。あなただけCythonでこれを行う必要があることに注意してください、あなたはCで名前を維持していることができます:

コードポイント3のために:あなたはその後、マイナーの束で終わる

ctypedef struct c_Queue "Queue": # called c_Queue in Cython but Queue in C 
    pass 

エラーメッセージ:

que.pyx:6:38: Cannot convert Python object to 'Queue *'

que.cqueue_new()あなたがCポインタにis notを使用することはできません

que.pyx:11:21: Invalid types for 'is_not' (Python object, void *)

の綴りを確認してください。スペルを確認してください!=

que.pyx:12:14: undeclared name not builtin: cqueue

を使用してください。

+0

これは本当に助けになりましたが、今は 'libcalg/queue.h'ファイルが見つからないというエラーが表示されます。 [ここ](https://github.com/fragglet/c-algorithms/releases)からダウンロードしましたが、 'configure --prefix =/usr/include'を' make'と 'make install'しましたが、 libcalg.so'と 'libarlg/queue.h'ファイルは表示されません。 – oldmansaur

+0

私は本当にそれが私が恐れていることについては知らない – DavidW

関連する問題