![](/img/trans.png)
[英]How/When to use a Cython Extension Type vs a Cython Struct to store data that is passed around class functions
[英]How to use pointer to typedef struct in Cython Extension Type
说我的头文件example.h
具有以下typedef struct
:
typedef struct A A;
我正在尝试在Cy.thon扩展类型中使用指向A
的指针作为test.pyx
的类变量,然后在对A
的引用上调用初始化函数f
:
cdef class Test:
cdef A* a
def __cinit__(self):
self.a = a
f(&a)
...
编译`test.pyx时,出现以下编译错误:
Error compiling Cython file:
------------------------------------------------------------
...
cdef class Test:
cdef A* a
def __cinit__(self):
self.a = a
^
------------------------------------------------------------
test.pyx: undeclared name not builtin: a
Error compiling Cython file:
------------------------------------------------------------
...
cdef class Test:
cdef A* a
def __cinit__(self):
self.a = a
^
------------------------------------------------------------
test.pyx: Cannot convert Python object to 'A *'
显然,它没有识别出对象a
,而是将其解释为python对象。 我怎样才能解决这个问题?
这是因为使用前必须先声明它。 例如,您在c代码example.h
具有此代码:
typedef struct struct_name{
int a;
float b;
}struct_alias;
然后,您的.pyx
文件应如下所示:
cdef extern from "example.h":
ctypedef struct struct_alias:
int a
int b
cdef class Test:
cdef A* a
def __cinit__(self):
self.a = a
f(&a)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.