为什么Mac Os X中的Mach-O库需要install_name_tool和otool? [英] Why is install_name_tool and otool necessary for Mach-O libraries in Mac Os X?

查看:189
本文介绍了为什么Mac Os X中的Mach-O库需要install_name_tool和otool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新版本的Xcode 4开发Cocoa应用程序,我想将动态库链接到我的项目(dylibs).

I am developing a Cocoa Application using the latest version of Xcode 4, I want to link dynamic libraries to my project (dylibs).

我在某处读到,在我的项目中添加库还不够,因为我必须运行install_name_toolotool才能使我的项目使用项目中捆绑的库.

I read somewhere that adding the libraries in my project was not enough as I have to run install_name_tool and otool to make my project use the libraries that were bundled in my project.

我已经阅读了install_name_tool的手册页,但是我不明白为什么必须这样做.

I have read the manual pages for install_name_tool, but I do not understand WHY I have to do this.

图书馆如何运作?对应用程序和库的路径指向我的计算机中特定位置的部分特别感兴趣,例如运行otool -L

How do libraries work? Especially interested in the part where the application and the libraries have paths that point to specific places in my machine, like /usr/local/lib/mylibrary.dylib when running otool -L

推荐答案

Apple有几种查找共享库的方法:

Apple has several ways of locating shared libraries:

  1. @executable_path:相对于主要可执行文件
  2. @loader_path:相对于引用二进制文件
  3. @rpath:相对于任何路径列表.
  1. @executable_path : relative to the main executable
  2. @loader_path : relative to the referring binary
  3. @rpath : relative to any of a list of paths.

@rpath是OS X 10.5中引入的最新功能.

@rpath is the most recent addition, introduced in OS X 10.5.

例如,如果您想将可执行文件放在Contents/MacOS中,将库放在Contents/Libraries中,则可以执行以下操作:

If for instance you want to have your executable in Contents/MacOS and libraries in Contents/Libraries you could do the following:

install_name_tool -id @rpath/Libraries/lib_this.dylib   builddir/lib_this.dylib

,并在顶级可执行文件集rpath中设置为:

and in the top-level executable set rpath with:

install_name_tool -add_rpath @loader_path/..  myexecutable

和:

install_name_tool -change builddir/lib_this.dylib @rpath/Libraries/lib_this.dylib myexecutable

注意:-change之后的第一个路径必须与二进制文件中的当前路径完全匹配.

Note: that the first path after -change must match exactly what is currently in the binary.

如果迷路了,otool -l -v myexecutable会告诉您可执行文件中当前到底有哪些加载命令.

If you get lost otool -l -v myexecutable will tell you what load commands exactly are currently in the executable.

有关更多信息,请参见man dyldman install_name_tool.

See man dyld and man install_name_tool for more information.

这篇关于为什么Mac Os X中的Mach-O库需要install_name_tool和otool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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