简体   繁体   English

为什么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?

I am developing a Cocoa Application using the latest version of Xcode 4, I want to link dynamic libraries to my project ( dylibs ). 我正在使用最新版本的Xcode 4开发一个Cocoa应用程序,我想将动态库链接到我的项目( dylibs )。

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_toolotool才能使我的项目使用我项目中捆绑的库。

I have read the manual pages for install_name_tool , but I do not understand WHY I have to do this. 我已经阅读了install_name_tool的手册页,但我不明白为什么我必须这样做。

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 特别感兴趣的是应用程序和库具有指向我机器中特定位置的路径的部分,例如运行otool -L时的/usr/local/lib/mylibrary.dylib

Apple has several ways of locating shared libraries: Apple有多种方法可以找到共享库:

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

@rpath is the most recent addition, introduced in OS X 10.5. @rpath是OS X 10.5中引入的最新版本。

If for instance you want to have your executable in Contents/MacOS and libraries in Contents/Libraries you could do the following: 例如如果你想在你的可执行Contents/MacOS ,并在图书馆Contents/Libraries ,你可以做到以下几点:

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

and in the top-level executable set rpath with: 并在顶级可执行文件集rpath中:

install_name_tool -add_rpath @loader_path/..  myexecutable

and: 和:

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

Note: that the first path after -change must match exactly what is currently in the binary. 注意: -change后的第一个路径必须与二进制文件中当前的路径完全匹配。

If you get lost otool -l -v myexecutable will tell you what load commands exactly are currently in the executable. 如果你迷路了otool -l -v myexecutable会告诉你当前在可执行文件中的加载命令。

See man dyld and man install_name_tool for more information. 有关更多信息,请参阅man dyldman install_name_tool

还有一个名为MacDependency的GUI工具,它将公开所有依赖库( https://github.com/kwin/macdependency/ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM