2017-03-31 16 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成员。