Matlab R2013a: C++ MEX on Ubuntu 14.04 64-bit

Note: the way Mex setup works has changed in R2014a and newer. This is for older version of Matlab R2013 and older

 

Ubuntu 14.04 comes with GCC 4.8, but Matlab R2013a gives warnings about needing GCC 4.4 if using MEX features.

(1) Packages to install (synaptic or apt-get)

gcc-4.4
g++-4.4

(2) in Matlab, type mex -setup and press 1 to copy the mexopts.sh to your ~/.matlab/R2013a/mexopts.sh

(3) sudo chmod 644 ~/.matlab/R2013a/mexopts.sh
(allows you to edit this file)

(3) gedit mexopts.sh
and change:

CC='gcc-4.4'
CXX='g++-4.4'

Also to save yourself grief when you’re using a modern C++ syntax file (especially if you get errors like
error: expected expression before ‘/’ token
)
then make this additional changes:

#CFLAGS='-ansi -D_GNU_SOURCE'
CFLAGS='-std=c99 -D_GNU_SOURCE'

(4) Save/exit gedit and restart Matlab. You should now be able to use MEX in Matlab R2013a under Ubuntu 14.04.

8 thoughts on “Matlab R2013a: C++ MEX on Ubuntu 14.04 64-bit”

  1. Hi, thanks for the 13.04-related explanation.
    How about gfortran ? supported version 4.3 is not in the synaptic packages anymore, but i’d like to have a fully supported setup, i’m having some trouble with compiling so gfortran functions and their gateways with gfortran 4.7. Any hint on what/how i should do ? Thanks

  2. Natael,

    I have looked into this gfortran-4.3 on Ubuntu 13.04, and I don’t see that anyone has yet made a PPA. I think at present you might have to compile gcc-4.4 and gfortran-4.3 from source. That would take some time I think.

    Maybe a simpler workaround would be to compile your fortran code outside of matlab, setting your fortran code to read/write files for input/output. You can then consider using /dev/shm (RAM drive) as a speedy method of I/O (>1GB/sec). You use the /dev/shm RAM drive to pass data between matlab and your gfortran code–call the gfortran code from within Matlab using the SYSTEM() function.

    I would consider doing this first as if it’s fast enough, you might save a day’s time of figuring out how to compile GCC/Gfortran from source. I have used this method with success when someone sends me binary (pre-compiled) program and the source code is unavailable. Or if someone sends me a Python program.

  3. Just want to say thanks! This worked perfectly with R2013b as well. But compilers 4.7 can be used.

    when compiling I just had to add the path to the mexopts.sh file like ‘mex -f filelocation src.c’

Leave a Reply

Your email address will not be published.

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>