Popular Posts

Monday, October 1, 2012

Compiling mex files on 64bit Linux(UBUNTU) using MATLAB 2012

I could not setup the compiler for myself to create mex files. So I searched the internet, going through many blogs and posts. Finally I got a tutorial and 2 posts which I have almost copied shamelessly(maybe for my own reference.. :) ). The source link is at the end of the post.

This is a simple guide to compiling and running mex files from MATLAB R2012a on Ubuntu 12.04 64bit

I compiled my sweet hello.c file(You can get this hello.c in this text DOWN)
compile hello.c using the command in MATLAB:

>> mex hello.c

I had no idea what the problem was. But I was getting a warning that
Warning: You are using gcc version "4.6.3-1ubuntu5)".  The version currently supported with MEX is "4.4.6".

Although I searched the internet and few blogs told I need not downgrade to 4.4.6.(But still I installed gcc-4.4 on top of gcc-4.6)

run this command in terminal: 
sudo apt-get install gcc-4.4

The gcc version compatible with MATLAB R2012a is gcc-4.4 while the version pre-installed on Ubuntu 12.04 is gcc-4.6

Just follow these steps:

1. Open terminal and type
sudo gedit /usr/local/MATLAB/R2012a/bin/mexopts.sh
(OR where you installed your MATLAB)

2. Change 'gcc' to 'gcc-4.4' ,'g++' to 'g++-4.4' , 'gfortran' to 'gfortran-4.4' at all instances of  CC = 'gcc' , CXX = 'g++' and FC = 'gfortran'.
Save the file and exit.

3. Open MATLAB and type:
mex -setup (in the Command line).

MATLAB will show the following:

The options files available for mex are:

  1: /usr/local/MATLAB/R2012a/bin/mexopts.sh :
      Template Options file for building gcc MEX-files

  0: Exit with no changes

Enter the number of the compiler (0-1):

Select 1.

The setup is complete. Now time to test it.
If you have your code its fine. 
or this is a sample code(hello.c :P)

#include <mex.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  mexPrintf("Hello World!\n");


#include "mex.h"
#include "stdio.h"
#include "matrix.h"

void mexFunction(int nlhs,mxArray *plhs[],int nrhs, const mxArray *prhs[])

mxArray *xdata;
double *xValues;
int i,j,r,c;
double avg;

xdata = prhs[0];
xValues = mxGetPr(xdata);

r = mxGetN(xdata);
c = mxGetM(xdata);
        avg += *(xValues++);
    avg = avg/c;
    printf("avg of column %d is %f \n",i+1, avg);


A fast short tutorial on creating mex files:
epub (looks good.. I mean soothing to eyes.. :P)
Save the text file as "hello.c"

Again compile your sweet "hello.c" in MATLAB command line:
>> mex hello.c

Now I got the following warning.

/usr/bin/ld: cannot find -lstdc++

Shit got real!!! 


Don't Panic!!!

To fix this you need to find your mexopts.sh file and change the line
 CLIBS="$CLIBS -lstdc++"
 TO  (for 64bit)
 CLIBS="$CLIBS -L/usr/local/MATLAB/R2012a/sys/os/glnxa64 -lstdc++"

 OR  (for 32bit)
 CLIBS="$CLIBS -L/usr/local/MATLAB/R2012a/sys/os/glnx86 -lstdc++"
 Please search the path and see that it exists.

obviously, you’ll need to change /usr/local/MATLAB/ to wherever you actually installed MATLAB.

Your next step is to do the following in a bash prompt

ln -s /usr/local/MATLAB/R2012a/sys/os/glnxa64/libstdc++.so.6 

(Please see that the path exists. Maybe /usr/local/MATLAB/R2012a/sys/os/glnx86

again – substituting wherever you installed MATLAB for /usr/local/MATLAB/

5. Now change your MATLAB folder to the folder you saved your text file in and type this in the command window:  

>>mex hello.c
It compiles. :)
6. The file "hello.mexglx" or "hello.mexa64" depending on your OS (32 bit / 64 bit) will show up in the same directory. 
7. Now run it from MATLAB.

(for SAMPLE1)
>> hello

(for SAMPLE2)
>> x = [1 2; 3 4];
>> hello(x);


Also came to know a few things about Matlab (In next post.. :) )