How to use coder.ceval to use mex function in Matlab code and then generate C++ code?
1 次查看(过去 30 天)
显示 更早的评论
I have Matlab code that I wish to convert to C++. It uses the Matlab-provided example file `mexfunction.c`:
#include "mex.h"
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
int i;
/* Examine input (right-hand-side) arguments. */
mexPrintf("\nThere are %d right-hand-side argument(s).", nrhs);
for (i=0; i<nrhs; i++) {
mexPrintf("\n\tInput Arg %i is of type:\t%s ",i,mxGetClassName(prhs[i]));
}
/* Examine output (left-hand-side) arguments. */
mexPrintf("\n\nThere are %d left-hand-side argument(s).\n", nlhs);
if (nlhs > nrhs)
mexErrMsgIdAndTxt( "MATLAB:mexfunction:inputOutputMismatch",
"Cannot specify more outputs than inputs.\n");
for (i=0; i<nlhs; i++) {
plhs[i]=mxCreateDoubleMatrix(1,1,mxREAL);
*mxGetPr(plhs[i])=(double)mxGetNumberOfElements(prhs[i]);
}
}
The Matlab code is below:
function exMexFunction
B=[0 1; 0 0; 1 0; 1 1];
C=[1.5 0; 0.5 0; 0.5 1.5; 1.5 1.5];
if coder.target('MATLAB')
res=B+C;
res1=sum(sum(res));
disp(['sum: ',num2str(res1)])
else
coder.updateBuildInfo('addSourceFiles','mexfunction.c');
P0=0;
P0=coder.ceval('mexFunction', B,C);
end
The resulting error is:
cl /c /Zp8 /GR /W3 /EHs /nologo /MD /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /DMATLAB_MEX_FILE /O2 /Oy- /DNDEBUG /fp:strict /I "C:\PROGRA~1\MATLAB\R2018b\simulink\include" /I "C:\PROGRA~1\MATLAB\R2018b\toolbox\shared\simtargets" /I "C:\Users\me\codegen\mex\EXMEXF~1" /I "C:\Users\me" /I ".\interface" /I "C:\PROGRA~1\MATLAB\R2018b\extern\include" /I "." "C:\Users\me/mexfunction.c"
mexfunction.c
cl /c /Zp8 /GR /W3 /EHs /nologo /MD /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /DMATLAB_MEX_FILE /O2 /Oy- /DNDEBUG /fp:strict /I "C:\PROGRA~1\MATLAB\R2018b\simulink\include" /I "C:\PROGRA~1\MATLAB\R2018b\toolbox\shared\simtargets" /I "C:\Users\me\codegen\mex\EXMEXF~1" /I "C:\Users\me" /I ".\interface" /I "C:\PROGRA~1\MATLAB\R2018b\extern\include" /I "." "exMexFunction_data.c"
exMexFunction_data.c
cl /c /Zp8 /GR /W3 /EHs /nologo /MD /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /DMATLAB_MEX_FILE /O2 /Oy- /DNDEBUG /fp:strict /I "C:\PROGRA~1\MATLAB\R2018b\simulink\include" /I "C:\PROGRA~1\MATLAB\R2018b\toolbox\shared\simtargets" /I "C:\Users\me\codegen\mex\EXMEXF~1" /I "C:\Users\me" /I ".\interface" /I "C:\PROGRA~1\MATLAB\R2018b\extern\include" /I "." "exMexFunction_initialize.c"
exMexFunction_initialize.c
cl /c /Zp8 /GR /W3 /EHs /nologo /MD /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /DMATLAB_MEX_FILE /O2 /Oy- /DNDEBUG /fp:strict /I "C:\PROGRA~1\MATLAB\R2018b\simulink\include" /I "C:\PROGRA~1\MATLAB\R2018b\toolbox\shared\simtargets" /I "C:\Users\me\codegen\mex\EXMEXF~1" /I "C:\Users\me" /I ".\interface" /I "C:\PROGRA~1\MATLAB\R2018b\extern\include" /I "." "exMexFunction_terminate.c"
exMexFunction_terminate.c
cl /c /Zp8 /GR /W3 /EHs /nologo /MD /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /DMATLAB_MEX_FILE /O2 /Oy- /DNDEBUG /fp:strict /I "C:\PROGRA~1\MATLAB\R2018b\simulink\include" /I "C:\PROGRA~1\MATLAB\R2018b\toolbox\shared\simtargets" /I "C:\Users\me\codegen\mex\EXMEXF~1" /I "C:\Users\me" /I ".\interface" /I "C:\PROGRA~1\MATLAB\R2018b\extern\include" /I "." "exMexFunction.c"
exMexFunction.c
exMexFunction.c(30): warning C4047: 'function': 'int' differs in levels of indirection from 'real_T [8]'
exMexFunction.c(30): warning C4024: 'mexFunction': different types for formal and actual parameter 1
exMexFunction.c(30): warning C4047: 'function': 'mxArray **' differs in levels of indirection from 'real_T [8]'
exMexFunction.c(30): warning C4024: 'mexFunction': different types for formal and actual parameter 2
exMexFunction.c(30): error C2198: 'mexFunction': too few arguments for call
gmake: *** [exMexFunction.obj] Error 2
??? Build error: C compiler produced errors. See the Build Log for
further details.
Code generation failed
What am I doing wrong?
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!