我有一个 C API,其函数适用于以下 C 结构:
typedef struct s_t{
mpq_ptr mpq;
mpfr_ptr mpfr;
} s_t;
我正在为此 C API 构建一个 python 包装器,因此我需要将此结构定义为继承自 ctypes 中“Structure”类的类,然后为 _field_ 赋予值。但是 mpq_ptr、mpfr_ptr 是 mpfr.h 中的类型,并且它们没有相应的 ctypes 类型。有办法解决这个问题吗?
我想在Python中获得的是这样的:
class S_t(Structure):
_fields_ = [('mpq', CTYPEFOR(mpq_ptr)),('mpfr', CTYPEFOR(mpfr_ptr))]
这样我就可以在 python 中创建此类对象并将它们传递给以 s_t 作为输入参数的相应 C 函数。
最佳答案
我通过跟踪 mpfr.h 和 gmp.h 文件中的所有 typedef 并在 python 中创建所有必要的结构来解决这个问题:
class Mpz(Structure):
# define struct manually
class Mpq(Structure):
# define struct manually
class Mpfr(Structure):
# define struct manually
关于python - 如何在 python 中使用 ctypes 创建包含 mpfr.h 类型的相应结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47311220/