问题描述
亲爱的所有人,
使用MATLAB 2009b,我使用mcc从MATLAB函数(first.m)创建了DLL(first_lib.dll,first_lib.h和first_lib.lib),我想使用Microsoft Visual C ++ 2008(Express Edition)在C代码中调用它),它是从互联网上以免费软件下载的.
我在编译C代码时在此处发布带有编译错误的完整代码(请提供建议):
报告的错误和警告是:
1)错误C2275:``mxArray'':非法将此类型用作表达式
c:\ program files \ matlab \ r2009a \ extern \ include \ matrix.h(292):请参见``mxArray''的声明
2)警告C4047:``='':``int''在间接级别上与``void *''
不同
3)警告C4047:``='':``int''在间接级别上与``mxArray *''
不同
4)警告C4047:``function'':``const mxArray *''的间接访问级别与``int''
不同
5)警告C4024:``mxGetPr_proxy'':形式参数和实际参数1的类型不同
6)错误C2172:``memcpy'':实际参数不是指针:参数2
请提供有关如何编译和运行它的建议.
我将在此处发布完整的C代码,其中mlfFirst是函数调用.
代码:
Dear All,
using MATLAB 2009b, I created the DLL (first_lib.dll, first_lib.h and first_lib.lib) from MATLAB function (first.m) using mcc and I would like to call it in a C code using Microsoft Visual C++ 2008 (Express Edition) which I downloaded from internet as free s/w.
I am posting the complete code here with compilation errors while compiling the C code (Please advice on that):
Reported Errors & warnings are:
1) error C2275: ''mxArray'' : illegal use of this type as an expression
c:\program files\matlab\r2009a\extern\include\matrix.h(292) : see declaration of ''mxArray''
2) warning C4047: ''='' : ''int'' differs in levels of indirection from ''void *''
3) warning C4047: ''='' : ''int'' differs in levels of indirection from ''mxArray *''
4) warning C4047: ''function'' : ''const mxArray *'' differs in levels of indirection from ''int''
5) warning C4024: ''mxGetPr_proxy'' : different types for formal and actual parameter 1
6) error C2172: ''memcpy'' : actual parameter is not a pointer : parameter 2
Please advice on how to compile it and run it.
I am posting my complete C code here, where mlfFirst is the function call.
Code:
#ifdef _MSC_VER // if MS Visual C++
#pragma warning(push)
#pragma warning(disable:4996)
#pragma comment(lib, "libdfblas.lib")
#pragma comment(lib, "libdflapack.lib")
#pragma comment(lib, "libemlrt.lib")
#pragma comment(lib, "libeng.lib")
#pragma comment(lib, "libfixedpoint.lib")
#pragma comment(lib, "libmex.lib")
#pragma comment(lib, "libmwblas.lib")
#pragma comment(lib, "libmwlapack.lib")
#pragma comment(lib, "libmwmathutil.lib")
#pragma comment(lib, "libmwservices.lib")
#pragma comment(lib, "libmx.lib")
#pragma comment(lib, "libut.lib")
#pragma comment(lib, "mclcom.lib")
#pragma comment(lib, "mclcommain.lib")
#pragma comment(lib, "mclmcr.lib")
#pragma comment(lib, "mclmcrrt.lib")
#pragma comment(lib, "mclxlmain.lib")
#pragma comment(lib, "first_lib.lib")
#endif
#include
#include ;
#include "mex.h"
#include "first_lib.h"
int main()
{
double a,b,c, Ans;
a = 1; b =2; c=3;
mxArray *pmX, *pmY, *pmZ;
mxArray* pmAns = NULL;
mclinitializeApplication(NULL,0);
*mxGetPr(pmX) = a;
*mxGetPr(pmY) = b;
*mxGetPr(pmZ) = c;
first_libInitialize();
memcpy(mxGetPr(pmX),&a, sizeof(double)*1);
memcpy(mxGetPr(pmY),&b, sizeof(double)*1);
memcpy(mxGetPr(pmZ),&c, sizeof(double)*1);
mlfFirst(1,&pmAns,pmX,pmY,pmZ);
Ans = *mxGetPr(pmAns);
first_libTerminate();
printf("%f %f\n", Ans);
mxDestroyArray(pmX);
mxDestroyArray(pmY);
mxDestroyArray(pmZ);
mxDestroyArray(pmAns);
mclTerminateApplication();
printf("Hello World");
return 0;
}
推荐答案
这篇关于如何通过DLL在C代码中编译MATLAB函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!