Dec 082011
 

This is a very quick post describing how to fix an annoyance with Matlab under Linux. I thought I’d share this with the internet to hopefully save the next person who has this trouble some time.

On Linux Matlab R2011b annoyingly requires the fairly old gcc 4.3.4 to compile C/C++ “mex” functions. This is a problem for recent versions of Ubuntu 64-bit. If you attempt to use a more recent version of gcc you get errors like “libstdc++.so.6: version `GLIBCXX_3.4.11′ not found” when trying to run the mex files. This is because matlab uses it’s own libc/stdc++, and only includes them for older compilers.

The fix is to install gcc 3.4 and point matlab to it by editing mexopts.sh. Annoyingly there is no package for gcc 3.4 on Ubuntu 11.10, so you have to build it yourself. The reason for this blog post is that the build isn’t quite “textbook” and it took me a while to get it to work. Hopefully this guide might save some googleing soul the hassle, and I’ll be able to point co-workers to it when they upgrade.

Here’s the commands to build and install gcc 4.3.4. The line which sets “LIBRARY_PATH” is what took a little while to track down, recent ubuntu/debian have reorganised /usr/lib, which unsurprisingly breaks lots of builds. If you don’t do this you’ll get an error saying “/usr/bin/ld: cannot find crti.o: No such file or directory” during build.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# download gcc 4.3.4
wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-4.3.4/gcc-4.3.4.tar.bz2
# extract, make and cd into separate build dir
tar -xvf gcc-4.3.4.tar.bz2tar -xvf gcc-4.3.4.tar.bz2
mkdir gcc-4.3.4-build
cd gcc-4.3.4-build/
# Install build dependencies, this should get all of them
sudo apt-get build-dep gcc-4.5
# the "fix"
export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu
# configure, set prefix so install will only touch the /opt/gcc-4.3.4/ directory
./gcc-4.3.4/configure --prefix=/opt/gcc-4.3.4/
# build, specify number of cpu corse after -j
make -j<number of cpu cores>
# install
sudo make install



Now you just need to point Matlab to this version of gcc.

Run “mex -setup” from matlab to ensure you have a mexopts.sh file – it should tell you where it is. Edit the mexopts.sh (mine was in $HOME/.matlab/R2011b/mexopts.sh):

  • Replace all instances of CC=”gcc” with CC=”/opt/gcc-4.3.4/bin/gcc”.
  • Replace all instances of CXX=”g++” with CC=”/opt/gcc-4.3.4/bin/g++” (and the same for gfortran if you use it).
  • Add a line with “export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu” at the top (this stops more “/usr/bin/ld: cannot find crti.o” errors from happening when you use mex).

Now you should be able to compile and run Matlab mex files.

EDIT: If you’re not running 64-bit ubuntu this guide might work, but you don’t need the “export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu” commands before compiling or added to mexopts.sh. (Or maybe you do but you can loose the “_64″ part.)

 Posted by at 2:41 pm

  11 Responses to “Compiling gcc 4.3.4 under Ubuntu 11.10 for Matlab”

  1. Thank you Luke, you saved me from an ongoing headache for the last 3 days!!! I managed to compile gcc-4.3.4, I just have a problem with Matlab. I modified the mexopts.sh file as you said, but I still get the error “/opt/R2010b/mexopts.sh: line 104: CC: command not found”. Same with g++ and fc. Any suggestions?

  2. It looks like something is wrong with your mexopts.sh – it’s not pointing to the correct paths for gcc/g++/fc.

    Here’s a copy of my mexopts.sh.

    By the way I’m assuming with this guide that you’re running 64 bit (I’ll add that into the post). After the lline in mexopts.sh which says “glnxa64)” is where you need to modify your variables (the others set the compiler for 32-bit, which I assume isn’t being used). Note, for example, that there is a line which says “CC=’/opt/gcc-4.3.4/bin/gcc’”.

    • Thanks, I got it working!

      I run 64bit, however I had a problem compiling. I was getting the error “”/usr/include/gnu/stubs-32.h” no such file.”. The workout is to install glibc-devel.i686 and libgcc.i686 probably too, if you want to compile/link 32-bit stuff on x86-64.

      p.s. I’m running FC14, your guide worked fine with minor modifications regarging the LIBRARY_PATH (=/usr/lib/gcc/x86_64-redhat-linux/) on my machine.

  3. Thank you very much.. that helped a lot.

  4. Thanks. It is very helpful.

  5. [...] Compile gcc-4.3.4 with support for fortran, adapted from here: [...]

  6. Thanks for trying to save my day with this. Matlab should urgently update their compiler support… But for now I went ahead with your method.

    However, when running the make -j command, I run into an error saying some libs are referring to “undefined reference to ‘__cxa_call_unexpected’. You got any idea where this might be coming from?

    Thanks again.

    • Sorry, off hand I have no idea. Maybe if you provide a few more details (ubuntu + matlab + gcc version for example) others might be able to help.

  7. Is a dot missing at the beginning of line 12?

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>