当我试图在Mac OS X中运行一个可执行文件时,我得到以下错误

dyld: Library not loaded: libboost_atomic.dylib
  Referenced from: /Users/"Directory my executable is in"
  Reason: image not found
Trace/BPT trap:5

我已经安装了boost库,它们位于/opt/local/lib中。我认为这个问题与可执行文件只在它所在的目录中查找有关,因为当我粘贴'libboost_atomic。Dylib '在那里,它不再介意了。不幸的是,它会抱怨找不到下一个增强库。

有没有简单的方法来解决这个问题?


当前回答

我在这里试图运行一个我刚刚用CMake编译的程序。当我试图运行它时,它会抱怨说:

dyld: Library not loaded: libboost_system.dylib
  Referenced from: /Users/path/to/my/executable
  Reason: image not found

我绕过了这个问题,告诉CMake使用静态版本的Boost,而不是让它使用动态版本:

set(Boost_USE_STATIC_LIBS ON)

其他回答

我通过使用产品>清洁构建文件夹(CommandShiftK)修复了这个问题,这使得一个新的清洁构建,真的很奇怪。

这是一个动态链接错误,在加载或运行时链接二进制文件

[@rpath]

Install_name_tool -add_rpath new_path可执行文件 Install_name_tool -delete_rpath old_path可执行文件

找到所有的boost库(其中exefile是你的可执行文件的名称):

$ otool -L exefile
exefile:
        @executable_path/libboost_something.dylib (compatibility version 0.7.0, current version 0.7.0)
        /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 65.1.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

对于每个libboost_xxx。dylib,做:

$ install_name_tool -change @executable_path/libboost_something.dylib /opt/local/lib/libboost_something.dylib exefile

最后再次使用otool进行验证:

$ otool -L exefile
exefile:
        /opt/local/lib/libboost_something.dylib (compatibility version 0.7.0, current version 0.7.0)
        /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 65.1.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

Manpages: otool install_name_tool

编辑前一段时间,我写了一个python脚本(copy_dylib .py),在构建应用程序时自动解决所有这些问题。它会将/usr/local或/opt/local中的所有库打包到应用程序包中,并使用@rpath修复对这些库的引用。这意味着您可以轻松地使用Homebrew安装第三方库并轻松地打包它们。

我现在已经在github上公开了这个脚本。

在我们的例子中,它是一个基于Xcode 11.5构建的iOS应用,使用cocoapods(如果你愿意,也可以是cocoapods-binary)。

我们看到了这样的崩溃:

dyld: Library not loaded: @rpath/PINOperation.framework/PINOperation
  Referenced from: /private/var/containers/Bundle/Application/4C5F5E4C-8B71-4351-A0AB-C20333544569/Tellus.app/Frameworks/PINRemoteImage.framework/PINRemoteImage
  Reason: image not found

原来我必须删除pod缓存并重新运行pod安装,所以Xcode会指出这个差异: