title: MAC OS: dynamically linked library not found despite correct rpath
body: <p>I have a (relatively) big project C++ that compiles and runs fine on ubuntu (using cmake/catkin). 
  It compiles fine on mac os, but when trying to start the executable, I get the error message:</p>
  
  <pre><code>dyld: Library not loaded: &lt;name of library&gt;.dylib
    Referenced from:
    &lt;path to executable&gt;/&lt;executable&gt;
  Reason: image not found
  </code></pre>
  
  <p>When running the command:</p>
  
  <pre><code>otool -l &lt;executable&gt; | grep LC_RPATH -A2
  </code></pre>
  
  <p>I get the output:</p>
  
  <pre><code>      cmd LC_RPATH
    cmdsize 64
       path &lt;correct absolute path to folder containing library&gt; (offset 12)
  
        cmd LC_RPATH
    cmdsize 24
       path /sw/lib (offset 12)
  
        cmd LC_RPATH
    cmdsize 32
       path /usr/X11/lib (offset 12)
  
        cmd LC_RPATH
    cmdsize 32
       path /opt/local/lib (offset 12)
  
        cmd LC_RPATH
    cmdsize 32
       path /opt/X11/lib (offset 12)
  </code></pre>
  
  <p>It is very unclear to me why the library is not found.
  Running:</p>
  
  <pre><code>otool -L &lt;executable&gt;
  </code></pre>
  
  <p>prints:</p>
  
  <pre><code>&lt;executable name&gt;:
      &lt;library name&gt;.dylib (compatibility version 0.0.0, current version 0.0.0)
      /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
      /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
      /opt/X11/lib/libglut.3.dylib (compatibility version 13.0.0, current version 13.0.0)
      /opt/X11/lib/libGL.1.dylib (compatibility version 1.2.0, current version 1.2.0)
      /opt/X11/lib/libGLU.1.dylib (compatibility version 1.3.0, current version 1.3.0)
      /opt/X11/lib/libX11.6.dylib (compatibility version 10.0.0, current version 10.0.0)
      /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
  </code></pre>
  
  <p>which seems to confirm it does not get the right path.</p>
  
  <p>Anything I am missing ?</p>
  
  <p>ps:</p>
  
  <p>not sure it is relevant, here the cmake commands I used:
  (from <a href="http://www.cmake.org/Wiki/CMake_RPATH_handling">here</a>)</p>
  
  <pre><code># 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/${MACHTYPE}")
  
  # 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/${MACHTYPE}" isSystemDir)
  IF("${isSystemDir}" STREQUAL "-1")
      SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/${MACHTYPE}")
  ENDIF("${isSystemDir}" STREQUAL "-1")
  </code></pre>