Friday, January 14, 2011

Compiling C/C++ code from Matlab on Ubuntu Linux machine

It seems that there is much easier way of compiling and running C/C++ from your Matlab application, than the one I was trying before.

Reproducing the instructions on this blog here:

My version of gcc (4.4) is too high for Matlab R2009b (4.3), and Matlab chokes on it. The solution is as follows:

Install gcc-4.3:

sudo apt-get install gcc-4.3
sudo apt-get install g++-4.3

In matlab, run mex -setup and select the GCC compiler.(Choose option 2). Next, at the command line:

sudo gedit ~/.matlab/R2009b/mexopts.sh

Replace all instances of CC=’gcc’ with CC=’gcc-4.3′, and al instances of CXX='g++' with CXX='g++-4.3'.
And restart Matlab !

Compile gcc from souce on Ubuntu 10.04.1 LTS

To run C/C++ code using the mex utility of Matlab 7.11.0 (R2010b I needed to compile a different version of GCC than what was already installed on my machine. (I had the gcc version 4.4.1-4ubuntu9 and the currently supported version with mex is 4.3.4)

So I downloaded gcc-4.3 release from here.
Untarred the file: tar -jxvf gcc-4.3.4.tar.bz2

And now when I tried to use the ./configure from inside the gcc-4.3.4 folder, the system complained that it could not find two necessary API's namely GMP and MPFR.

The issue can be resolved by doing the following. We need to (1) download the GMP and MPFR libraries separately , (2) place them inside the GCC folder and then, (3) tell the configure script to look for it there.
tar -jxf gcc-4.3.2.tar.bz2
tar -jxf mpfr-2.3.2.tar.bz2
mv mpfr-2.3.2 gcc-4.3.2/mpfr
tar -jxf gmp-4.2.4.tar.bz2
mv gmp-4.2.4 gcc-4.3.2/gmp
cd gcc-4.3.2
./configure --with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs
make
make install

Hopefully, that should solve the issues !