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 :

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.

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:

Create C code, give a name foowrap.c :

and create Makefile (change include and lib folder with your include and lib matlab installed location) :

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 :

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