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

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.

 Accepted Answer

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

I try it like you have suggested.
function result = callfunction %#codegen
result = uint32(0);
coder.extrinsic('Addi_mex');
result = Addi_mex(2,3); %call a mex-file
end
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 do.prj
??? Failed to eliminate a call to the MATLAB function 'RealtimeTest'. For non-simulation builds, calls to unsupported MATLAB functions are eliminated if they do not affect function outputs.
By declaring these functions extrinsic, you instruct the software not to compile or generate code from it.
Your proposal to separate the input parameter, I tried as follows. I made a MEX-Function from "callfunction". In the Workspace "callfunction_mex" works fine. I include it in a new function:
function start
callfunction_mex();
I try to generate code or Standalone-Application from the function "start.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/callfunction_mex.mexw32'.
And for "coder.ceval" I need the source.
Yes, I added the second paragraph to my answer when I re-read your question. The compiler does not have the ability to deconstruct a binary file and produce C source code. So if you have an algorithm that is used in a mex file, you need to factor that algorithm into a standalone C library. Then rewrite your mex function to call into and link against this library. That restores your mex file to working order. Now, rewrite your MATLAB Code so that it only calls your mex file if it is running in MATLAB, otherwise use the same library as the mex file. A call site might look like this:
c = uint32(0);
if isempty(coder.target)
% running in MATLAB
c = mymexfun(a,b);
else
coder.ceval('mylibraryfun',a,b,coder.wref(c));
end
The only reason for referring to both functions here is so the code can be run in MATLAB. If you only want to generate standalone code, you just need the coder.ceval line.
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.
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.
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)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!