[CMake] Unable to get working RPATH (even absolute) on macOS

Nicholas Devenish ndevenish at gmail.com
Tue Nov 6 07:39:26 EST 2018


Hi All,

Like many, I've been having trouble understanding the RPATH control settings.

In particular, I can't seem to get external libraries (e.g. boost) to link in
*any* way that allows it to find dependent libraries without DYLD_LIBRARY_PATH.
Eventually I'd like to refer to them with relative paths (build or install) but
want to just get it working first. CMakeLists to reproduce, on macOS 10.13.4,
cmake 3.12.4:

    cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

    find_package(Boost COMPONENTS thread REQUIRED)
    file(WRITE test.cpp "int main(){return 0;}")

    add_executable(main test.cpp)
    target_link_libraries(main Boost::thread )

    install(TARGETS main RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})

Building works fine but it can't find the library, from build:

  $ cmake .. -DBOOST_ROOT=/full/path/to/boost -DCMAKE_INSTALL_PREFIX=../install
  $ make
  $ ./main
  dyld: Library not loaded: libboost_thread.dylib
    Referenced from: /Users/nickd/test_cmr/_b/./main
    Reason: image not found
  Abort trap: 6

or from install:

  $ make install
  $ ../install/main
  dyld: Library not loaded: libboost_thread.dylib
    Referenced from: /Users/nickd/test_cmr/_b/../install/main
    Reason: image not found
  Abort trap: 6

Using `otool -l main` also shows just the library name stored in the
Load command,
and no RPATH entries (and no output from `otool -l main | grep LC_RPATH -A2`)

Trying to follow
https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling,
it seems to imply that by default "full" RPaths will be used, and that with the
MACOSX_RPATH flag set this should be the same for mac (but this policy
is now the default - and I checked that cmake_policy(GET CMP00042 var)
== NEW, and also setting manually). I tried the "Always full RPATH"
code block and it didn't seem to change anything. In fact the exact
same example above seems to work on linux, with the executable running
and:

  $ readelf -d main | grep rpath
   0x000000000000000f (RPATH)              Library rpath:
[/full/path/to/boost/lib:]

I'm also aware that macOS RPATH handling was supposedly somewhat ah,
reduced when SIP was brought in, but my understanding was that only
applied to libraries in the system-bundled folders.

Any ideas or guidance? What am I doing wrong?

Thanks,

Nick Devenish


More information about the CMake mailing list