gpt4 book ai didi

xcode - install_name_tool -change 和 -id 之间的区别

转载 作者:行者123 更新时间:2023-12-03 16:03:57 25 4
gpt4 key购买 nike

我一直在为这个概念苦苦挣扎,我无法真正理解 -change 之间的区别。和 -id .手册页状态

 -id name
Changes the shared library identification name of a dynamic shared library to name. If the Mach-O binary is not a dynamic
shared library and the -id option is specified it is ignored.

-change old new
Changes the dependent shared library install name old to new in the specified Mach-O binary. More than one of these options
can be specified. If the Mach-O binary does not contain the old install name in a specified -change option the option is
ignored.

到目前为止,我已经尝试过 -change .假设我有以下结构
Test.App
|_Contents
|_MacOS
| |_test -----> item A
|_Library
|_test_library.dylib --->item B
|_another_library.dylib --->item C

现在假设我在 itemB 上运行了以下内容
$ otool -L test_library.dylib
test_library.dylib
/some/path/another_library.dylib -->item D

以上结果表明 test_library.dylib取决于 another_library.dylib现在如果我需要更改 another_library.dylib 的位置我会这样做
install_name_tool -change /some/path/another_library.dylib some/new/path/another_library.dylib  test_library.dylib 

这将改变项目 D 的位置。我的问题是 install-name_tool -id做,我什么时候使用它?

最佳答案

安装名称
术语安装名称指的是 .dylib 的确切路径。最终用户系统中的文件,以便运行时链接器可以找到并加载动态库。
名称可以是:

  • 绝对的,系统库就是这种情况。这些在最终用户和开发人员的系统上都位于相同的位置。
  • 相对的,这是与应用程序捆绑在一起的库的情况。在最终用户的系统上 .dylib将嵌入到应用程序包和开发者系统中,它们将预先内置在 /usr/local 中, /opt/local或其他地方,或者它们将作为应用程序构建的一部分从源代码构建。

  • 后者是 .dylib 时的主要问题。已构建,其安装名称被标记为 .dylib由链接器,这就是在运行时可以找到并加载它的地方。显然这在最终用户系统上不起作用,因为该路径只存在于开发人员的系统上,所以解决方案是使用 install_name_tool在将应用程序包放在一起时,修改库的安装名称以及引用这些库的可执行文件。
    占位符
    由于可执行文件/应用程序包可以安装在最终用户系统的不同位置,您可以使用占位符系统来抽象安装名称位置:
  • @executable_path :主可执行文件的完整路径。
  • @loader_path : 引用可执行文件的完整路径或 .dylib .
  • @rpath :主可执行文件中设置的 RPATH。这也可以使用 install_name_tool 更改。 .

  • 因此,例如在 macOS 应用程序包中,可执行文件将位于 TheApp.app/Contents/MacOS/TheApp 中。和图书馆将在 TheApp.app/Contents/Frameworks所以你会想要使用路径 @executable_path/../Frameworks/Library.dylib 来引用库.
    最好将主可执行文件的 RPATH 设置为 @executable_path/../Frameworks但是,请使用 @rpath/Library.dylib 引用库.
    install_name_tool install_name_tool有两个主要选项: -id : 这将设置 .dylib 的安装名称文件本身,并且当某些内容与 .dylib 链接时,将用作原型(prototype)安装名称。 .您可以在构建 .dylib 后立即“更正”安装名称。 ,但是这是一个不寻常的工作流程,因为图书馆如何知道使用它的环境? -change : 这会更改 .dylib 的安装名称在引用可执行文件(或 dylib)中。
    -id名称与 -change 不匹配姓名?没有。 -change一旦运行时链接器找到 .dylib,选项是正确的重要选项。那么任务就完成了。
    xcodedevtools
    您显然会使用脚本编写所有修复程序,但这有点乏味,所以我开发了 copy_dylibs.py 脚本为您完成这一切。您将其配置为在链接您的应用程序可执行文件后运行,它会查看您的可执行文件以递归查找 .dylib要复制到应用程序包中的文件。然后修复它们的安装名称,保留原来的 .dylib单独的文件。

    关于xcode - install_name_tool -change 和 -id 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35220111/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com