当我试图在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 '在那里,它不再介意了。不幸的是,它会抱怨找不到下一个增强库。

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


当前回答

如https://gist.github.com/berkedel/d1fc6d13651c16002f64653096d1fded中所说,你可以试试

brew uninstall --ignore-dependencies node icu4c
brew install node
brew link --overwrite node

其他回答

可以使用sudo install_name_tool -change change dylib路径 和 Sudo install_name_tool -id change dylib名称

要解决我的Macbook Catalina 10.15.4上的以下错误:

dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
    Referenced from: /usr/local/bin/mongoexport
    Reason: image not found
Abort trap: 6

我运行下面的命令,解决了上面的问题:

brew switch openssl 1.0.2s

我忘记添加use_frameworks了!在我的播客里。我把它加回来,它起作用了。

找到所有的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上公开了这个脚本。

如果使用cmake,在目标属性中添加DYLIB_INSTALL_NAME_BASE "@rpath":

set_target_properties(target_dyLib PROPERTIES
        # # for FRAMEWORK begin
        # FRAMEWORK TRUE
        # FRAMEWORK_VERSION C
        # MACOSX_FRAMEWORK_IDENTIFIER com.cmake.targetname
        # MACOSX_FRAMEWORK_INFO_PLIST ./Info.plist
        # PUBLIC_HEADER targetname.h
        # # for FRAMEWORK end
        IPHONEOS_DEPLOYMENT_TARGET "8.0"
        DYLIB_INSTALL_NAME_BASE "@rpath" # this is the key point
        XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer"
        DEVELOPMENT_TEAM "${DEVELOPMENT_TEAM}"
    )

或者在Xcode动态库项目Target -> Build Setting中将动态库安装名库设置为@rpath