问题描述
我做调用一些dylib成功,但是,dylib文件和可执行文件在不同的目录中的应用程序(可执行文件)。我添加了目录包含dylib文件到$ PATH环境变量,但是,它仍然不会加载。我所有的dylib文件复制到可执行文件,程序终于运行。这证实了dylib文件没有问题。但是,我怎么能告诉OS找呢?
在Windows中,我只需要添加的目录路径中包含的dll文件到$ PATH。我需要做什么的Mac OS X呢?
非常感谢!
读取贾斯汀提供的链接后,我能够成功使用 @executable_path
标记改变我dylib install_name的以指向我的可执行文件位于同一目录。
I will demonstrate with an example.
Let's say I have the following executable /opt/local/bin/convert and its dylibs are in /opt/local/lib. I want to copy it to another dir and have it load its dylibs from the same directory as where I copied the executable.
> mkdir ~/tmp/bin
> cp /opt/local/bin/convert ~/tmp/bin
Get a list of the executables dylibs
> otool -L ~/tmp/bin/convert
~/tmp/bin/convert:
/opt/local/lib/libtiff.3.dylib (compatibility version 13.0.0, current version 13.5.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
/opt/local/lib/libjpeg.8.dylib (compatibility version 12.0.0, current version 12.0.0)
/opt/local/lib/libfontconfig.1.dylib (compatibility version 6.0.0, current version 6.4.0)
/opt/local/lib/libiconv.2.dylib (compatibility version 8.0.0, current version 8.1.0)
/opt/local/lib/libfreetype.6.dylib (compatibility version 15.0.0, current version 15.0.0)
/opt/local/lib/libexpat.1.dylib (compatibility version 7.0.0, current version 7.2.0)
/opt/local/lib/libbz2.1.0.dylib (compatibility version 1.0.0, current version 1.0.6)
/opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.6)
...
I only care about the dylibs in the /opt/local/lib dir, so we pull out only dylibs in /opt. I want to keep all other dylib references intact especially to /usr/lib/libSystem stuff.
> DYLIBS=`otool -L ~/tmp/bin/convert | grep "/opt" | awk -F' ' '{ print $1 }'`
Copy all the dylibs that the executable references to the same dir where the executable has been copied to.
> for dylib in $DYLIBS; do cp $dylib ~/tmp/bin/; done;
Use the install_name_tool
to change the install name of all the dylibs we pulled out in the step above, and replace them by prepending the @executable_path
to the dylib name. This will make the dynamic linker look for the dylib in the same directory as where the executable is located.
> for dylib in $DYLIBS; do install_name_tool -change $dylib @executable_path/`basename $dylib` ~/tmp/bin/convert; done;
Confirm that the install names have been changed and that libSystem is still pointing to /usr/lib/libSystem.
> otool -L ~/tmp/bin/convert
~/tmp/bin/convert:
@executable_path/libtiff.3.dylib (compatibility version 13.0.0, current version 13.5.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
@executable_path/libjpeg.8.dylib (compatibility version 12.0.0, current version 12.0.0)
@executable_path/libfontconfig.1.dylib (compatibility version 6.0.0, current version 6.4.0)
@executable_path/libiconv.2.dylib (compatibility version 8.0.0, current version 8.1.0)
@executable_path/libfreetype.6.dylib (compatibility version 15.0.0, current version 15.0.0)
@executable_path/libexpat.1.dylib (compatibility version 7.0.0, current version 7.2.0)
@executable_path/libbz2.1.0.dylib (compatibility version 1.0.0, current version 1.0.6)
@executable_path/libz.1.dylib (compatibility version 1.0.0, current version 1.2.6)
...
这篇关于如何在Mac OS X(C ++)使用dylib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!