How unpleasant I may find it, sometimes I have to use some tools in Matlab at work. Today I stumbled on a problem with compiling MEX-files written in C++ on my Ubuntu 8.04 (Hardy Heron) box. To illustrate, I'll use the example C++ MEX file mexcpp.cpp
provided with Matlab (Matlab r2007a) in the extern/examples/mex/
folder.
Out-of-the-box compilation with
mex mexcpp.cpp
gave the following warning:
Warning: You are using gcc version "4.2.4". The earliest gcc version supported with mex is "4.0.0". The latest version tested for use with mex is "4.2.0". To download a different version of gcc, visit http://gcc.gnu.org
Despite the warning a MEX-file mexcpp.mexglx
was generated. However, its usage resulted in the following:
Invalid MEX-file '/foo/bar/mexcpp.mexglx': /matlabdir/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by /foo/bar/mexcpp.mexglx).
As a sidenote: compiling MEX-files written in C also caused the warning message, but using the compiled MEX-files did not fail.
A bit of googling ('a bit' is unfortunately an understatement, I almost lost a whole day) brought me to this thread in the Ubuntu forums. The fix (workaround actually) is, in a nutshell, to listen to the warning message mentioned above: use an older compiler.
The GCC version in Ubuntu 8.04 is 4.2.4 by default (at time of this writing on my setup). I decided to install version 4.1 (no worries, different versions of GCC compilers can coexist):
sudo apt-get install gcc-4.1 g++-4.1
Then, I had to tell the mex
tool to use version 4.1 instead of the default. To do so, one has to create a settings file mexopts.sh
, which can be generated with
mex -setup
This created a file ~/.matlab/R2007a/mexopts.sh
in my case. In this file, I went to the section for the right architecture ('glnx86' in my case) and changed the appropriate variables:
CC='gcc-4.1'
...
CXX='g++-4.1'
Just recompiled the MEX-file and no more warnings or error messages. kthxbye