Create C/C++ shared library with MATLAB Compiler
MATLAB® is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++, and Fortran (http://www.mathworks.com/products/matlab/). Matlab have a complete function to solve mathematic problem.
We can call function code from matlab in other language (ex. C/C++). Before we create code in C/C++, we must install MCRInstaller.bin (in Linux) or MCRInstaller.exe (in Windows). I am using Linux Ubuntu 10.10. To install this file you can type at konsole with command :
sudo MCRInstaller.bin
You can find this file at Matlab folder location. After you install this file, you must add library path from MCR file to your system path. Edit file ~/.bashrc and add this text :
#matlab export MATLABMCR=/opt/MATLAB/MATLAB_Compiler_Runtime export MATLABLIB=/data/ProgramFiles/MatlabR2010b/bin/glnx86/:${MATLABMCR}/v714/runtime/glnx86:${MATLABMCR}/v714/sys/os/glnx86:${MATLABMCR}/v714/sys/java/jre/glnx86/jre/lib/i386/native_threads:${MATLABMCR}/v714/sys/java/jre/glnx86/jre/lib/i386/server:${MATLABMCR}/v714/sys/java/jre/glnx86/jre/lib/i386 export XAPPLRESDIR=${MATLABMCR}/v714/X11/app-defaults #update library LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${MATLABLIB}
Change line MATLABMCR with your MCR folder location. Log off from your system to update system library. After preparation complete, create matlab file. Example create foo.m file.
function y = foo(x) y = x*10;
compile this code from command matlab with :
mcc -B csharedlib:libfoo foo.m
from this command, matlab will create some file output. That file ie:
libfoo.c libfoo.exports libfoo.h libfoo_mcc_component_data.c libfoo.prj libfoo.so mccExcludedFiles.log readme.txt
Create C code, give a name foowrap.c :
/* Include the MCR header file and the library specific header file * as generated by MATLAB Compiler */ #include "mclmcr.h" #include "libfoo.h" #include <time.h> #include <stdio.h> int main(int argc, char **argv) { mxArray *x_ptr; mxArray *y_ptr=NULL; double *y; double ret; float input; size_t t1, t2; t1 = time(NULL); /* Call the MCR and library initialization functions */ if( !mclInitializeApplication(NULL,0) ) { fprintf(stderr, "Could not initialize the application.\n"); exit(1); } if (!libfooInitialize()) { fprintf(stderr,"Could not initialize the library.\n"); exit(1); } sscanf(argv[1], "%f", &input); /* Create an mxArray to input into mlfFoo */ x_ptr = mxCreateDoubleScalar(input); /* Call the implementation function */ /* Note the second input argument should be &y_ptr instead of y_ptr*/ mlfFoo(1,&y_ptr,x_ptr); /* The return value from mlfFoo is an mxArray so we must extract the data from it */ y = mxGetPr(y_ptr); ret = *y; /* Print a double precision number*/ printf("The output of foo is %f\n",ret); /* Call the library termination function */ libfooTerminate(); mxDestroyArray(x_ptr); mxDestroyArray(y_ptr); mclTerminateApplication(); t2 = time(NULL); div_t divresult; divresult = div (t2-t1, 60); warn("Process time = %d min %d sec\n", divresult.quot, divresult.rem); return 0; }
and create Makefile (change include and lib folder with your include and lib matlab installed location) :
SOURCE=foowrap.c MATLABDIR=/data/ProgramFiles/MatlabR2010b/ INCLUDELOC=-I${MATLABDIR}extern/include/ -I./ LIBLOC=-L${MATLABDIR}runtime/glnx86/ -L./ FLAGS=-lm -lfoo -lmwmclmcrrt all: gcc foowrap.c $(INCLUDELOC) $(LIBLOC) $(FLAGS) -o foowrap clean: rm foowrap
Executed code with command make. After compiling is success, add library path from libfoo.so at your library from konsole. Type this command :
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./
Executed output from console with command : ./foowrap 7
This is output from this command :
./foowrap 7 The output of foo is 70.000000 foowrap: Process time = 0 min 3 sec
This is a simple example to call Matlab function from C/C++. Slower process related with matlab library initialization and destroying.
You can donwload the complete source code at here. Dont forget to read “how_to_running.txt: file.
Source : http://note.sonots.com/Mex/Mcl.html http://www.mathworks.com/support/solutions/en/data/1-2QTWCE/index.html?solution=1-2QTWCE
Totosugito,
Thank you for your post. Its really helpful. Unfortunately I am not able to run your example. I think the problem is with my make file. If you don’t mind, I have a few questions.
1) You have sighted foo.m, libfoo, and foowrap.c, so where does mlfFoo come from?
2) In Makefile, is there a reason that you have written
-I/data/ProgramFiles/Matlab2008/extern/include/ twice?
3) In Makefile, what is the point of -lm -lfoo -lmwmclmcrrt?
4) Would this code be different for CPP?
5) How would you use MATLAB functions from a nested CPP function?
Thank you for your wonderful sight,
David K
Hi David,
Thank you for visiting my site. Please look in my site http://toto-share.com/2011/01/create-cc-shared-library-with-matlab-compiler/, I have update all code and attach the complete source code.
This is my answer for your question :
1. When we compile a matlab code with command “mcc -W lib:libfoo -T link:lib foo.m bar.m”, matlab will create libfoo.c and libfoo.h. Matlab translate m file function foo to mlFoo function in C/C++. Please check libfoo.c and libfoo.h file.
2. I have remove duplicate line
3. -lm -lfoo -lmwmclmcrrt is a library used for this code. -lm is math library for C/C++. -lfoo is foo lib file (we get this file from “mcc -W lib:libfoo -T link:lib foo.m bar.m” command). -lmwmclmcrrt is maltab MCR library.
4. I think the different command is when create library from matlab code. You can check at mcc documentation.
5. I dont try to use complex function from Matlab and call from C/C++, because they very slow. But, I think that is like we call other function from C/C++.
Hi Totosugito,
This tutorial is great.
I got a problem when I do the process you posted.
I have already compiled the project and also run it.
However,
if (!libfooInitialize())
{
fprintf(stderr,”Could not initialize the library.
“);
exit(1);
}
Here, can not initialize the library…
Do you know what is the problem?
Thanks a lot.
I think the problem is when compile the matlab code to dll/so (library file) using mcc compiler. Are you get an error message when compile using mcc ?
Hi Totosugito,
I have a doubt, is it possible to convert a deep learning program in matlab to C++ so that I can use it in raspberry pi.