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.

5 Comments

Natael posted on July 31, 2013 at 05:41

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

mhirsch posted on July 31, 2013 at 11:21

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.

Ruben Verhack posted on October 10, 2014 at 11:21

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’

Elabbadi posted on November 12, 2014 at 07:48

how to insstall matlab in ubuntu 14.04. I have matlab in my logitheque but it’s not go.
Cdlts

Michael Hirsch posted on November 12, 2014 at 15:45

hi Elabbadi, navigate to the directory of your matlab DVD or downloaded matlab install files and type
sudo ./install

3 Trackbacks

[...] To use MEX for Matlab R2013a on Ubuntu 13.04, you must first have setup GCC 4.4 as described in: http://blogs.bu.edu/mhirsch/2013/07/matlab-r2013a-mex-on-ubuntu-13-04-64-bit/. [...]

[...] http://blogs.bu.edu/mhirsch/2013/07/matlab-r2013a-mex-on-ubuntu-13-04-64-bit/ By mhirsch | Posted in Linux, MATLAB / Octave | Tagged Ubuntu 13.10 [...]

[…] under a system where I can’t easily compile MEX-files (Ubuntu 13.04, Matlab2012b; Update: this issue can be solved), I wasn’t able to check it out myself. Anyway, the paper’s code is shorter than 200 […]

Post a Comment

Your email address is never shared.