How to tell mex to link with the libstdc++.so.6 in

2020-01-28 08:40发布

问题:

Now mex in MATLAB 2012a only officially supports gcc 4.4.6 but I want to use gcc 4.7 at my own risk. Now If I compile something with mex directly, it will complain that

/usr/lib/gcc/i686-linux-gnu/4.7/cc1plus:
/usr/local/MATLAB/R2012a/sys/os/glnx86/libstdc++.so.6:
version `GLIBCXX_3.4.15' not found
(required by /usr/lib/i386-linux-gnu/libppl_c.so.4)

/usr/lib/gcc/i686-linux-gnu/4.7/cc1plus:
/usr/local/MATLAB/R2012a/sys/os/glnx86/libstdc++.so.6:
version `GLIBCXX_3.4.15' not found
(required by /usr/lib/i386-linux-gnu/libppl.so.9)

By strings /usr/lib/i386-linux-gnu/libstdc++.so.6 | grep 'GLIBCXX' I confirm that this libstdc++.so.6 has that version string. I reviewed mexopts.sh and modified the variable $RPATH and $MLIBS in that script, but it doesn't work. So if I don't use symbolic link, where can I config the path of the libstdc++.so.6 that mex uses? Thank you.

回答1:

You need to create a symbolic link to the gcc 4.7 library so matlab knows to use it. Something like:

ln -s {/path/to/file-name} {link-name}

If you don't want to use symbolic links, then just define this path in a terminal from which you launch matlab:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/libstdc++.so.6
./matlab


回答2:

/usr/lib/gcc/i686-linux-gnu/4.7/cc1plus: /usr/local/MATLAB/R2012a/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.15' not found

The problem is that when you are building with mex, it puts -L/usr/local/MATLAB/R2012a/sys/os/glnx86 on the link line, and so the linker picks up libstdc++.so from there.

If you can't convince mex to prepend -L/usr/lib/i386-linux-gnu first, then I think your only other choice is to remove /usr/local/MATLAB/R2012a/sys/os/glnx86/libstdc++.so (just rename it to e.g. libstdc++.so.bak).



回答3:

It's a late answer, but I believe the cleanest, most Mathworks-approved and least invasive solution is to edit the .matlab7rc.sh script. This is a script used by the matlab script when you start MATLAB under UNIX-like systems. (See http://www.mathworks.ch/ch/help/matlab/ref/matlabunix.html)

Copy that script (found under {matlabroot}/bin) to the root of your project, or to your home directory. Then tell MATLAB to first search in the system directories for the C++ libraries, instead of its own directories. On my system I changed line 191:

191c191
<       LDPATH_PREFIX='/usr/lib/x86_64-linux-gnu'
---
>       LDPATH_PREFIX=''

(Simply setting LD_LIBRARY_PATH to the empty string is not a good solution, because that will prevent you from loading other third-party libraries.)

When this is done you might get the following message when running mex:

/usr/bin/ld: cannot find -lstdc++

This usually means that g++ is not installed. On a Debian-like system, run:

sudo apt-get install g++

From here on, you might still get an annoying warning about using a version of gcc beyond what is officially supported, but that is harmless and can be ignored.



回答4:

i tried both the answers.. but none worked for me.

however this worked for me. in matlab run this -

setenv('LD_LIBRARY_PATH', '');

just for anyone who is facing the same problem.

P.S: I found this solution here



回答5:

You can modify ~/.matlab/R2012a/mexopts.sh that generated after doing mex -setup by adding a line in glnx86 section:

LD_LIBRARY_PATH='/usr/lib:$LD_LIBRARY_PATH'

or in glnx64:

LD_LIBRARY_PATH='/usr/lib64:$LD_LIBRARY_PATH'


回答6:

If you don't have root access then you can try,
LD_PRELOAD='path/to/libstdc++.so.6.0.21' matlab



回答7:

I was not able to find where my libstdc++.so.6 is, so I was not able to fully test the solution given by geek_girl. However, a modification of th1rdey3's worked. I ran in the matlab console:

setenv('LD_LIBRARY_PATH', 'usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64:/usr/local/MATLAB/R2011a/bin/glnxa64:/usr/lib/boost_1_54_0/libs/regex/build/gcc');

This is the value I use for 'LD_LIBRARY_PATH' when compiling my C++ code in Eclipse (I am not using mex files, instead I create an executable of my C++ code in Eclipse and later run it from matlab shell). In my case the value of 'LD_LIBRARY_PATH' is that long because my C++ code uses boost's regex, matlab libraries (libmat, libmx and so on), GSL library and Armadillo. If you don't use all these libraries, setenv('LD_LIBRARY_PATH','') should be enough, I guess.



回答8:

On Matlab R2015b, I first relinked libstdc++.so.6 and then edited .matlab7rc.sh as described above by @lindelof. On my desktop, from terminal:

locate libstdc++.so.6

In my case, the system library is located in /usr/lib64. Then

cd /usr/local/matlab/sys/os/glnxa64
mv libstdc++.so.6 libstdc++.so.6.bak
ln -s /usr/lib64/libstc++.so.6 libstc++.so.6
cd /usr/local/src/matlab/bin/glnxa64/
mv libstdc++.so.6 libstdc++.so.6.bak
ln -s /usr/lib64/libstc++.so.6 libstc++.so.6

Then edit .matlab7rc.sh in {matlabroot}/bin. Delete in the same directory any mexopts.sh file. Restart Matlab. MEX your file from scratch (this will build a new mexopts.sh file with the new settings. Run it from Matlab console.