2012-09-20 58 views
53

我想使用install_name_tool来更改可执行文件的路径,但我无法弄清楚rpath是什么。 install_name_tool要求在命令行上给出旧的和新的rpath。我可以使用什么命令在OSX下打印可执行文件的路径?在OSX上打印可执行文件的路径

回答

70

首先,了解可执行文件不包含单个rpath条目,而是包含一个或多个条目的数组。

其次,您可以使用otool列出图像的rpath条目。使用otool -l,你会得到如下的输出,在最后这都是rpath条目:

Load command 34 
      cmd LC_LOAD_DYLIB 
     cmdsize 88 
     name /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (offset 24) 
    time stamp 2 Wed Dec 31 19:00:02 1969 
     current version 1038.32.0 
compatibility version 45.0.0 

Load command 35 
      cmd LC_RPATH 
     cmdsize 40 
     path @loader_path/../Frameworks (offset 12) 

外观为LC_RPATH命令并注意path条目下的路径。

+2

有什么' @ loader_path'? – nn0p

+0

@ nn0p我发现[本文](https://wincent.com/wiki/@executable_path,[email protected][email protected])有助于理解loader_path。 –

+0

我在我的otool中看不到LC_RPATH - l输出,但是我在LC_ID_DYLIB cmd条目后面的名称字段下看到安装名称 –

1

我目前正在写几个猛砸-3脚本处理dyld的,这一次回答了这个问题,所以我将它张贴供参考:

#! /bin/bash 

# ######################################################################### # 

if [ ${#} -eq 0 ] 
then 
    echo " 
Usage: ${0##*/} FILE... 

List rpaths in FILEs 
"  
    exit 0 
fi 

# ######################################################################### # 

shopt -s extglob 

# ######################################################################### # 

for file in "${@}" 
do 
    if [ ! -r "${file}" ] 
    then 
     echo "${file}: no such file" 1>&2 
     continue 
    fi 

    if ! [[ "$(/usr/bin/file "${file}")" =~ ^${file}:\ *Mach-O\ .*$ ]] 
    then 
     echo "${file}: is not an object file" 1>&2 
     continue 
    fi 

    if [ ${#} -gt 1 ] 
    then 
     echo "${file}:" 
    fi 

    IFS_save="${IFS}" 
    IFS=$'\n' 

    _next_path_is_rpath= 

    while read line 
    do 
     case "${line}" in 
      *(\)cmd\ LC_RPATH) 
       _next_path_is_rpath=yes 
       ;; 
      *(\)path\ *\ \(offset\ +([0-9])\)) 
       if [ -z "${_next_path_is_rpath}" ] 
       then 
        continue 
       fi 
       line="${line#* path }" 
       line="${line% (offset *}" 
       if [ ${#} -gt 1 ] 
       then 
        line=$'\t'"${line}" 
       fi 
       echo "${line}" 
       _next_path_is_rpath= 
       ;; 
     esac 
    done < <(/usr/bin/otool -l "${file}") 

    IFS="${IFS_save}" 
done 

# ######################################################################### # 

“希望它可以帮助;-)

NB :有人知道一些可用于此脚本的Bash-3技巧吗?

-1

我只是用otool命令

otool -L <my executable> 

它打印出的rpath的领域。不需要任何长脚本。

+3

这将打印出使用的共享库的路径,但这些路径可能使用@rpath,但是'otool -L'会不是,在我测试的库中包含lib自己的LC_RPATH条目 – Baggers

+0

据我所知,otool -L中的第一个路径是rpath,与otool -D打印的路径相同 –

1

我发现我可以使用otool -D在OSX上打印共享库的安装名称。

而且,我可以不直接引用通过传递-id标志install_name_tool设置安装名称旧安装名称:install_name_tool -id“@ rpath的/我的/路径 MYLIB