使MATLAB Mex在特定文件夹中搜索库 [英] Make MATLAB Mex search for a library in a particular folder

查看:233
本文介绍了使MATLAB Mex在特定文件夹中搜索库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个mex函数,该函数依赖于一个依赖于另一个库的库.当我执行该函数时,它将在运行时输出此错误:

I have created a mex function which relies on a library that relies on another library. When I execute the function, it outputs this error at runtime:

Invalid MEX-file
...
Library not loaded: /usr/local/lib/libgomp.1.dylib

我的计算机上确实有这个库,但是它位于usr/local/gfortran/lib

I do have this library on my computer but it is located in usr/local/gfortran/lib

所以我尝试了这个:

setenv('DYLD_LIBRARY_PATH', '/usr/local/gfortran/lib');

但这根本不能解决问题.我正在使用MacOS Sierra 10.12.6.如何让MATLAB搜索该特定文件夹?

But this doesn't fix the issue at all. I'm using MacOS Sierra 10.12.6. How can I make MATLAB search for that specific folder?

推荐答案

MacOS在搜索动态库(==共享对象)方面与其他OS有所不同.要知道的几件事:

MacOS works differently than other OSes with regard to how it searches for dynamic libraries (== shared objects). A few things to know:

  1. 每个.dylib文件都有一个安装名称".这是嵌入在文件中的字符串,它告诉链接器在哪里可以找到它.将库/可执行文件/MEX文件链接到.dylib时,将存储安装名称",并在运行时使用它来查找库.也就是说,存储的不是文件的当前位置,而是报告文件的位置.

  1. Each .dylib file has an "install name". This is a string embedded in the file that tells the linker where it is to be found. When you link your library/executable/MEX-file to the .dylib, the "install name" is stored and used at run time to locate the library. That is, it's not the current location of the file that is stored, but the location that it reports it should be found at.

安装名称"可以以"@rpath"开头,表示相对路径.

The "install name" can start with "@rpath", this indicates a relative path.

链接到.dylib的可执行文件/库/MEX文件可以指定用于搜索依赖项的备用目录.这等效于Linux下的rpath.这些目录可以是绝对目录,也可以以"@executable_path"或"@loader_path"开头,表示相对路径. "@executable_path"是可执行文件的目录(如果是MEX文件,则为MATLAB二进制文件),而"@loader_path"是试图加载该库(例如MEX文件)的二进制文件的路径. /p>

An executable/library/MEX-file that links to a .dylib can specify alternative directories where to search for dependencies. This is equivalent to the rpath under Linux. These directories can be absolute, or start with "@executable_path" or "@loader_path", indicating a relative path. "@executable_path" is the directory of the executable (the MATLAB binary in case of a MEX-file), and "@loader_path" is the path of the binary that is trying to load the library (e.g. the MEX-file).

以下是有关这些主题的更多信息: https://www.mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html

Here is more information on these topics: https://www.mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html

有些链接器标志可用于设置正确的安装名称和rpath等,但是您也可以使用 install_name_tool 程序以更改这些内容链接.这可能是最简单的情况.

There are linker flags that you can use to set correct install names and rpaths and so forth, but you can also use the install_name_tool program to change these things after linking. This might be easiest in your case.

在您的情况下,您可以使用类似以下的方法来更改MEX文件查找依赖库的位置:

In your case, you can use something like this to change where your MEX-file looks for the dependent library:

install_name_tool -change /usr/local/lib/libgomp.1.dylib usr/local/gfortran/lib/libgomp.1.dylib mexfile.mexmaci64

(用您的MEX文件的名称替换mexfile.mexmaci64).

(replace mexfile.mexmaci64 with the name of your MEX file).

如果要使用相对路径,例如,将依赖项libgomp.1.dylib移至取决于MEX文件位置的路径,则可以执行以下操作:

If you want to use relative paths, for example if you move the dependent libgomp.1.dylib to a path that depends on the location of the MEX file, you'd instead do:

install_name_tool -change /usr/local/lib/libgomp.1.dylib @rpath/libgomp.1.dylib mexfile.mexmaci64
install_name_tool -add_rpath @loader_path/../lib mexfile.mexmaci64

install_name_tool -change /usr/local/lib/libgomp.1.dylib @loader_path/../lib/libgomp.1.dylib mexfile.mexmaci64

这篇关于使MATLAB Mex在特定文件夹中搜索库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆