[英]Wrapping a C++ struct in Cython
I am trying to Cython-wrap a dll written in C++ with the following header file: 我正在尝试使用以下头文件Cython包装用C ++编写的dll:
#define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)
struct cmplx64
{
float re;
float im;
};
EXTERN_DLL_EXPORT int foo(cmplx64 *arr, int arr_sz);
The PXD file: PXD文件:
cdef extern from "mylib.h":
cdef struct cmplx64:
np.float64_t re
np.float64_t im
int foo(cmplx64 *arr, int arr_sz) except +
The PYX file: PYX文件:
cimport cmylib
import numpy as np
cimport numpy as np
import cython
def foo(np.ndarray[np.complex64_t, ndim=1] arr, int arr_sz):
return cmylib.foo(&arr[0], arr_sz)
The problem does not appear with my setup file. 我的安装文件不会出现此问题。
In lieu of the struct
definition, I have tried building a cppclass
per a suggestion I found, but I did not get as far with that as this current method. 代替struct
定义,我尝试根据我发现的建议构建一个cppclass
,但是我没有像现在这样的方法那样得到它。
The error message I am getting is: 我得到的错误消息是:
Cannot assign type 'float complex *' to 'complexFloatStruct *'
My problem is caused by the fact that the author of the library I'm using defined a complex type with a struct rather than simply using the built in complex type in the C++ std
library. 我的问题是由于我正在使用的库的作者定义了一个带结构的复杂类型,而不是简单地使用C ++ std
库中的内置复杂类型。 If that were the case, I would have no issue. 如果是这样的话,我就没有问题了。
However, it seems perfectly reasonable that I should be able to wrap a C++ class or struct with Cython. 但是,我应该能够使用Cython包装C ++类或结构似乎是完全合理的。 I have been over the documentation and have pretty much failed. 我已经阅读了文档并且几乎失败了。 Thanks for your help! 谢谢你的帮助!
A simple cast might be sufficient, 一个简单的演员可能就足够了,
def foo(np.ndarray[np.complex64_t, ndim=1] arr, int arr_sz):
return cmylib.foo(<cmylib.cmplx64 *>&arr[0], arr_sz)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.