Linux 如何告诉 mex 与 /usr/lib 中的 libstdc++.so.6 链接而不是 MATLAB 目录中的链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9959306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:31:54  来源:igfitidea点击:

How to tell mex to link with the libstdc++.so.6 in /usr/lib instead of the one in the MATLAB directory?

linuxmatlablinkermex

提问by ziyuang

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

现在 MATLAB 2012a 中的 mex 仅正式支持 gcc 4.4.6,但我想自担风险使用 gcc 4.7。现在如果我直接用 mex 编译一些东西,它会抱怨

/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.6has that version string. I reviewed mexopts.shand modified the variable $RPATHand $MLIBSin 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.6that mex uses? Thank you.

通过strings /usr/lib/i386-linux-gnu/libstdc++.so.6 | grep 'GLIBCXX'我确认这libstdc++.so.6具有该版本字符串。我mexopts.sh和修改变量$RPATH,并$MLIBS在该脚本,但它不工作。所以如果我不使用符号链接,我可以在哪里配置libstdc++.so.6mex 使用的路径?谢谢你。

采纳答案by geek_girl

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

您需要创建一个指向 gcc 4.7 库的符号链接,以便 matlab 知道使用它。就像是:

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:

如果您不想使用符号链接,则只需在启动 matlab 的终端中定义此路径:

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

回答by Employed Russian

/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

/usr/lib/gcc/i686-linux-gnu/4.7/cc1plus:/usr/local/MATLAB/R2012a/sys/os/glnx86/libstdc++.so.6:未找到版本“GLIBCXX_3.4.15”

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

问题是,当您使用 构建时mex,它会放在-L/usr/local/MATLAB/R2012a/sys/os/glnx86链接行上,因此链接器会libstdc++.so从那里接收。

If you can't convince mexto prepend -L/usr/lib/i386-linux-gnufirst, 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).

如果你无法说服mex前面加上-L/usr/lib/i386-linux-gnu第一,那么我认为你唯一的其他选择是删除/usr/local/MATLAB/R2012a/sys/os/glnx86/libstdc++.so(只是将其重命名为如libstdc++.so.bak)。

回答by th1rdey3

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

我尝试了两个答案..但没有一个对我有用。

however this worked for me. in matlab run this -

但是这对我有用。在 matlab 中运行这个 -

setenv('LD_LIBRARY_PATH', '');

just for anyone who is facing the same problem.

仅适用于面临相同问题的任何人。

P.S: I found this solution here

PS:我在这里找到了这个解决方案

回答by user1533059

You can modify ~/.matlab/R2012a/mexopts.shthat generated after doing mex -setupby adding a line in glnx86 section:

您可以通过在 glnx86 部分中添加一行来修改~/.matlab/R2012a/mexopts.sh执行后生成的mex -setup内容:

LD_LIBRARY_PATH='/usr/lib:$LD_LIBRARY_PATH'

or in glnx64:

或在 glnx64 中:

LD_LIBRARY_PATH='/usr/lib64:$LD_LIBRARY_PATH'

回答by Alejo NH

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:

我找不到我的 libstdc++.so.6 在哪里,所以我无法完全测试 geek_girl 给出的解决方案。但是,对 th1rdey3 的修改有效。我在matlab控制台中运行:

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.

这是我在 Eclipse 中编译 C++ 代码时用于 'LD_LIBRARY_PATH' 的值(我没有使用 mex 文件,而是在 Eclipse 中创建了我的 C++ 代码的可执行文件,然后从 matlab shell 运行它)。在我的例子中,'LD_LIBRARY_PATH' 的值这么长是因为我的 C++ 代码使用了 boost 的正则表达式、matlab 库(libmat、libmx 等)、GSL 库和犰狳。如果您不使用所有这些库,我想 setenv('LD_LIBRARY_PATH','') 应该就足够了。

回答by lindelof

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

这是一个迟到的答案,但我相信最干净、最受 Mathworks 认可且侵入性最小的解决方案是编辑.matlab7rc.sh脚本。这是matlab在类 UNIX 系统下启动 MATLAB 时脚本使用的脚本。(见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:

将该脚本(在 下{matlabroot}/bin)复制到项目的根目录或主目录。然后告诉 MATLAB 首先在系统目录中搜索 C++ 库,而不是它自己的目录。在我的系统上,我更改了第 191 行:

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

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

(简单地设置LD_LIBRARY_PATH为空字符串不是一个好的解决方案,因为这会阻止您加载其他第三方库。)

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

完成此操作后,您可能会在运行时收到以下消息mex

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

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

这通常意味着g++未安装。在类似 Debian 的系统上,运行:

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.

从这里开始,您可能仍然会收到关于使用超出官方支持的 gcc 版本的恼人警告,但这是无害的,可以忽略。

回答by maurizio

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

在 Matlab R2015b 上,我首先重新链接libstdc++.so.6,然后.matlab7rc.sh按照 @lindelof 的上述说明进行编辑。在我的桌面上,从终端:

locate libstdc++.so.6

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

就我而言,系统库位于/usr/lib64. 然后

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.shin {matlabroot}/bin. Delete in the same directory any mexopts.shfile. Restart Matlab. MEXyour file from scratch (this will build a new mexopts.shfile with the new settings. Run it from Matlab console.

然后.matlab7rc.sh{matlabroot}/bin. 删除同一目录中的任何mexopts.sh文件。重新启动 Matlab。MEX从头开始创建您的文件(这将mexopts.sh使用新设置构建一个新文件。从 Matlab 控制台运行它。

回答by neeraj battan

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

如果您没有root访问权限,那么您可以尝试,
LD_PRELOAD='path/to/libstdc++.so.6.0.21' matlab