使用完整路径链接到 Mac 上的动态库
我正在使用以下命令(使用 cmake 生成)链接嵌入 Matlab 引擎的(Python 扩展)库,
c++ -mmacosx-version-min=10.6 -bundle -headerpad_max_install_names -o library.so library.o /Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib /Applications/MATLAB_R2009b.app/bin/maci64/libmx.dylib -framework Python
导致
$ otool -L library.so
library.so:
@loader_path/libeng.dylib (compatibility version 0.0.0, current version 0.0.0)
@loader_path/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
/System/Library/Frameworks/Python.framework/Versions/2.6/Python (compatibility version 2.6.0, current version 2.6.1)
/opt/local/lib/gcc44/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.13.0)
/opt/local/lib/gcc44/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.0)
然而,当我尝试使用该库时,我收到一条错误消息:
ImportError: dlopen(./library.so, 2): Library not loaded: @loader_path/libmex.dylib
Referenced from: ./library.so
Reason: image not found
我相信问题源于以下事实:链接器包含 @loader_path/libeng.dylib
形式的 matlab dylib 文件,而不是使用完整路径,即使我提供了 g++
的完整路径。如何强制链接器使用完整路径?
我知道一种解决方案是使用
export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2009b.app/bin/maci64:$DYLD_LIBRARY_PATH
这些库文件所在的位置,但我想避免这种情况,因为它会导致一些其他问题。
I am linking a (Python extension) library that embeds the Matlab engine with the following command (generated using cmake)
c++ -mmacosx-version-min=10.6 -bundle -headerpad_max_install_names -o library.so library.o /Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib /Applications/MATLAB_R2009b.app/bin/maci64/libmx.dylib -framework Python
resulting in
$ otool -L library.so
library.so:
@loader_path/libeng.dylib (compatibility version 0.0.0, current version 0.0.0)
@loader_path/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
/System/Library/Frameworks/Python.framework/Versions/2.6/Python (compatibility version 2.6.0, current version 2.6.1)
/opt/local/lib/gcc44/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.13.0)
/opt/local/lib/gcc44/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.0)
However, when I try to use the library, I get an error message:
ImportError: dlopen(./library.so, 2): Library not loaded: @loader_path/libmex.dylib
Referenced from: ./library.so
Reason: image not found
I believe the problem stems from the fact that the linker includes the matlab dylib files in the form @loader_path/libeng.dylib
rather than using the full path, even though I give the full path to g++
. How can I force the linker to use the full path?
I know one solution is to use
export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2009b.app/bin/maci64:$DYLD_LIBRARY_PATH
which is where those library files reside, but I'd like to avoid that as it causes some other problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
install_name_tool
手动更改文件我可以使用它作为临时修复,但我想知道是否没有更好的解决方案,为链接器提供使用完整路径的设置。
Manually changing the files using
install_name_tool
I could use this as a temporary fix, but I wonder if there isn't a better solution where the linker is given a setting to use the full paths.
请注意,可以通过使用
DYLD_FALLBACK_LIBRARY_PATH
来避免DYLD_LIBRARY_PATH
的一些问题。仅当在默认路径中找不到该库时才会使用此选项。Note that some of the problems with
DYLD_LIBRARY_PATH
can be prevented by usingDYLD_FALLBACK_LIBRARY_PATH
instead. This will only be used if the lib cannot be found in the default paths.查看 ld 命令的 -rpath 选项来控制它。您可能还对 https://github.com/bimargulies/jni-origin 的内容感兴趣-testbed,这是一些相关技术的演示。
这里的关键技术是:
Look into the -rpath option to the ld command to control this. You might also be interested in the contents of https://github.com/bimargulies/jni-origin-testbed, which is a demonstration of some relevant technology.
The critical technique here is:
您还可以使用符号链接!
You can also use symbolic link !