MAC:@rpath的坑

Problem

这篇文章的缘由是我在尝试使用ns3带的NetAnim程序时,显示了下面这个错误:

dyld: Library not loaded: @rpath/QtGui.framework/Versions/4/QtGui
  Referenced from: /path/to/ns-allinone-3.28/netanim-3.108/./NetAnim
  Reason: image not found
[1]    86663 abort      ./NetAnim

这是一个动态链接的错误,所以没法通过编译的时候添加LDFLAGS来解决。不过错误里面的@rpath这个东西倒是挺有意思,显然并不是环境变量。我在网上查了很多,但是大多数是围绕xcode讨论的,不太适用于我面临的场景(命令行)。不过这些文章(如这篇)能够大致阐明@rpath的用途。简而言之,@rpath是一个类似Shell中的PATH的变量,程序在执行时会从@rpath指定的路径中寻找动态链接库文件。那么剩下的问题就是我们如何操作这个变量了。

Solution

通过otool我们可以查看一个程序的动态链接文件搜索地址,例如我要用的NetAnim

$ otool -L ./NetAnim
./NetAnim:
    @rpath/QtGui.framework/Versions/4/QtGui (compatibility version 4.8.0, current version 4.8.7)
    @rpath/QtCore.framework/Versions/4/QtCore (compatibility version 4.8.0, current version 4.8.7)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)

而我们可以通过install_name_tool来对这些地址进行操作。

$ install_name_tool -h
Usage: /Library/Developer/CommandLineTools/usr/bin/install_name_tool [-change old new] ... [-rpath old new] ... [-add_rpath new] ... [-delete_rpath old] ... [-id name] input

对我而言,我需要将Qt4的动态链接库添加到NetAdmin的搜索路径中去,可以使用如下的命令:

install_name_tool -add_rpath /usr/local/Cellar/qt@4/4.8.7_3/lib ./NetAnim

大功告成。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容