2017-03-31 21 views
0

UPDATE 在GitHub上最小的例子:https://github.com/wl2776/cython_error地圖數組用Cython

我有一個C庫,我想從Python來訪問。我正在爲它開發一個Cython包裝器。

圖書館有以下聲明:

文件 「globals.h」

typedef struct 
{ 
    int x; 
    int y; 
    int radius; 
} circleData; 

文件 「O_Recognition.h」

#include "globals.h" 
typedef struct 
{ 
    int obj_count; 
    circleData circle_data[2]; 
    float parameters[2]; 
} objectData; 

我在映射這些類型用Cython。 PXD文件,如下所示:

文件 「cO_Recognition.pxd」:

cdef extern from "globals.h": 
    ctypedef struct circleData: 
     int x; 
     int y; 
     int radius; 

cdef extern from "O_Recognition.h": 
    ctypedef struct objectData: 
     int obj_count; 
     circleData circle_data[2]; 
     float parameters[2]; 

而且這不會編譯。我得到的錯誤:

Error compiling Cython file: 
------------------------------------------------------------ 
... 
    void PyTuple_SET_ITEM(object p, Py_ssize_t pos, object o) 
    void PyList_SET_ITEM(object p, Py_ssize_t pos, object o) 

@cname("__Pyx_carray_to_py_circleData") 
cdef inline list __Pyx_carray_to_py_circleData(circleData *v, Py_ssize_t length): 
               ^
------------------------------------------------------------ 
carray.to_py:112:45 'circleData' is not a type identifier 

一個細節,這是CMake的項目的一部分,正在使用從GitHub這個例子建:https://github.com/thewtex/cython-cmake-example

的CMakeLists.txt的相關部分包括與.pyx文件其他名稱,cimport s This cDeclarations.pxd

+0

添加一些包括警衛C頭文件,我在jupyter筆記本編譯了代碼成功。(cython0.25.2 + vs2015) – oz1

+0

@ OZ1,我已經添加了細節。這些聲明位於.pxd文件中 – wl2776

回答

1

問題是circleDataO_Recognition.h extern塊中未定義。其先前的定義僅適用於globals.h外部塊。

只需要包括它的類型,以便用Cython知道它是什麼。它不需要重新定義。

cdef extern from "globals.h" nogil: 
    ctypedef struct circleData: 
     int x; 
     int y; 
     int radius; 

cdef extern from "O_Recognition.h" nogil: 
    ctypedef struct circleData: 
     pass 
    ctypedef struct objectData: 
     int obj_count; 
     circleData circle_data[2]; 
     float parameters[2]; 

當代碼被編譯時,.c文件將include兩個頭文件和從globals.h得到circleData類型定義。不需要

從技術上講,在globals.h的extern塊中的circleData成員定義要麼除非結構成員將要在用Cython代碼中使用。

記住,pxd文件是用Cython代碼,而不是C代碼定義。僅包含要在Cython代碼中使用的成員,否則可以只爲上述每個識別外部塊的circleData定義類型sans成員。