title: CMake use rpath for linked libraries instead of the full path
body: <p>For simplicity, here's an example of what I'm trying to achieve:  </p>
  
  <p>CMakeLists.txt:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>After building it, I run the following command:<br>
  <code>otool -L libexample.dylib</code> which outputs:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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? <code>@rpath/libobjc.A.dylib</code> instead of <code>/usr/lib/libobjc.A.dylib</code></p>
  
  <p>Edit:<br>
  I've tried the solution for this <a href="https://stackoverflow.com/questions/43330165/how-to-link-a-shared-library-with-cmake-with-relative-path">question</a>, but it doesn't seem to fix my issue. The output of the <code>otool</code> command remains the same.</p>
  
  <p>Edit 2:<br>
  The following example still links to the full path and not the rpath:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Edit 3:    </p>
  
  <p>With further investigation, I've come to the conclusion that this wouldn't be possible without using <code>install_name_tool -change</code>. From this <a href="https://cmake.org/Bug/view.php?id=15708" rel="nofollow noreferrer">issue</a>, 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: <code>otool -D /usr/lib/libobjc.A.dylib</code> to find the id of the lib).</p>