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

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

问题描述

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

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,如下所示(

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在dylib中?.

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

在垃圾箱中,我执行了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)

这是完成添加操作的命令:

This is the command to accomplish the addition:

 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天全站免登陆