title: CMAKE: How do I install executable along with required dylib to custom directory?
body: <p>So I'm using CMake to build a C++ project (on Mac OS) and my project relies on a dylib (I'm using TBB <a href="https://www.threadingbuildingblocks.org/" rel="nofollow noreferrer">https://www.threadingbuildingblocks.org/</a> but the specific library itself doesn't matter) </p>
  
  <p>If I do a standard "cmake" and "make" it builds the executable where I want it and when I run my app, the dylib links correctly and everything works perfectly. </p>
  
  <p>The problem comes in when I try to do a "make install" and try to run the resulting executable from the install directory. I get an "image not found" error: </p>
  
  <pre><code>dyld: Library not loaded: @rpath/libtbb.dylib
  Referenced from: 
  /Users/MyName/Desktop/ProjectRoot/install/./MyApp
  Reason: image not found
  </code></pre>
  
  <p>Interestingly, if I do a regular "make" without an install, and then manually copy over the executable to the install directory, then that <em>will</em> link against my dylib properly. I have no idea why that is. </p>
  
  <p>My directory structure is as follows: </p>
  
  <pre><code>Root 
      CMakeLists.txt 
      Source/ 
      Libraries/ 
          tbb/ 
              include/ 
              lib/ 
                  libtbb.dylib
      install/ 
  </code></pre>
  
  <p>...and my CMakeLists.txt file is below:</p>
  
  <pre><code># Start of CMakeLists.txt
  cmake_minimum_required(VERSION 3.3)
  project (MyApp)
  
  # Set C++ version and output paths
  set (CMAKE_CXX_STANDARD 14)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
  set(CMAKE_MACOSX_RPATH 1)
  set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/install")
  set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
  
  # Find TBB
  find_library (
      TBB_LIBRARIES
      NAMES tbb libtbb # what to look for
      HINTS "${CMAKE_SOURCE_DIR}/Libraries/tbb/lib" # where to look
      NO_DEFAULT_PATH # do not search system default paths
  )
  
  # Set Custom Include Files + TBB header files 
  include_directories(Source/Headers Libraries/tbb/include)
  
  
  # Set Source Files
  file(GLOB_RECURSE SRC_FILES "Source/*.cpp")
  add_executable(MyApp ${SRC_FILES})
  
  # Link Libraries 
  target_link_libraries(MyApp ${TBB_LIBRARIES})
  
  # Set compile flags
  set_target_properties(MyApp PROPERTIES CXX_STANDARD 14) #LINK_FLAGS "-Wl")
  target_compile_features(MyApp PUBLIC cxx_std_14)
  
  # Install executable
  install(TARGETS MyApp DESTINATION .)
  </code></pre>
  
  <p>If I try to also add the following line, and install the dylib as well: </p>
  
  <pre><code>install(TARGETS ${TBB_LIBRARIES} DESTINATION lib)
  </code></pre>
  
  <p>then when I do a "make install" I get the following error instead: </p>
  
  <pre><code>install TARGETS given target 
  "/Users/MyName/Desktop/ProjectRoot/Libraries/tbb/lib/libtbb.dylib"
  which does not exist in this directory.
  </code></pre>
  
  <p>So I just can't seem to get this install to work. How do I fix it so that both my executable and my library get installed in the right place and that my executable will be able to link against my library when run? </p>