在 python 中定义一个结构体以使用 C 库 (dll) 中的函数
首先,对不起我的英语。
我在 Python 脚本中使用动态库(gnu gsl 库)中的函数时遇到一些麻烦。
事实上,我知道使用一个函数,但我需要定义一个结构白色 C 类型数据来使用特定的函数。
首先,这是在 C++ 脚本中使用该函数的示例,我将在 Python 脚本中使用该函数:
double fn1 (double x, void * params)
{
return cos(x) + 1.0;
}
int
main (void)
{
gsl_min_fminimizer *s;
double m = 2.0, m_expected = M_PI;
double a = 0.0, b = 6.0;
gsl_function F;
F.function = &fn1;
F.params = 0;
T = gsl_min_fminimizer_brent;
s = gsl_min_fminimizer_alloc (T);
gsl_min_fminimizer_set (s, &F, m, a, b);
...
}
标头中 gsl_function 的 typedef 是:
struct gsl_function_struct
{
double (* function) (double x, void * params);
void * params;
};
typedef struct gsl_function_struct gsl_function ;
#define GSL_FN_EVAL(F,x) (*((F)->function))(x,(F)->params)
我知道使用带有 ctypes 的函数:
#Library Call
lib_gsl=CDLL(path_gsl+"gsl.dll")
#Functions Define
gsl_opti_brent=lib_gsl.gsl_min_fminimizer_brent
gsl_opti_alloc=lib_gsl.gsl_min_fminimizer_alloc
gsl_opti_init=lib_gsl.gsl_min_fminimizer_set
T=gsl_opti_brent
T_=pointer(T)
s=gsl_opti_alloc(T_)
在此之前,没有问题,但是当我想使用函数 gsl_min_fminimizer_set,我需要在名为 gsl_function 的标头中定义一个特定类型。
有人知道如何在Python中定义typedef gsl_function来使用名为函数gsl_min_fminimizer_set的dll?
谢谢。 我希望我是明确的
For beginning, sorry for my english.
I have some trouble to use a function from a dynamic library (the gnu gsl library) in a Python script.
Indeed, I know use a function but I need to define a Structure white C type datas to use a particulary function.
In first this is an example of the use of the function in a C++ script that I would use in Python script:
double fn1 (double x, void * params)
{
return cos(x) + 1.0;
}
int
main (void)
{
gsl_min_fminimizer *s;
double m = 2.0, m_expected = M_PI;
double a = 0.0, b = 6.0;
gsl_function F;
F.function = &fn1;
F.params = 0;
T = gsl_min_fminimizer_brent;
s = gsl_min_fminimizer_alloc (T);
gsl_min_fminimizer_set (s, &F, m, a, b);
...
}
The typedef of gsl_function in the header is:
struct gsl_function_struct
{
double (* function) (double x, void * params);
void * params;
};
typedef struct gsl_function_struct gsl_function ;
#define GSL_FN_EVAL(F,x) (*((F)->function))(x,(F)->params)
I know use a function with ctypes:
#Library Call
lib_gsl=CDLL(path_gsl+"gsl.dll")
#Functions Define
gsl_opti_brent=lib_gsl.gsl_min_fminimizer_brent
gsl_opti_alloc=lib_gsl.gsl_min_fminimizer_alloc
gsl_opti_init=lib_gsl.gsl_min_fminimizer_set
T=gsl_opti_brent
T_=pointer(T)
s=gsl_opti_alloc(T_)
Until this, there is no trouble but when I want use the function gsl_min_fminimizer_set, I need a particulary type define in a header called gsl_function.
There is somebody knows how can I define the typedef gsl_function in Python to use the dll called function gsl_min_fminimizer_set?
Thank you.
I expect I am explicit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该查看此 ctypes 教程(特别是结构和回调函数)。
它应该是这样的:
You should take a look at this ctypes tutorial (specially Structures and Callback functions).
It should be something like this:
我认为您正在寻找
ctypes.Structure
。这是在 python 中创建结构体的一种方法;有关更多示例,您可以查看 ctypes 文档。
I think you are looking for
ctypes.Structure
. Here is a way to create structs in python;For more examples you can check out ctypes documentation.