C++-MEX files compilation problems under Ubuntu 8.04 (Hardy)

23 September, 2009 - 18:09

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

23 February, 2011 - 10:45

yaah, it worked. Just a

Anonymous (not verified)

yaah, it worked. Just a comment below on something missing:
Also remember to copy back the edited .sh file back to from where it was copied for you to edit. Only then will the edit work.

17 September, 2010 - 14:33

Command line arguments

Johan Pauwels (not verified)

You can actually provide the compiler as an argument to the 'mex' program so that you don't have to fiddle with the 'mexopts.sh' file.
mex CC=g++-4.1 CXX=g++-4.1 LD=g++-4.1 mexcpp.cpp

As usual, it's described in the help :-)
mex -help

1 September, 2010 - 20:09

thanks !

Luis (not verified)

this worked ! I hate these workarounds, why does MATHWORKS update their compiler support?

12 March, 2010 - 06:51

Thnx a lot !!!!

Freaky (not verified)

Thnx a lot !!!!

5 March, 2010 - 22:21

Thanks!

Simen K. (not verified)

Great post -- saved my day! :-D

10 February, 2010 - 23:20

Thank You

Saravanan (not verified)

Thanks Pal. This post was a life saver !

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options