c++ - MAC 操作系统 : dynamically linked library not found despite correct rpath

标签 c++ macos cmake catkin

我有一个(相对)大的 C++ 项目,它在 ubuntu 上编译和运行良好(使用 cmake/catkin)。 它在 mac os 上编译得很好,但是当尝试启动可执行文件时,我收到错误消息:

dyld: Library not loaded: <name of library>.dylib
  Referenced from:
  <path to executable>/<executable>
Reason: image not found

运行命令时:

otool -l <executable> | grep LC_RPATH -A2

我得到输出:

      cmd LC_RPATH
  cmdsize 64
     path <correct absolute path to folder containing library> (offset 12)

      cmd LC_RPATH
  cmdsize 24
     path /sw/lib (offset 12)

      cmd LC_RPATH
  cmdsize 32
     path /usr/X11/lib (offset 12)

      cmd LC_RPATH
  cmdsize 32
     path /opt/local/lib (offset 12)

      cmd LC_RPATH
  cmdsize 32
     path /opt/X11/lib (offset 12)

我很不清楚为什么找不到图书馆。 运行:

otool -L <executable>

打印:

<executable name>:
    <library name>.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /opt/X11/lib/libglut.3.dylib (compatibility version 13.0.0, current version 13.0.0)
    /opt/X11/lib/libGL.1.dylib (compatibility version 1.2.0, current version 1.2.0)
    /opt/X11/lib/libGLU.1.dylib (compatibility version 1.3.0, current version 1.3.0)
    /opt/X11/lib/libX11.6.dylib (compatibility version 10.0.0, current version 10.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)

这似乎证实它没有获得正确的路径。

有什么我想念的吗?

附言:

不确定是否相关,这里是我使用的 cmake 命令: (来自here)

# use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH  FALSE)

# when building, don't use the install RPATH already
# (but later on when installing)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/${MACHTYPE}")

# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# the RPATH to be used when installing, but only if it's not a system directory
LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib/${MACHTYPE}" isSystemDir)
IF("${isSystemDir}" STREQUAL "-1")
    SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/${MACHTYPE}")
ENDIF("${isSystemDir}" STREQUAL "-1")

最佳答案

答案来自 here 的评论

我需要使用@rpath

Actually Mac OS X does have the concept of rpath. Previous to 10.4 there was only @executable_path which pointed to the location of the executable binary (as opposed to any dylibs that it loaded). @loader_path was introduced in 10.4 which allowed dylibs to load dependencies based on their location (and not on the location of the executable - who knows what's using the library).

With 10.5 apple introduced @rpath which is baked into the loading binary - executable or library. So instead of seeing: @loader_path/../../Library/Frameworks/blah.dylib you might see @rpath/Library/Frameworks/blah.dylib

Which means if the rpath is set to . and / it would look at ./Library/Frameworks/blah.dylib and then /Library/Frameworks/blah.dylib for any libraries.

关于c++ - MAC 操作系统 : dynamically linked library not found despite correct rpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32460262/

相关文章:

c++ - Cmake 和 QT5 - Include 只接受一个参数

c++ - 读取访问冲突_First 是 nullPtr

linux - bash pid 和 $$ 之间的区别

html - 如何在 Safari 的地址字段中禁用自动完成功能?

c++ - 即使使用 -lstdc++fs 也无法链接 std::filesystem

c++ - 为什么不总是构建带有调试信息的版本?

C++:使用迭代器替换部分字符串不起作用

Qt : has initializer but incomplete type 中的 C++ 错误

c++ - 2D 射弹 - 速度和位置 vector

macos - 检查签名工具失败并显示消息 : resource envelope is obsolete (custom omit rules)