Packaging matlab to standalone

From Computational Biology Group

Jump to: navigation, search



This is about compiling matlab code (.m file) and packaging it into a standalone executable.

Converting .m files can be done easily with the mcc command. It will compile the code with gcc (beware of which gcc version you are using) and provide shell script to run the executable.

To run the code, you will also need the Matlab Compiler Runtime (MCR) which contains run time components.

The MCR is needed for running any compiled standalone. Please note, the MCR is Matlab version dependent, so make sure the target machine is running the same MCR than the compilation machine ! In some cases, a standalone can be kernel dependent ...

Building/finding the MCR

With Matlab version 2008, one can build the MCR using self-extracting files :

PlatformFile self-extracting file location
Windows 32-bit MCRInstaller.exe matlabroot\toolbox\compiler\deploy\win32
Windows 64-bit MCRInstaller.exe matlabroot\toolbox\compiler\deploy\win64
Linux (glnx86) MCRInstaller.bin matlabroot/toolbox/compiler/deploy/glnx86
Linux (glnxa64) MCRInstaller.bin matlabroot/toolbox/compiler/deploy/glnxa64
Mac MCRInstaller.dmg matlabroot/toolbox/compiler/deploy/mac
Mac intel MCRInstaller.dmg matlabroot/toolbox/compiler/deploy/maci
Solaris (sol64) MCRInstaller.bin matlabroot/toolbox/compiler/deploy/sol64

Where matlabroot is where your matlab was installed (i.e. /usr/bin/matlab2008b_PDE/ )

In older Matlab version (< 2008), one included a file into the distribution. This zip file could be created by running the buildmcr command. This function is now deprecated.

Compiling .mat files with mcc

Let say I have a main function callCNVs.m and that all helper functions are either standard Matlab functions or under a fun/ directory.

My callCNVs.m function looks like :

function callCNVs(input_file, varargin)

if nargin < 1
    error('Missing input file');
fprintf('Processing file %s\n', input_file);

I can then create my small distribution with

mcc -m -I /home/armand/MATLAB/COLAUS -I /home/armand/MATLAB/COLAUS/fun -d /home/armand/MATLAB/COLAUS_COMPILED callCNVs.m

Where -I specify which directories to include for compilation, -d the destination directory and callCNVs.m the main function.

This will convert callCNVs.m to C, compile it and link functions that are in the include directories.

Looking into /home/armand/MATLAB/COLAUS_COMPILED, I now have :

callCNVs:                      ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, \
dynamically linked (uses shared libs), not stripped
callCNVs_main.c:               ASCII C program text
callCNVs_mcc_component_data.c: ASCII C program text
callCNVs.prj:                  XML document text
mccExcludedFiles.log:          ASCII English text
readme.txt:                    Matlab v5 mat-file               Bourne shell script text executable

To run the standalone, I simply use the script :

$ sh /usr/bin/matlab2008b_PDE/ test.dat
Setting up environment variables
LD_LIBRARY_PATH is .:/usr/bin/matlab2008b_PDE//runtime/glnxa64:/usr/bin/matlab2008b_PDE//bin/glnxa64:/usr/bin/matlab2008b_PDE//sys/os/glnxa64:\

Processing file test.dat


Error while loading libraries

error while loading shared libraries: cannot open shared object file: No such file or directory

Most likely your LD_LIBRARY_PATH is not pointing correctly to the MCR. The run_[appName].sh script should do it for you.

$ sh /path-to-my-MCR/v79 test.dat # i.e. for compiled distrib with Matlab 2008, build v79