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
5 Comments

Add a Comment

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