更新 GitHub上的最小示例:https://github.com/wl2776/cython_error
我有一个C库,我想从Python访问它。我正在为它开发一个Cython包装。在
库有以下声明:
文件“globals.h”
typedef struct
{
int x;
int y;
int radius;
} circleData;
文件“O\u Recognition.h”
^{pr2}$我将这些类型映射到.pxd文件中的Cython,如下所示:
文件“cO_识别.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
cDeclarations.pxd文件在
问题是
circleData
在O_Recognition.h
外部块中未定义。它以前的定义只适用于globals.h
外部块。在只需要包括它的类型,这样Cython就知道它是什么了。不需要重新定义。在
在编译代码时,
.c
文件将include
两个头文件,并从globals.h
获取circleData的类型定义。在从技术上讲,
globals.h
外部块中的circleData
成员的定义也不需要,除非结构成员将在Cython代码中使用。在记住,
pxd
文件是Cython代码的定义,而不是C代码。只包含要在Cython代码中使用的成员,否则只能在上面的每个识别外部块中定义sans members percircleData
类型。在相关问题 更多 >
编程相关推荐