Thursday, May 19, 2011

Compiling MEX files on Ubuntu 11.04 amd64 with MATLAB 2011a, 2010b, and 2010a

I use MATLAB a lot in my day job, and my recent in-place upgrade from maverick (Ubuntu 10.10) to natty (Ubuntu 11.04) on my 64-bit workstation broke mex and emlmex in MATLAB versions ranging from 2009b through 2011a. Another user on the Ubuntu forums reported the following error which I have confirmed for both commands:

/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by /usr/lib/libppl_c.so.2)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libppl_c.so.2)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by /usr/lib/libppl.so.7)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libppl.so.7)
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1: /usr/local/MATLAB/R2011a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libgmpxx.so.4)


Mathworks support suggested checking my installation of gcc-4.3, which is kind of funny since natty has removed gcc-4.3 from the list of available packages. I wasted a bunch of time trying to get gcc-4.3 running on my natty install to support MEX functions before I discovered a simple workaround.

My fix assumes that you can run MATLAB under Ubuntu 11.04. To do so without warning messages required a symbolic link described on Morgan Bye's blog.

Note that Ubuntu 10.10 used gcc-4.4 as the system default gcc, and that MATLAB was happy to compile MEX files using that version. Since Ubuntu still supports the 4.4.x packages, installation with dependencies is as easy as APT:

$ sudo apt-get install gcc-4.4 g++-4.4 gfortran-4.4

At the MATLAB command prompt, execute mex -setup and select either option 1 or 2 to ensure you have a MEX options file in your home directory: ~/.matlab/mexopts.sh. Edit this file with your favorite text editor, e.g. vim ~/.matlab/mexopts.sh. And do the following:
  • Change each instance of gcc to gcc-4.4
  • Change each instance of g++ to g++-4.4
  • Change each instance of gfortan to gfortan-4.4
You should now be able to use MEX or EMLMEX on your Ubuntu 11.04 system.

20 comments:

  1. Thanks, your post has exactly what i needed.

    ReplyDelete
  2. Worked for me. Thanks a bunch!

    ReplyDelete
  3. it compiled OK for me, but I'm still getting an error with the .mexa64 file:
    ??? Invalid MEX-file '/home/cparmer/VarietyPack/Programming/Lessons/Mex/import_matrix.mexa64':
    /home/cparmer/MATLAB/R2011a/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by
    /home/cparmer/VarietyPack/Programming/Lessons/Mex/import_matrix.mexa64)

    ReplyDelete
  4. Hi Christopher,

    I don't have any insight to your post-compile problem. If your MEX function includes external C-language source code, perhaps it has additional dependencies outside of MATLAB? In my case, I'm compiling pure MATLAB (to mexa64).

    ReplyDelete
  5. Thanks a bunch!! Your post saved my day :)

    ReplyDelete
  6. Could you help me?
    I'm new in Ubuntu. I made all the steps but I can't compile this .mex file "http://www.cs.berkeley.edu/~smaji/projects/fiksvm/" yet. Could you compile this to verify if it's the problem?

    ReplyDelete
  7. Hi Halcon,
    How about you try to verify it yourself by starting with a very simple mex file like the "hello world" example in the MATLAB mex documentation?

    ReplyDelete
  8. Great post (and shame on Matlab QA ..). You saved my day also (unfortunately, i didn't avoid spending a day on trying to install gcc-3.4 :( )

    One question:
    Natty comes with gcc-4.5, so naturally i tried your method with 4.5 instead of 4.4.
    It didn't work.
    Any idea why?

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. I have the following fix which works with stock Natty gcc/g++ compiler (mine is 4.5.2-8ubuntu4). If you use this workaround, you do NOT need to install gcc/g++ 4.4 or edit mexopts.sh.

    Do the following at terminal:

    $ cd /usr/local/MATLAB/R2011b/sys/os/glnxa64

    [this assumes default install location on 64-bit Ubuntu]

    $ sudo rm libstdc++.so.6
    $ sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14 libstdc++.so.6

    [the target libstdc++.so.6.X.XX version may vary]

    I'm not sure about gfortran compilation as I only use C/C++ code.

    Hope that helps anyone out there with the same problems!

    Cheers,
    Mike

    ReplyDelete
  12. $ sudo rm libstdc++.so.6
    $ sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14 libstdc++.so.6

    When I try this the matlab doesnt start-up at all..

    ReplyDelete
  13. Hi vijay,

    Although I haven't used schmichael's method, I might be able to help. Did you make sure that /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14 exists on your system? In my up-to-date Ubuntu Oneiric install, I have /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16 rather than version 6.0.14. It's important to make sure you are linking to an actual file. Good luck.

    ReplyDelete
  14. Thank you very much! schmichael's method and the solution of the post worked perfectly for me, with ubuntu 11.04

    I must add the issue that after running a mex-compiled file, the comand `exit' doesn't work any more (but exit(0) works well)...

    ReplyDelete
  15. You turned out to be my savior! Spent lot of time before ending up here and your method worked like a charm. Thanks!

    ReplyDelete
  16. Thnx, schmichael's method worked perfectly for me :)

    ReplyDelete
  17. After applying Schmichael's method I was able to compile with MEX,
    but the created function gives a segmentation error (see below).
    Here is what I get:

    > Test
    Testing the spherical Fourier transform code
    Try all individual spherical harmonics from j=0 .. 2*bandwidth
    Watch for aliasing once j>bandwidth
    Press any key to start

    bandwidth =

    5




    ****************
    Testing with j=0, m=0:

    ------------------------------------------------------------------------
    Segmentation violation detected at Wed Jan 9 16:59:59 2013
    ------------------------------------------------------------------------

    Configuration:
    MATLAB Version: 7.9.0.529 (R2009b)
    MATLAB License: 161051
    Operating System: Linux 2.6.38-12-generic #51-Ubuntu SMP Wed Sep 28 14:27:32 UTC 2011 x86_64
    GNU C Library: 2.13 stable
    Window System: The X.Org Foundation (11001000), display :0.0
    Current Visual: 0x23 (class 4, depth 24)
    Processor ID: x86 Family 6 Model 10 Stepping 7, GenuineIntel
    Virtual Machine: Java 1.6.0_12-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
    Default Encoding: UTF-8

    Fault Count: 1

    Register State:
    rax = 0000000000000001 rbx = 0000000000000001
    rcx = 00007fcda5145370 rdx = 0000000000000001
    rbp = 00007fcdaaebb540 rsi = 00007fcdaaebb540
    rdi = 0000000000000004 rsp = 00007fcdaaebb468
    r8 = 0000000000000000 r9 = 0000000000000001
    r10 = 0000000000000b90 r11 = 0000000000000000
    r12 = 0000000000000001 r13 = 0000000000000001
    r14 = 0000000000000000 r15 = 0000000000000000
    rip = 0000000000000000 flg = 0000000000010202

    Stack Trace:
    [0] 0x0000000000000000(0x7fcda40d2000, 0, 16, 0x7fcda4a3d8b0)

    This error was detected while a MEX-file was running. If the MEX-file
    is not an official MathWorks function, please examine its source code
    for errors. Please consult the External Interfaces Guide for information
    on debugging MEX-files.

    If it is an official MathWorks function, please
    follow these steps to report this problem to The MathWorks so we
    have the best chance of correcting it:

    The next time MATLAB is launched under typical usage, a dialog box will
    open to help you send the error log to The MathWorks. Alternatively, you
    can send an e-mail to segv@mathworks.com with the following file attached:
    /home/panos/matlab_crash_dump.20315

    If the problem is reproducible, please submit a Service Request via:
    http://www.mathworks.com/support/contact_us/ts/help_request_1.html

    A technical support engineer might contact you with further information.

    Thank you for your help. MATLAB may attempt to recover, but even if recovery appears successful,
    we recommend that you save your workspace and restart MATLAB as soon as possible.

    Caught MathWorks::System::FatalException
    [Please exit and restart MATLAB]>>

    ReplyDelete