python - 如何在 python 中使用 ctypes 创建包含 mpfr.h 类型的相应结构?

标签 python c struct ctypes mpfr

我有一个 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/

相关文章:

python - 如何在python中对集合进行排序?

c - 我的 switch 语句没有从 ​​for 循环中获取值

c - 初始化具有 Union 的 Struct 数组的元素时出现问题

C++ 结构排序错误

python - 如何使用 python kubernetes 客户端读取 Kubernetes 部署

python - 多线程在同一程序中运行UDP客户端和UDP服务器

c++ - 如何确保始终为特定 C++ 实例返回相同的 Python 实例?

c - C 程序中的 Strncmp 问题

c++ - 将 GetAsyncKeyState() 重置为默认值

C编程: String arrays - how to check equality?