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 ( 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 at your library from konsole. Type this command :


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 :


Hi, This is my notes when I try something. Maybe, this is NOT THE BEST SOLUTION for your problems. If you have other method or idea that better with my post, please share in this blog. Thank for visiting my site.

More Posts - Website

4 thoughts on “Create C/C++ shared library with MATLAB Compiler

  1. 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, 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++.

  2. 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.


    if (!libfooInitialize())
    fprintf(stderr,”Could not initialize the library.

    Here, can not initialize the library…

    Do you know what is the problem?

    Thanks a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *