我如何传递一个C ++回调到C库函数? [英] How do I pass a C++ callback to a C library function?
问题描述
我用C ++开发我的code和要使用MPFIT非线性曲线拟合库,这是C语言开发,但允许在C ++编译。
例如我有一个名为MyClass的类,这个类有一个函数MyClass的::执行()
我有mpfit.h来myClass.h文件。并尝试调用一个函数从执行(称为mpfit)。
INT状态= mpfit(ErrorFunction,NUM1,NUM2,xsub_1D,0,0,(无效*)及变量,和放大器;结果);
问题是ErrorFunction是myClass的的功能。所以编译器为错误,当我尝试使用此。我试图携带ErrorFunction出来的类的对象,但这次我把错误如下:
错误时ErrorFunction是类以外的:
错误4错误C2664:'mpfit':无法从'诠释转换参数1
(__cdecl *)(INT,INT,双*双,双*,无效*)来
mp_func
块引用>错误时ErrorFunction是类里面的:
错误3错误C3867:'MyClass的:: ErrorFunction':函数调用缺少参数列表;用'和; MyClass的:: ErrorFunction来
误差函数的定义:
INT ErrorFunction(INT dummy1,诠释dummy2,双* XSUB,双*差异,双** dvec,无效*增值经销商)
我怎样才能调用这个函数,它解析为mpfit,这是一个C函数?
mp_func
的定义是:/ *强制拟合函数类型* /
的typedef INT(* mp_func)(INT男,/ *的功能数(fvec的应急定位发射器)* /
INT N,/ *变量数(x的应急定位发射器)* /
双* X,/ * I - 参数* /
双* fvec,/ *○ - 函数值* /
双** dvec,/ *○ - 功能衍生物(可选)* /
无效* private_data的); / * I / O - 功能私有数据* /
解决方案请确保您的调用约定相匹配。 C库使用C调用约定,或CDECL(__cdecl)。如果您使用C ++中的mp_func的typedef,它可以被默认为编译器的标准调用约定,或STDCALL(__stdcall)。无论是做一个新的typedef,或将其更改为以下内容:
的typedef INT __cdecl(* mp_func)(INT男,/ *的功能数(fvec的应急定位发射器)* /
INT N,/ *变量数(x的应急定位发射器)* /
双* X,/ * I - 参数* /
双* fvec,/ *○ - 函数值* /
双** dvec,/ *○ - 功能衍生物(可选)* /
无效* private_data的); / * I / O - 功能私有数据* /当你声明ErrorFunction,也将其声明为__cdecl:
INT __cdecl ErrorFunction(INT,INT,双*双*,**双,无效*);
如果编译器调用mpfit函数时还是会抱怨,你可以尝试铸造你的函数指针的typedef mp_func与CDECL:
INT状态= mpfit((mp_func)ErrorFunction,NUM1,NUM2,xsub_1D,0,0,(无效*)及变量,和放大器;结果);
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++.
For example I have a class named "myClass", and this class has a function myClass::Execute()
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);
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:
Error when the ErrorFunction is outside of the class:
Error 4 error C2664: 'mpfit' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,double *,double,double *,void *)' to 'mp_func'
Error when the ErrorFunction is inside the class:
Error 3 error C3867: 'myClass::ErrorFunction': function call missing argument list; use '&myClass::ErrorFunction' to
Definition of error function:
int ErrorFunction(int dummy1, int dummy2, double* xsub, double *diff, double **dvec, void *vars)
How can I call this function and parse it into mpfit, which is a C function?
mp_func
is defined as:/* 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*/
解决方案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*/
And when you declare ErrorFunction, also declare it as __cdecl:
int __cdecl ErrorFunction(int, int, double*, double *, double **, void *);
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屋!