MATLAB Answers

Generating C/C++ Code from M-Function include MEX-File

37 views (last 30 days)
Thomas
Thomas on 20 Sep 2012
Hi. Is it possible to generating C/C++ Code and Standalone-Application from a M-Function include MEX-File?
I try it like this.
function result = callfunction %#codegen
result = uint32(0);
result = Addi_mex(2,3); %call a mex-file
end
The MEX-Function is written in C++
#include "mex.h"
#include "mexhelper.h"
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
ULONG sum=0, value1=0,value2=0;
// check Inputs
...
// Convert input data to unsigned integer via Matlab-function
...
sum = value1+value2;
plhs[0] = mmCreateMxArray(sum);
}
And Build to "Addi_mex.mexw32" with "mex -g -D_WIN32_WINNT -I ..." (MVS2010)
In the Workspace it works fine, but if I try to generate code or Standalone-Application from the function "callfunction.m" I get an error:
>> coder -build coder.prj
??? Only MATLAB files are supported for code generation. Unsupported file extension 'mexw32' for 'C:/.../coder_add/Add_mex.mexw32'.
I use the Matlab Coder. Is it maybe possible with the Matlab Compiler? And is it also possible to use in the MEX-File some other lib`s or dll`s? (in workspace seems to do)
I hope someone can help me.

  0 Comments

Sign in to comment.

Accepted Answer

Mike Hosea
Mike Hosea on 20 Sep 2012
Edited: Mike Hosea on 20 Sep 2012
Add
coder.extrinsic('Addi_mex');
to the top of your MATLAB function, and I think it will work in MATLAB. Basically, the compiler is expecting to be given MATLAB code to compile, but when it goes to try to compile Addi_mex, it finds the binary mex file, which it can't handle. If you declare it extrinsic, the compiler never tries to compile Addi_mex, just sets up a call to it.
However, for standalone code generation, ALL inputs must be in MATLAB or C. The compiler cannot deconstruct a mex file and emit C code. For this you wouldn't use your MEX file, rather you'd write a C library that is used both by your MEX file and your coder project. Then from your MATLAB function you would call into this library using coder.ceval.

  6 Comments

Show 3 older comments
Mike Hosea
Mike Hosea on 20 Sep 2012
Your second comment showed up at the same time as my last comment, and I didn't see it until just now. Yes, you need the source. I thought since you knew that the mex file was written in C++ that you had the source. If all you have is a mex file (a binary) and your goal is to generate standalone code, then the MATLAB Compiler is the only supported way that I know of. The results will be quite different from MATLAB Coder, of course, and only usable on systems that are capable of running MATLAB.
Thomas
Thomas on 21 Sep 2012
Ok thank you. A little step :-) No I creat the mex file by myselfe. So I have the code and not only in binary.
function c = callfunction %#codegen
a = 6;
b = 2;
c = 0;
if isempty(coder.target)
% running in MATLAB
c = Addi(a,b); %call mex
else
% running by coding
c = coder.ceval('Addi',a,b); %call Addi from myLib.lib
end
end
No Problem my generating Code with Matlab Coder and running the exe. But creating the Addi MEX File is not longer possible.
#include "mex.h"
#include "mexhelper.h"
#include "myLib.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
double sum=0, value1=0,value2=0;
// check inputs
...
// Convert input data to unsigned integer via Matlab-function
...
//sum = value1 + value2; // that runs
sum = Addi(value1,value2); //call the same lib as used from Matlab coder
plhs[0] = mmCreateMxArray(sum);
}
Can you help me, please?
1> === building: "Addi.cpp"
1> Creating library C:\Users\EHT1LO\AppData\Local\Temp\mex_f8x8_K\templib.x and object C:\Users\EHT1LO\AppData\Local\Temp\mex_f8x8_K\templib.exp
1>Addi.obj : error LNK2019: unresolved external symbol "double __cdecl Addi(double,double)" (?Addi@@YANNN@Z) referenced in function _mexFunction
1>out\Addi.mexw32 : fatal error LNK1120: 1 unresolved externals
1>
1>C:\PROGRA~2\MATLAB\R2012A\BIN\MEX.PL : error : Link of 'out\Addi.mexw32' failed.
Mike Hosea
Mike Hosea on 21 Sep 2012
It's been a very long time since I created a mex file "from scratch", but it looks like the mex command doesn't know about the library. Did you supply a "-l myLib" ?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!