2012-03-21 80 views
10

我有一个我编译为动态库的项目集合。这些.dylib中的每一个都依赖于其他各种.dylibs,我希望将其放置在各种其他目录中(即一些位于可执行路径,一些位于加载路径,一些位于固定路径)。如何正确设置运行路径,搜索路径和安装名称?

当我在编译的库上运行otool -L时,我得到了这些依赖关系的路径列表,但我知道如何设置/确定这些路径。它们几乎都是伪随机的。我花了几个小时在Xcode的“构建设置”中尝试更改这些路径(w/@rpath,@executable_path,@loader_path等),但我似乎无法更改任何内容(如通过运行otool -L )。我甚至完全知道在哪里添加这些标志和并不真正了解以下或如何正确地使用它们之间的区别:

链接 - “动态库安装名称”
链接 - “运行路径搜索路径“
链接 - ‘其他链接标志’
搜索路径 - ‘库搜索路径’

当我运行的各种库install_name_tool -change,我能够再次成功改变运行路径搜索路径(如通过运行验证otool -L确认)。

我正在运行Xcode 4.2,我非常接近放弃,只是使用运行install_tool_name进行更改的后构建脚本。但它是一个kludge黑客修复,我宁愿不这样做。

我在哪里可以看到如何设置dylib依赖关系的搜索/运行路径?
任何人有任何想法,我可能做错了什么?

回答

10

通常,在我的dylib目标中,我将INSTALL_PATH又名“安装目录”设置为我想要的前缀(例如@executable_path/../Frameworks)。

我将LD_DYLIB_INSTALL_NAME又名为“动态库安装名称”设置为其默认值,即$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)

Xcode根据您的目标名称进行扩展,因此它最终可能会是@executable_path/../Frameworks/MyFramework.framework/Versions/A/MyFramework

要实现的重要一点是安装路径内置于 dylib作为其构建过程的一部分。稍后,当您链接指向A.dylib的B.dylib时,A.dylib的安装路径是复制到 B.dylib。 (这就是otool向你展示的内容 - 那些复制的安装路径。)因此,最好先获得内置到dylib中的正确安装路径。

在试图让所有的dylib一起工作之前,请单独检查每个dylib。构建它,然后在构建的dylib上使用otool -L。每个架构的第一行应该是LD_DYLIB_INSTALL_NAME向您展示的内容。

一旦你有了这样的组织,尝试获取互相链接的dylib。它应该更直接。

+3

虽然不是我一直在寻找确切的答案,帮我找出我的问题。 'otool -L'列出所有的安装名称。列出的第一个“安装名称”是库本身的名称。列出的任何其他路径都是针对其依赖项的。假设你已经从源代码编译了依赖关系,在Xcode中设置“动态库安装名称”将正确设置这些路径。但在我的情况下,依赖来自第三方,所以路径已经设置。由于我无法控制第三方库的编译,我不得不使用'install_name_tool -change'。 – BigMacAttack 2012-03-23 04:19:21

+0

很高兴你知道了。 – 2012-03-23 04:25:11

+2

非常有帮助的答案.. – Ahmed 2013-07-08 22:50:28

1

install_name_tool是用于设置名称和路径是非常有用的。如果程序在构建目录中运行自我测试,那么它特别有用,然后在make install期间发生变化。在这种情况下,您可以使用install_name_tool而不需要单独构建。

install_name_tool也很有用,因为Apple的LD不支持像Linux/GCC那样的rpath链接器选项。也就是说,你需要使用一组不同的命令来设置它们。

这是它的手册页。它的全部内容包括在内,因为它讨论了其他选项,如-headerpad_max_install_names

INSTALL_NAME_TOOL(1)          INSTALL_NAME_TOOL(1) 

NAME 
     install_name_tool - change dynamic shared library install names 

SYNOPSIS 
     install_name_tool [-change old new ] ... [-rpath old new ] ... 
     [-add_rpath new ] ... [-delete_rpath new ] ... [-id name] file 

DESCRIPTION 
     Install_name_tool changes the dynamic shared library install names and 
     or adds, changes or deletes the rpaths recorded in a Mach-O binary. 
     For this tool to work when the install names or rpaths are larger the 
     binary should be built with the ld(1) -headerpad_max_install_names 
     option. 

     -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. 

     -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. 

     -rpath old new 
       Changes the rpath path 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 rpath path name in a 
       specified -rpath it is an error. 

     -add_rpath new 
       Adds the rpath path name new in the specified Mach-O binary. 
       More than one of these options can be specified. If the Mach-O 
       binary already contains the new rpath path name specified in 
       -add_rpath it is an error. 

     -delete_rpath old 
       deletes the rpath path name old in the specified Mach-O binary. 
       More than one of these options can be specified. If the Mach-O 
       binary does not contains the old rpath path name specified in 
       -delete_rpath it is an error. 

SEE ALSO 
     ld(1)