install_name_tool 更新可执行文件以在 Mac OS X 中搜索 dylib [英] install_name_tool to update a executable to search for dylib in Mac OS X

查看:51
本文介绍了install_name_tool 更新可执行文件以在 Mac OS X 中搜索 dylib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安装在 /PATH/lib 中的动态库 libtest.dylib,以及一个执行二进制文件 myapp,它使用安装在 /PATH/bin.

I have a dynamic libray libtest.dylib that is installed in /PATH/lib, and an execution binary, myapp, that uses the dylib installed in /PATH/bin.

我可以运行 myapp 来查找 dylib,如下所示 (可以在 Mac OS X 上使用 DYLD_LIBRARY_PATH 吗?还有,它的动态库搜索算法是什么?):

I can run myapp to find the dylib as follows (Is it OK to use DYLD_LIBRARY_PATH on Mac OS X? And, what's the dynamic library search algorithm with it?):

DYLD_LIBRARY_PATH="/PATH/lib" myapp 

我想我可以使用 install_name_tool 来更新库和可执行文件,以便可以使用 rpath 找到该库.我使用了这篇文章中的提示 - 如何指定 rpath在动态库中?.

I think I can use install_name_tool to update the library and executable so that the library can be found with rpath. I used the hints in this post - How can I specify the rpath in a dylib?.

在lib中,我执行了这个命令来添加rpath.

In lib, I executed this command to add rpath.

install_name_tool -id "@rpath/libtest.dylib" libtest.dylib
install_name_tool -add_rpath "@executable_path/../lib/" libtest.dylib

在 bin 中,我执行了 install_name_tool -add_rpath "@executable_path/../lib/" myapp.

In bin, I executed install_name_tool -add_rpath "@executable_path/../lib/" myapp.

但是,当我在 bin 目录中执行 myapp 时,出现错误消息.

However, when I executed myapp in bin directory, I have the error messages.

dyld: Library not loaded: libtest.dylib
  Referenced from: /PATH/bin/./myapp
  Reason: image not found
Trace/BPT trap: 5

otool -l myapp 显示 rpath 在 myapp 中正确更新.

otool -l myapp shows the rpath is correctly updated in myapp.

Load command 16
          cmd LC_RPATH
      cmdsize 40
         path @executable_path/../lib/ (offset 12)

libtest.dylib 也是如此

The same is true with libtest.dylib

Load command 13
          cmd LC_RPATH
      cmdsize 40
         path @executable_path/../lib/ (offset 12)

可能有什么问题?

当然,我可以在编译和链接时使用 cc -install_name,但我想知道如何做同样的事情我修改了生成的 dylib 和执行二进制文件.

Of course, I can use cc -install_name when compile and link time, but I wanted to know how to do the same thing my modifying the generatd dylib and execution binary.

来自库:

cc -install_name "@loader_path/../lib/libtest.dylib" -dynamiclib -o libtest.dylib test.c

或者,install_name 可以使用@rpath:

Or, the install_name can use @rpath:

cc -install_name "@rpath/libtest.dylib" -dynamiclib -o libtest.dylib test.c

来自垃圾箱:

cc -I../lib -c main.c
cc -o main main.o ../lib/libtest.dylib -Wl,-rpath -Wl,@loader_path/../lib

或者只有一行:

cc -I../lib -L../lib -o main main.c -ltest -Wl,-rpath -Wl,@loader_path/../lib

推荐答案

otool -l 中,我分析了原始库和二进制文件中应该添加或修改的内容.

From otool -l, I analyzed what should be added or modified from the original library and binary.

更改在 id 中:

Load command 2 <-- OLD
          cmd LC_ID_DYLIB
      cmdsize 40
         name libtest.dylib (offset 24)
   time stamp 1 Wed Dec 31 18:00:01 1969

Load command 2 <-- NEW
          cmd LC_ID_DYLIB
      cmdsize 64
         name @loader_path/../lib/libtest.dylib (offset 24)

这是完成更改的命令:

install_name_tool -id "@loader_path/../lib/libtest.dylib" libtest.dylib 

或者使用rpath:

install_name_tool -id "@rpath/libtest.dylib" libtest.dylib

可执行文件

有两个变化:rpath 和 load_dylib

The executable

There are two changes: rpath and load_dylib

Load command 12 <-- OLD
          cmd LC_LOAD_DYLIB
      cmdsize 40
         name libtest.dylib (offset 24)

Load command 12 <-- NEW
          cmd LC_LOAD_DYLIB
      cmdsize 64
         name @loader_path/../lib/libtest.dylib (offset 24)

这是完成更改的命令

install_name_tool -change libtest.dylib @loader_path/../lib/libtest.dylib myapp 

我还需要添加 rpath

Also I needed to add the rpath

Load command 14
          cmd LC_RPATH
      cmdsize 32
         path @loader_path/../lib (offset 12)

这是完成添加的命令:

 install_name_tool -add_rpath "@loader_path/../lib" myapp

想法

二进制文件试图找到库,它知道它从 install_name_tool -add_rpath "@loader_path/../lib" myapp 所在的位置.它加载库,库的 id 是 @rpath/libtest.dylib,其中 @rpath 设置为 @loader_path/../lib在可执行二进制文件中进行匹配.

The idea

The binary tries to find the library, it knows where it is located from install_name_tool -add_rpath "@loader_path/../lib" myapp. It loads the library, and the library's id is @rpath/libtest.dylib where @rpath is set to @loader_path/../lib in the executable binary to make the match.

使用 CMake 时,我们可以通过在 CMakeLists.txt 文件中添加以下内容来自动化该过程.

When using CMake, we can automatize the process with the following addition in CMakeLists.txt file.

应该添加id.

# https://cmake.org/pipermail/cmake/2006-October/011530.html
SET_TARGET_PROPERTIES (test
  PROPERTIES BUILD_WITH_INSTALL_RPATH 1
             INSTALL_NAME_DIR "@rpath"
  )

可执行

应该指定rpath:

Executable

The rpath should be specified:

SET(CMAKE_INSTALL_RPATH "@loader_path/../lib/libtest.dylib")

这篇关于install_name_tool 更新可执行文件以在 Mac OS X 中搜索 dylib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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