Thursday, May 19, 2011

Compiling MEX files on Ubuntu 11.04 amd64 with MATLAB 2011a, 2010b, and 2010a

I use MATLAB a lot in my day job, and my recent in-place upgrade from maverick (Ubuntu 10.10) to natty (Ubuntu 11.04) on my 64-bit workstation broke mex and emlmex in MATLAB versions ranging from 2009b through 2011a. Another user on the Ubuntu forums reported the following error which I have confirmed for both commands:

/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by /usr/lib/libppl_c.so.2)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libppl_c.so.2)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by /usr/lib/libppl.so.7)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libppl.so.7)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libgmpxx.so.4)


Mathworks support suggested checking my installation of gcc-4.3, which is kind of funny since natty has removed gcc-4.3 from the list of available packages. I wasted a bunch of time trying to get gcc-4.3 running on my natty install to support MEX functions before I discovered a simple workaround.

My fix assumes that you can run MATLAB under Ubuntu 11.04. To do so without warning messages required a symbolic link described on Morgan Bye's blog.

Note that Ubuntu 10.10 used gcc-4.4 as the system default gcc, and that MATLAB was happy to compile MEX files using that version. Since Ubuntu still supports the 4.4.x packages, installation with dependencies is as easy as APT:

$ sudo apt-get install gcc-4.4 g++-4.4 gfortran-4.4

At the MATLAB command prompt, execute mex -setup and select either option 1 or 2 to ensure you have a MEX options file in your home directory: ~/.matlab/mexopts.sh. Edit this file with your favorite text editor, e.g. vim ~/.matlab/mexopts.sh. And do the following:
  • Change each instance of gcc to gcc-4.4
  • Change each instance of g++ to g++-4.4
  • Change each instance of gfortan to gfortan-4.4
You should now be able to use MEX or EMLMEX on your Ubuntu 11.04 system.