For simplicity, here's an example of what I'm trying to achieve:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(example)
add_library(${PROJECT_NAME} SHARED)
target_link_libraries(${PROJECT_NAME} PUBLIC objc)
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)
After building it, I run the following command:
otool -L libexample.dylib
which outputs:
libexample.dylib:
@rpath/libexample.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
Is there anything that could be done through CMake to cause the linking to the objc library to use rpath instead of the full path? @rpath/libobjc.A.dylib
instead of /usr/lib/libobjc.A.dylib
Edit:
I've tried the solution for this question, but it doesn't seem to fix my issue. The output of the otool
command remains the same.
Edit 2:
The following example still links to the full path and not the rpath:
cmake_minimum_required(VERSION 3.9)
project(example)
set(CMAKE_MACOSX_RPATH TRUE)
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_INSTALL_NAME_DIR "@rpath/")
add_library(${PROJECT_NAME} SHARED)
set_target_properties(${PROJECT_NAME} PROPERTIES
LINKER_LANGUAGE CXX
LINK_FLAGS "-Wl,-rpath,./"
MACOSX_RPATH TRUE
SKIP_BUILD_RPATH FALSE
BUILD_WITH_INSTALL_RPATH TRUE
INSTALL_RPATH_USE_LINK_PATH TRUE
INSTALL_NAME_DIR "@rpath/")
target_link_libraries(${PROJECT_NAME} PUBLIC objc)
Edit 3:
With further investigation, I've come to the conclusion that this wouldn't be possible without using install_name_tool -change
. From this issue, it appears that for any external libraries (outside the build tree) the resulting linking path will be whatever the id of the library is (use: otool -D /usr/lib/libobjc.A.dylib
to find the id of the lib).