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.)