macos 使用完整路径链接到 Mac 上的动态库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1937232/
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
Linking to a dynamic library on a Mac with full path
提问by dzhelil
I am linking a (Python extension) library that embeds the Matlab engine with the following command (generated using cmake)
我正在使用以下命令(使用 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
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?
我相信问题源于这样一个事实,即链接器在表单中包含 matlab dylib 文件@loader_path/libeng.dylib
而不是使用完整路径,即使我提供了g++
. 如何强制链接器使用完整路径?
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.
这是那些库文件所在的位置,但我想避免这种情况,因为它会导致其他一些问题。
回答by dzhelil
Manually changing the files using install_name_tool
使用手动更改文件 install_name_tool
install_name_tool -change "@loader_path/libeng.dylib" "/Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib" library.so
install_name_tool -change "@loader_path/libmx.dylib" "/Applications/MATLAB_R2009b.app/bin/maci64/libmx.dylib" library.so
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.
我可以将其用作临时修复,但我想知道是否没有更好的解决方案,其中为链接器提供了使用完整路径的设置。
回答by quazgar
Note that some of the problems with DYLD_LIBRARY_PATH
can be prevented by using DYLD_FALLBACK_LIBRARY_PATH
instead. This will only be used if the lib cannot be found in the default paths.
请注意,DYLD_LIBRARY_PATH
可以通过使用DYLD_FALLBACK_LIBRARY_PATH
代替来防止一些问题。仅当在默认路径中找不到 lib 时才会使用此选项。
回答by bmargulies
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.
查看 ld 命令的 -rpath 选项来控制它。您可能还对https://github.com/bimargulies/jni-origin-testbed的内容感兴趣,它是一些相关技术的演示。
The critical technique here is:
这里的关键技术是:
install_name_tool -change libsl2.so "@loader_path/libsl2.so" libsl1.so
回答by JM Marino
You can also use symbolic link !
您也可以使用符号链接!