title: Cmake : How the dynamic linker locates dynamic libraries when loading an executable?
body: <p>I'm trying to understand how to build C++ project with CMake.
  I'm not familiar with C++.
  I'm working on mac Os X and i'd like share the app on Linux too. </p>
  
  <p>This is my project directory architecture. You can find here on <a href="https://github.com/ZheFrenchKitchen" rel="nofollow noreferrer">github</a></p>
  
  <p><a href="https://i.stack.imgur.com/FylvY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FylvY.png" alt="This is my project directory architecture."></a></p>
  
  <p>I'have done a program with static and shared library to see how it works.
  I read about <a href="http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html" rel="nofollow noreferrer">shared and static libraries</a> ,<a href="https://stackoverflow.com/questions/2649334/difference-between-static-and-shared-libraries">here on stackoverflow</a> , and Cmake stuffs <a href="http://derekmolloy.ie/hello-world-introductions-to-cmake/" rel="nofollow noreferrer">1</a> &amp; <a href="http://mirkokiefer.com/blog/2013/03/cmake-by-example/" rel="nofollow noreferrer">2</a></p>
  
  <p>Initialy i was merging all sources in an executable. It was working well.
  But the goal of a shared is not to be packaged, isn't it ?
  It should be access by location.Or when i move my lib and bin under install build. The program was still working. Great but i doubt it should be working as this.
  I mean if i move my shared libray, the program should be not working no more.</p>
  
  <p>So instead of doing ,</p>
  
  <blockquote>
    <p>#ADD_EXECUTABLE(ChimericGenomeMaker "${SOURCES_MAIN}" "${SOURCES_STATIC}" "${SOURCES_SHARED}")</p>
  </blockquote>
  
  <p>I'm trying to use TARGET_LINK_LIBRARIES and maybe (LINK_DIRECTORIES):</p>
  
  <blockquote>
    <p>TARGET_LINK_LIBRARIES(ChimericGenomeMaker "calculMean" )
    TARGET_LINK_LIBRARIES(ChimericGenomeMaker "calculSum" )</p>
  </blockquote>
  
  <p>But it's not working.</p>
  
  <pre><code> ./install/bin/ChimericGenomeMaker 4
   dyld: Library not loaded: libcalculMean.dylib
   Referenced from:  /Users/JP/Desktop/ChimericGenomeMaker/./install/bin/ChimericGenomeMaker
    Reason: image not found
     Trace/BPT trap: 5
  </code></pre>
  
  <p>Using otool -L , tell me what is following : </p>
  
  <pre><code>./install/bin/ChimericGenomeMaker:
      libcalculMean.dylib (compatibility version 0.0.0, current version 0.0.0)
      /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
      /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
  </code></pre>
  
  <p>So ok there is a problem of location of my dylib... And is my first idea (which merge all executables anyway the library they came from) is wrong thinking ?
  What is the good "framework" ?</p>
  
  <p>And last question, anyway shared or static library, if i moved my lib directory, does the program will continue to run correctly ?</p>
  
  <p>Thanks.</p>
  
  <p>This is my CmakeLists.txt <strong>[UPDATED]</strong></p>
  
  <pre><code>#cmake -G "Eclipse CDT4 - Unix Makefiles" ..
  #SET (CMAKE_CXX_COMPILER "/usr/bin/g++")
  
  IF (${APPLE})
    MESSAGE(STATUS "WELCOME TO APPLE")
  ENDIF()
  
  CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
  PROJECT(ChimericGenomeMaker)
  
  SET (ChimericGenomeMaker_VERSION_MAJOR 1)
  SET (ChimericGenomeMaker_VERSION_MINOR 0)
  
  #used by makefile users in order to install software at non-default location
  #make DESTDIR=/home/john install --&gt; prepended with the DESTDIR value
  SET(CMAKE_INSTALL_PREFIX install)
  
  #SET(CMAKE_SKIP_BUILD_RPATH true )
  #Can manually add the sources using the set command as follows:
  FILE(GLOB SOURCES_MAIN "src/main/*.cpp")
  INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/main")
  
  #Generate the shared library from the sources
  FILE(GLOB SOURCES_SHARED "src/shared/*.cpp")
  INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/shared")
  ADD_LIBRARY(calculMean SHARED "${SOURCES_SHARED}")
  INSTALL(TARGETS calculMean DESTINATION "lib/shared")
  #MACOSX_RPATH is not specified for the following targets calcuMean
  
  #Generate the static library from the sources
  FILE(GLOB SOURCES_STATIC "src/static/*.cpp")
  INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/static")
  ADD_LIBRARY(calculSum STATIC "${SOURCES_STATIC}")
  INSTALL(TARGETS calculSum DESTINATION "lib/static")
  
  # Use, i.e. don't skip the full RPATH for the build tree
  # SET(CMAKE_SKIP_BUILD_RPATH  FALSE)
  
  # When building, don't use the install RPATH already (but later on when installing)
  #SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 
  
  #SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/shared")
  
  # add the automatically determined parts of the RPATH which point to directories outside the build tree to the install RPATH
  #SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  
  # the RPATH to be used when installing, but only if it's not a system directory
  #LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib/shared"  isSystemDir)
  # IF("${isSystemDir}" STREQUAL "-1")
  #    SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/shared")
  # ENDIF("${isSystemDir}" STREQUAL "-1")
  
  #Build a stand alone program
  ADD_EXECUTABLE(ChimericGenomeMaker "${SOURCES_SHARED}" "${SOURCES_MAIN}"  )# "${SOURCES_STATIC}" 
  #TARGET_LINK_LIBRARIES(ChimericGenomeMaker calculMean) 
  TARGET_LINK_LIBRARIES(ChimericGenomeMaker calculSum)
  
  INSTALL(TARGETS ChimericGenomeMaker DESTINATION bin)
  
  ADD_CUSTOM_COMMAND(TARGET calculMean
                     POST_BUILD
                     COMMAND "${CMAKE_SOURCE_DIR}/bash/post-build.sh" "HELLO YOU!"
                     WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
                     COMMENT "Post-build is Running" VERBATIM
  )
  </code></pre>
  
  <p>OUTPUT OF CMAKE : </p>
  
  <pre><code>    ./build.sh
  -- Custom Bash Cleaning
  -- Old Install Directory Deleted with sucess
  -- WELCOME TO APPLE
  -- The C compiler identification is AppleClang 6.1.0.6020053
  -- The CXX compiler identification is AppleClang 6.1.0.6020053
  -- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
  -- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - done
  -- Detecting C compile features
  -- Detecting C compile features - done
  -- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
  -- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
  -- Detecting CXX compiler ABI info
  -- Detecting CXX compiler ABI info - done
  -- Detecting CXX compile features
  -- Detecting CXX compile features - done
  -- INSTALL_NAME_DIR:         
  -- CMAKE_BINARY_DIR:         /Users/JP/Desktop/ChimericGenomeMaker/build
  -- CMAKE_CURRENT_BINARY_DIR: /Users/JP/Desktop/ChimericGenomeMaker/build
  -- CMAKE_SOURCE_DIR:         /Users/JP/Desktop/ChimericGenomeMaker
  -- CMAKE_CURRENT_SOURCE_DIR: /Users/JP/Desktop/ChimericGenomeMaker
  -- PROJECT_BINARY_DIR: /Users/JP/Desktop/ChimericGenomeMaker/build
  -- PROJECT_SOURCE_DIR: /Users/JP/Desktop/ChimericGenomeMaker
  -- EXECUTABLE_OUTPUT_PATH: 
  -- LIBRARY_OUTPUT_PATH:     
  -- CMAKE_MODULE_PATH: 
  -- CMAKE_COMMAND: /usr/local/Cellar/cmake/3.2.1/bin/cmake
  -- CMAKE_ROOT: /usr/local/Cellar/cmake/3.2.1/share/cmake
  -- CMAKE_CURRENT_LIST_FILE: /Users/JP/Desktop/ChimericGenomeMaker/CMakeLists.txt
  -- CMAKE_CURRENT_LIST_LINE: 122
  -- CMAKE_INCLUDE_PATH: 
  -- CMAKE_LIBRARY_PATH: 
  -- CMAKE_SYSTEM: Darwin-14.4.0
  -- CMAKE_SYSTEM_NAME: Darwin
  -- CMAKE_SYSTEM_VERSION: 14.4.0
  -- CMAKE_SYSTEM_PROCESSOR: x86_64
  -- UNIX: 1
  -- WIN32: 
  -- APPLE: 1
  -- MINGW: 
  -- CYGWIN: 
  -- BORLAND: 
  -- MSVC: 
  -- MSVC_IDE: 
  -- MSVC60: 
  -- MSVC70: 
  -- MSVC71: 
  -- MSVC80: 
  -- CMAKE_COMPILER_2005: 
  -- CMAKE_SKIP_RULE_DEPENDENCY: 
  -- CMAKE_SKIP_INSTALL_ALL_DEPENDENCY: 
  -- CMAKE_SKIP_RPATH: NO
  -- CMAKE_VERBOSE_MAKEFILE: FALSE
  -- CMAKE_SUPPRESS_REGENERATION: 
  -- CMAKE_C_FLAGS: 
  -- CMAKE_CXX_FLAGS: 
  -- CMAKE_BUILD_TYPE: 
  -- BUILD_SHARED_LIBS: 
  -- CMAKE_C_COMPILER: /Library/Developer/CommandLineTools/usr/bin/cc
  -- CMAKE_CXX_COMPILER: /Library/Developer/CommandLineTools/usr/bin/c++
  -- CMAKE_COMPILER_IS_GNUCC: 
  -- CMAKE_COMPILER_IS_GNUCXX : 
  -- CMAKE_AR: /Library/Developer/CommandLineTools/usr/bin/ar
  -- CMAKE_RANLIB: /Library/Developer/CommandLineTools/usr/bin/ranlib
  -- DESKTOP : 
  -- PATH : /Users/JP/.jenv/shims:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
  -- Configuring done
  CMake Warning (dev):
    Policy CMP0042 is not set: MACOSX_RPATH is enabled by default.  Run "cmake
    --help-policy CMP0042" for policy details.  Use the cmake_policy command to
    set the policy and suppress this warning.
  
    MACOSX_RPATH is not specified for the following targets:
  
     calculMean
  
  This warning is for project developers.  Use -Wno-dev to suppress it.
  
  -- Generating done
  -- Build files have been written to: /Users/JP/Desktop/ChimericGenomeMaker/build
  Scanning dependencies of target calculSum
  [ 20%] Building CXX object CMakeFiles/calculSum.dir/src/static/calc_sum.cpp.o
  Linking CXX static library libcalculSum.a
  [ 20%] Built target calculSum
  Scanning dependencies of target ChimericGenomeMaker
  [ 40%] Building CXX object CMakeFiles/ChimericGenomeMaker.dir/src/shared/calc_mean.cpp.o
  [ 60%] Building CXX object CMakeFiles/ChimericGenomeMaker.dir/src/main/foo.cpp.o
  [ 80%] Building CXX object CMakeFiles/ChimericGenomeMaker.dir/src/main/main.cpp.o
  Linking CXX executable ChimericGenomeMaker
  [ 80%] Built target ChimericGenomeMaker
  Scanning dependencies of target calculMean
  [100%] Building CXX object CMakeFiles/calculMean.dir/src/shared/calc_mean.cpp.o
  Linking CXX shared library libcalculMean.dylib
  Post-build is Running
  POST-BUILD SCRIPT
  HELLO YOU!
  [100%] Built target calculMean
  [ 20%] Built target calculSum
  [ 80%] Built target ChimericGenomeMaker
  [100%] Built target calculMean
  Install the project...
  -- Install configuration: ""
  -- Installing: /Users/JP/Desktop/ChimericGenomeMaker/build/install/lib/shared/libcalculMean.dylib
  -- Installing: /Users/JP/Desktop/ChimericGenomeMaker/build/install/lib/static/libcalculSum.a
  -- Installing: /Users/JP/Desktop/ChimericGenomeMaker/build/install/bin/ChimericGenomeMaker
  m1-p19-gen:ChimericGenomeMaker JP$ 
  </code></pre>