8

I am trying to compile a c++ ubuntu project via matlab here. When I am trying to use it after the compilation with make command, I am getting the following error:

Invalid MEX-file
'////fashionista_v0.2/lib/+bsr/buildW.mexglx':
 //local/MATLAB/R2011a/bin/glnx86/../../sys/os/glnx86/libstdc++.so.6: version
`GLIBCXX_3.4.11' not found (required by
 ////fashionista_v0.2/lib/+bsr/buildW.mexglx)

I am not familiar with those processes, so I couldnt understand the several proposed solutions like that. What is exactly libstdc++ and GLIBCXX and how can I solve the problem?

I am trying to fix the problem using the proposed link from nkjt:

export LD_LIBRARY_PATH=${prefix}/lib:$LD_LIBRARY_PATH

However, due to lack of unix shell knowledge I don't understand what to put in the command. I am have locate libstdc++ .a and .so file which is in gcc folder /usr/lib/gcc/i686-linux-gnu/4.6 and I am trying the following:

 export LD_LIBRARY_PATH=/usr/lib/gcc/i686-linux-gnu/4.6:$LD_LIBRARY_PATH

However, my matlab error still exist. I am using Ubuntu 12.04 version with gcc 4.6 and matlab r2011a.

EDIT: I ve updated the matlab version to r2012a the problem still the same. I also tried the following:

sudo ln -s /usr/lib/cc/i686-linux-gnu/4.6/libstdc++.so libstdc++.so.6 

I got failed to create the file the file already exists.

The output of usr/lib/libstdc++.so.6 | grep GLIBC:

        GLIBCXX_3.4
        GLIBCXX_3.4.1
        ...
        GLIBCXX_3.4.10
        GLIBCXX_3.4.11
        GLIBCXX_3.4.12
        GLIBCXX_3.4.13
        GLIBCXX_3.4.14
        GLIBCXX_3.4.15
        GLIBCXX_3.4.16
        GLIBC_2.0
        GLIBC_2.3
        GLIBC_2.4
        GLIBC_2.3.4
        GLIBC_2.1
        GLIBC_2.1.3
        GLIBC_2.3.2
        GLIBC_2.2
        GLIBCXX_DEBUG_MESSAGE_LENGTH

How can I ensure that I ve defined the right version of libstdc++?

Community
  • 1
  • 1
Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
  • 1
    http://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.how_to_set_paths – nkjt Sep 19 '14 at 09:13
  • So the error here is wrong version of libstdc++ or wrong linking to the library? – Jose Ramon Sep 19 '14 at 09:23
  • 1
    All you have to do is link it to define it as the default version. – SamuelNLP Sep 19 '14 at 13:21
  • Other similar questions: http://stackoverflow.com/q/9959306/97160, http://stackoverflow.com/q/22564357/97160, http://stackoverflow.com/q/16084323/97160, http://stackoverflow.com/q/5216399/97160 – Amro Sep 22 '14 at 14:27

2 Answers2

13

Matlab (and a ton of other commercial programs like Steam, Mathematica, etc.) ships its own version of the libstdc++.so:

/usr/local/MATLAB/R2011a/bin/glnx86/../../sys/os/glnx86/libstdc++.so.6

The problem is that when you start matlab, it loads this one first, and since it is loaded, this version is used to resolve all dynamic loader dependencies.

You compiled with your system GCC and linked to your system's libstdc++, which is newer. The resulting binary then requests symbols of a certain (newer) version and the loader does not find them in the already loaded version (i.e. Matlab's).

There are two ways to solve this:

1. * Delete/rename Matlab's libstdc++.so and symlink your system's version to the exact same name:

    ```
    sudo rm /usr/local/MATLAB/R2011a/bin/glnx86/../../sys/os/glnx86/libstdc++.so.6
    sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/local/MATLAB/R2011a/bin/glnx86/../../sys/os/glnx86/libstdc++.so.6
    ```

* Delete Matlab's version and let your OS's loader pick up the system's `libstdc++`:

    ```
    sudo rm /usr/local/MATLAB/R2011a/bin/glnx86/../../sys/os/glnx86/libstdc++.so.6
    ```

* Use the environment variable `LD_PRELOAD` to "inject" the system's version of `libstdc++` into the execution environment before anything else, which prevents the old Matlab version to be loaded:
    ```
    LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6 matlab
    ```
  1. Install the GCC version Matlab expects and modify the Mex building options (or use update-alternatives) to use that instead of the system's GCC.

Note that for 1-3, you may need to mess with additional libraries like libgcc_s.so, in the same way.

The reason that using the new version works is because of the symbol versioning scheme employed in libstdc++ internally (hence also the detailed error message mentioning the version). A similar "fix" needs to be done for Steam on e.g. Arch Linux, where several system libraries Steam uses are linked to the newer libstdc++.

The real solution is for Matlab not to ship the libstdc++.so and instead use the OS provided version.

Sergey Volkov
  • 391
  • 3
  • 6
rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 2
    +1 well explained. For what it's worth the same project author has troubleshooting steps for this kind of problem on another project here: http://kyamagu.github.io/mexopencv/#linux. So a fourth way to solve this is to use [`LD_PRELOAD`](http://stackoverflow.com/q/426230/97160) to force the OS loader into dynamically loading the system's libstdc++ before it finds that of MATLAB. – Amro Sep 22 '14 at 13:50
  • @Amro you're absolutely right, I amended my answer. Thanks! – rubenvb Sep 22 '14 at 14:12
  • ERROR: ld.so: object '/usr/lib/i386-linux-gnu/libstdc++.do.6' from LD_PRELOAD cannot be preloaded: ignored. matlab: command not found – Jose Ramon Sep 22 '14 at 15:23
  • 1
    @Fere you have a typo there (`.do.6` instead of `.so.6`). Also: seems like `matlab` isn't on your `PATH`. – rubenvb Sep 22 '14 at 15:27
  • Jesus, you are right! Silly typo. Thanks a lot for this well explained answer. – Jose Ramon Sep 23 '14 at 07:30
  • Ok this is weird I run LD_PRELOAD=/usr/lib/i386-linux-gnu/libstdc++.do.6 matlab once and run ok but in the second time I am getting LD_PRELOAD cannot be preloaded: ignored. matlab: command not found – Jose Ramon Sep 23 '14 at 07:36
  • 1
    @FereRes you can also create a bash alias as explained in @Amro's link, or `export` the environment variable. This will affect everything (not only Matlab) you start from that point forward though, which may not be a good idea. – rubenvb Sep 23 '14 at 07:50
  • Basically, second typo :( and I couldnt delete the comment. Thanks again! – Jose Ramon Sep 23 '14 at 07:51
  • Why do you use `/usr/local/MATLAB/R2011a/bin/glnx86/../../sys/os/glnx86/libstdc++.so.6` instead of just `/usr/local/MATLAB/R2011a/sys/os/glnx86/libstdc++.so.6` ? – Louis M Nov 15 '16 at 21:21
2

Link it with something like this, depending on the version.

sudo ln -s /usr/lib/libstdc++.so.6.0.9 libstdc++.so.6
SamuelNLP
  • 4,038
  • 9
  • 59
  • 102
  • libstdc++ exists only in the folder gcc/i686-linux-gnu/4.6/libstdc++.so. Is this the file I am looking for? I am tried sudo ln -s /usr/lib/cc/i686-linux-gnu/4.6/libstdc++.so libstdc++.so.6 however I got the same error. – Jose Ramon Sep 22 '14 at 08:54
  • In what way can I see which libstdc++ is defined as default? – Jose Ramon Sep 22 '14 at 11:23