在进行这个实验中,通过c++调用matlab中自己编写的函数(是通过将该函数在matlab中mcc编译成.so,然后c++中调用这个.so的方式),成功编译后(编译时也出现一些错误,详情点这里),但是成功编译完后,居然不能运行。为什么编译通过不报任何错误,但是运行不了呢?原来是因为matlab的动态链接库运行时需要安装MCRInstaller(它包含matlab所有的动态链接库文件),所以本文需要安装一下MCRInstaller。我的matlab版本为2012a。安装步骤:
1)在matlab命令行窗口输入mcrinstaller,可以看到很详细的安装提示,什么版本的,下载路径都告诉你了:
>> mcrinstaller
The GLNXA64 MCR Installer, version 7.17, is:
/opt/MATLAB/R2012a/toolbox/compiler/deploy/glnxa64/MCRInstaller.zip
MCR installers for other platforms are located in:
/opt/MATLAB/R2012a/toolbox/compiler/deploy/<ARCH>
<ARCH> is the value of COMPUTER('arch') on the target machine.
Full list of available MCR installers:
/opt/MATLAB/R2012a/toolbox/compiler/deploy/glnxa64/MCRInstaller.zip
For more information, read your local MCR Installer help.
Or see the online documentation at MathWorks' web site. (Page may load slowly.)
ans =
/opt/MATLAB/R2012a/toolbox/compiler/deploy/glnxa64/MCRInstaller.zip
2)到它提示的路径(/opt/MATLAB/R2012a/toolbox/compiler/deploy/glnxa64/MCRInstaller.zip),下载安装包。
3)准备好一个可写入的安装路径
$ cd /opt/MATLAB/
$ ls
R2012a
$ sudo mkdir MATLAB_Compiler_Runtime
$ sudo chmod 777 MATLAB_Compiler_Runtime
$ ls
MATLAB_Compiler_Runtime R2012a
4)安装
$ unzip MCRInstaller.zip
$ ./install
进入图形化安装界面,选好安装路径:/opt/MATLAB/MATLAB_Compiler_Runtime,默认安装就行,部分截图如下:
安装好了后,很人性化的提醒你:On the target computer, append the following to your LD_LIBRARY_PATH environment variable: /opt/MATLAB/MATLAB_Compiler_Runtime/v717/runtime/glnxa64:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/bin/glnxa64:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/sys/os/glnxa64:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64/server:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64 Next, set the XAPPLRESDIR environment variable to the following value: /opt/MATLAB/MATLAB_Compiler_Runtime/v717/X11/app-defaults
照做就行:
$ sudo gedit /etc/profile
添加到这里:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MATLAB/bin/glnxa64:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/runtime/glnxa64:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/bin/glnxa64:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/sys/os/glnxa64:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64/server:/opt/MATLAB/MATLAB_Compiler_Runtime/v717/sys/java/jre/glnxa64/jre/lib/amd64
$ source /etc/profile
装完之后,再运行这里的最后一个错误就消失了,matlab动态链接看可以成功调用了!

本文详细介绍了如何在Linux 64位环境下,针对MATLAB 2012a版本,安装MCR Installer以解决运行时无法找到动态链接库的问题。通过MATLAB命令行获取MCRInstaller路径,下载并安装,最后设置LD_LIBRARY_PATH和XAPPLRESDIR环境变量,确保正确运行编译后的MATLAB函数。
8438

被折叠的 条评论
为什么被折叠?



