Getting gcc to work with Matlab in Ubuntu to compile mex files

Matlab R2009b, Lucid Lynx

My version of gcc (4.4) is too high for Matlab R2009b (4.1 and 4.2), and Matlab chokes on it. The solution is as follows:

Install gcc-4.2:

sudo apt-get install gcc-4.2
sudo apt-get install g++-4.2

In matlab, run mex -setup and select the GCC compiler (I believe you will have to have sudo’d into Matlab for this to work). Next, at the command line:

sudo gedit ~/.matlab/R2009b/mexopts.sh

Replace all instances of CC=’gcc’ with CC=’gcc-4.2′. While you’re at it, replace -ansi with -std=c99 if you want to be able to use C++ style // comments.

DON’T RERUN mex -setup, since this overwrites mexopts.sh. That’s all.

Matlab R2011a, Natty Narwhal

There is a similar issue with newer versions of Matlab and Ubuntu (R2011a requires gcc 4.3). gcc-4.3 is not currently available as a package for Natty, you must first add the appropriate repository from Maverick. Thus:

Go to System > Administration > Synaptic Package Manager, then Settings > Repositories > Other Software, and click Add… In the box, write:
deb http://archive.ubuntu.com/ubuntu maverick universe
OK, close, Reload, then exit.

Then follow the instructions for R2009b, replacing 4.2 with 4.3. Should work as is. However, in my setup Matlab was giving me a weird error when starting it from the command line:

/usr/local/MATLAB/R2011a/bin/util/oscheck.sh: 605: /lib64/libc.so.6: not found

I don’t think this is a common error, but rather is due to my upgrade path. In any case, creating these two symlinks fixed the issue:

sudo ln -s /lib/x86_64-linux-gnu/libc.so.6 /lib64/libc.so.6
sudo ln -s /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib64/libgcc_s.so.1

About these ads

11 thoughts on “Getting gcc to work with Matlab in Ubuntu to compile mex files

  1. I did what you said above ,but It didn’t work!My matlab error still exist:matlab/bin/util/oschec/sh:605:/lib/libc.so.6: not found.
    By the way,after I did this “sudo ln -s /lib/x86_64-linux-gnu/libc.so.6 /lib64/libc.so.6″,I found that their link target didn’t change ,both of them link to “lib/x86_64-linux-gnu//libc.2.13″,can you tell me why?

  2. I have a new problem now though when i edited the gcc options file in order for matlab 2009b to recognise gcc 4.2 but i get this error.

    I followed your instructions

    My Error:

    cc1plus: warning: command line option ‘-std=c99’ is valid for C/ObjC but not for C++ [enabled by default]

    Any help is appreciated! Thanks!

  3. Thanks a lot for this! When I tried compiling mex-files with Ubuntu 11.10 and Matlab 2011b I got similar complaints about the version of gcc. I also got errors of the kind “error: expected expression before ‘/’ token”, which I didn’t really understand until I read your post. After changing the occurences of -ansi with -std=c99 in the mexopts.sh file, the files finally compiled! It turned out that for my case, the version of gcc wasn’t really a problem – only the C++ comments were.
    ===that’s right! thanks

  4. Thanks a lot for this! When I tried compiling mex-files with Ubuntu 11.10 and Matlab 2011b I got similar complaints about the version of gcc. I also got errors of the kind “error: expected expression before ‘/’ token”, which I didn’t really understand until I read your post. After changing the occurences of -ansi with -std=c99 in the mexopts.sh file, the files finally compiled! It turned out that for my case, the version of gcc wasn’t really a problem – only the C++ comments were.

  5. It’s not mex-setup, it’s mex -setup (with a space). This may not have been obvious with the font I was using, so I changed the formatting on the post.

  6. Hi,
    I’ve had the same problem as you had, but I can’t download the package gcc-4.2 with synpatic because it said that there’s no link to download it, so I downloaded it from other sites and installed it manually. Now I’ve no idea how to run mex-setup!! Did you mean the mex_setup.m file which exist in matlab directory??…how can I select the gcc compiler for that file??…cause when I run this file I’ve got nothing and it ain’t make any sense.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s