title: macports, cmake/make and 'dyld: Library not loaded'
body: <p>I recently upgraded a library I use in a project of mine (glfw-devel) with macports and now my executable is dynamically link with <code>lib/libglfw.3.dylib</code> instead of <code>opt/local/lib/libglfw.3.dylib</code> which is the actual location of the lib so I get a <code>dyld: Library not loaded</code> error. </p>
  
  <p>I know how to fix this with <code>install-names</code> or with a simple copy. What I cannot understand, however, is why this happens suddenly after a simple upgrade. I haven't changed anything in my <code>CMakeLists.txt</code> (the most relevant part is below) and all the other libraries I use have correct paths. Note that there is no place in the file where I refer to <code>lib/</code>.</p>
  
  <p>Could it be a macports related issue? How can I make sure that my future binaries will be compiled with the proper paths?</p>
  
  <pre><code>set(CMAKE_CXX_COMPILER clang++)
  set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wall -std=c++14 -stdlib=libc++ -DBOOST_LOG_USE_NATIVE_SYSLOG")
  set (SOURCES       src/helper.cpp
             src/capture.cpp
             src/imageBuffer.cpp
             src/projection.cpp
             )
  set (APP_SOURCES   src/main.cpp )      
  set (TEST_SOURCES  src/tests.cpp
                     lib/gmock-all.cc
             lib/gtest-all.cc)
  include_directories(/opt/local/include
              /usr/local/include
              /usr/local/include/gmock
              /usr/local/include/gtest
              src
              )
  set (DYN_LINK       /opt/local/lib
                      /usr/local/lib)
  set (STATIC_LINK    static)
  
  set(CMAKE_INSTALL_PREFIX  "${CMAKE_BINARY_DIR}/Install")
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Install")
  
  find_package(Boost 1.59.0 COMPONENTS
                  log
              log_setup
              thread
              date_time
              filesystem
              system
              program_options
              REQUIRED)     
  find_package(OpenCV 3.0 REQUIRED)
  if (OpenCV_FOUND)
    message( "-- OpenCV found")
  endif()
  
  find_package(OpenGL REQUIRED)
  if (OpenGL_FOUND)
    message( "-- OpenGL found")
  endif()
  
  include_directories(${Boost_INCLUDE_DIRS})
  include_directories(${OpenCV_INCLUDE_DIRS})
  
  #user configuration
  if(${CMAKE_LINKAGE} MATCHES Static)
    message("-- STATIC LINKAGE SELECTED")
    link_directories(${STATIC_LINK})
    set(Boost_USE_STATIC_LIBS ON)
    set(Boost_USE_STATIC_RUNTIME OFF)
    set(CMAKE_FIND_LIBRARY_SUFFIXES   ".a")
    set(BUILD_SHARED_LIBRARIES OFF)
  else()
    message("-- DYNAMIC LINKAGE SELECTED")
    #find libraries
    link_directories(${DYN_LINK})
    link_directories(${Boost_LIBRARY_DIR})
    link_directories(${Boost_LIBRARY_DIR})
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBOOST_ALL_DYN_LINK")
    set(Boost_USE_STATIC_LIBS OFF)
  endif()
  
  if(${CMAKE_BUILD_MODE} MATCHES Release)
    message("-- RELEASE MODE ON")
    set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -O3")
  else()
    message("-- DEBUG MODE ON")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DDEBUG")
  endif()
  
  # core
  add_library(core ${SOURCES})
  target_link_libraries(core ${OpenCV_LIBS})
  target_link_libraries(core ${Boost_LIBRARIES})
  target_link_libraries(core ${OPENGL_LIBRARIES})
  target_link_libraries(core GLEW)
  target_link_libraries(core glfw)
  </code></pre>