Unable to get working RPATH (even absolute) on macOS

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Unable to get working RPATH (even absolute) on macOS

Nicholas Devenish
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
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake