如何将C ++回调传递给C库函数? [英] How do I pass a C++ callback to a C library function?

查看:137
本文介绍了如何将C ++回调传递给C库函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++开发我的代码,并想使用MPFIT非线性曲线拟合库,它是在C开发的,但允许在C ++中编译。

I'm developing my code using C++ and want to use MPFIT nonlinear curve fitting library, which is developed in C but allows to compile in C++.

例如,我有一个名为myClass的类,该类有一个函数myClass :: Execute()

For example I have a class named "myClass", and this class has a function myClass::Execute()

我将mpfit.h包含到myClass.h文件中。尝试从Execute()调用一个名为mpfit的函数。

I include "mpfit.h" to myClass.h file. And try to call a function called mpfit from Execute().

int status = mpfit(ErrorFunction, num1, num2, xsub_1D, 0, 0, (void *) &variables, &result);

问题是ErrorFunction是myClass的函数。所以编译器给出错误时,我尝试使用这个。我尝试从类对象中携带Erro​​rFunction,但这次我采取以下错误:

The problem is ErrorFunction is a function of myClass. So compiler gives error when I try to use this. I tried to carry the ErrorFunction out of the class object, but this time I take the error given below:

ErrorFunction超出类时出错:

Error when the ErrorFunction is outside of the class:


错误4错误C2664:'mpfit':无法从int
(__cdecl *)(int,int,double * ,double,double *,void *)'到
'mp_func'

Error 4 error C2664: 'mpfit' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,double *,double,double *,void *)' to 'mp_func'

ErrorFunction在类中时出错:

Error when the ErrorFunction is inside the class:

Error   3   error C3867: 'myClass::ErrorFunction': function call missing argument list; use '&myClass::ErrorFunction' to

错误函数的定义:

int ErrorFunction(int dummy1, int dummy2, double* xsub, double *diff, double **dvec, void *vars)

如何调用此函数并将其解析为mpfit,这是一个C函数?

How can I call this function and parse it into mpfit, which is a C function?

mp_func 定义为:

/* Enforce type of fitting function */
typedef int (*mp_func)(int m, /* Number of functions (elts of fvec) */
               int n, /* Number of variables (elts of x) */
               double *x,      /* I - Parameters */
               double *fvec,   /* O - function values */
               double **dvec,  /* O - function derivatives (optional)*/
               void *private_data); /* I/O - function private data*/


推荐答案

确保您的调用约定匹配。 C库使用C调用约定,或cdecl(__cdecl)。如果你在C ++中使用mp_func typedef,它可能默认为编译器的标准调用约定,或stdcall(__stdcall)。要么创建一个新的typedef,要么将其改为:

Make sure that your calling conventions match. C libraries use the C calling convention, or cdecl (__cdecl). If you're using the mp_func typedef within C++, it could be defaulting to the compiler's standard calling convention, or stdcall (__stdcall). Either make a new typedef or change it to the following:

typedef int __cdecl (*mp_func)(int m, /* Number of functions (elts of fvec) */
               int n, /* Number of variables (elts of x) */
               double *x,      /* I - Parameters */
               double *fvec,   /* O - function values */
               double **dvec,  /* O - function derivatives (optional)*/
               void *private_data); /* I/O - function private data*/

当你声明ErrorFunction时, __cdecl:

And when you declare ErrorFunction, also declare it as __cdecl:

int __cdecl ErrorFunction(int, int, double*, double *, double **, void *);

如果编译器仍然抱怨调用mpfit函数时,可以尝试将函数指针转换为mp_func typedef with cdecl:

If the compiler still complains when calling the mpfit function, you can try casting your function pointer to the mp_func typedef with cdecl:

int status = mpfit((mp_func)ErrorFunction, num1, num2, xsub_1D, 0, 0, (void *) &variables, &result);

这篇关于如何将C ++回调传递给C库函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆