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.
4.  
If you have your code its fine. 
or this is a sample code(hello.c :P)

//SAMPLE1
#include <mex.h>

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


//SAMPLE2 

#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);
for(i=0;i<r;i++)
{
    avg=0.0;
    for(j=0;j<c;j++)
        avg += *(xValues++);
   
    avg = avg/c;
    printf("avg of column %d is %f \n",i+1, avg);
}

}

A fast short tutorial on creating mex files:
pdf 
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 
/usr/local/MATLAB/R2012a/sys/os/glnxa64/libstdc++.so

(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);
 

References: 
link1
link2
link3


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

4 comments:

  1. Hi Devendra,
    Sorry to disturb you. I am compiling a mex file on 64bit fedora 20 and I get the following error:
    **************************************
    >> mex em_ghmm.c

    /usr/local/MATLAB/R2012a/bin/mex: ligne305: gcc-4.4 : commande introuvable

    Warning: You are using gcc version "". The version
    currently supported with MEX is "4.4.6".
    For a list of currently supported compilers see:
    http://www.mathworks.com/support/compilers/current_release/

    /usr/local/MATLAB/R2012a/bin/mex: ligne1326: gcc-4.4 : unfound command

    mex: compile of ' "em_ghmm.c"' failed.

    Error using mex (line 206)
    Unable to complete successfully.
    ***************************************************

    I've tried your procedure without success.
    What would be the problem?
    Thank for your help!!

    Anatole GOUNDAN

    ReplyDelete
  2. The folloowing error produces:
    error: expected identifier or ‘(’ before ‘/’ token

    ReplyDelete
  3. Excellent! Got the same problem after reinstalling ubuntu 12.04.5 LTS
    Your solution work for me

    Regards
    Olivier

    ReplyDelete
  4. Don't forget to execute "mex -setup" again after modifying CLIBS="$CLIBS -L/usr/local/MATLAB/R2012a/sys/os/glnxa64 -lstdc++" line. ;)

    ReplyDelete