3

I am trying to include a dynamic library in my project and I am setting up the compilation using the following CMake script:

find_package( DLIB 18.18.0 REQUIRED )
include_directories( ${DLIB_INCLUDE_DIRS} )
add_executable( executable executable.cxx )
target_link_libraries( executable ${dlib_LIBRARIES} )

If I print the directories of the variable ${dlib_LIBRARIES} I get:

/Users/../INSTALL/lib/libdlib.dylib

Which is correct and compiles. The problem comes when executing the executable and gives the following runtime error:

dyld: Library not loaded: libdlib.18.18.0.dylib
  Referenced from: /Users/.../bin/executable
  Reason: image not found
Trace/BPT trap: 5

And this seem logic because if I run otool -L executable I get a relative path instead of an absolute path:

$otool -L executable
libdlib.18.18.0.dylib (compatibility version 0.0.0, current version 18.18.0)

Why the path shown by otool is not the same as the path added with target_link_libraries and how could I solve this issue with cmake?

Edu
  • 178
  • 1
  • 7
  • 1
    You mean `DLIB_LIBRARIES`, not a `dlib_LIBRARIES`, don't you? – Tsyvarev Dec 16 '15 at 17:13
  • No, the variable name is dlib_LIBRARIES with lowercase as stated in the dlibConfig.CMake file. I amb also running: otool -l | grep LC_RPATH -A2 to se the RPATHs as is recomended in https://cmake.org/Wiki/CMake_RPATH_handling But it seems the RPATH is not set since the output is nothing – Edu Dec 16 '15 at 17:17
  • 2
    Wiki page you refer says about [MACOSX_RPATH](https://cmake.org/cmake/help/v3.0/prop_tgt/MACOSX_RPATH.html) property, which affects on using `@path` instead of directory portion of the library. Probably, this property is set for your target (it is set automatically since policy CMP0042 was introduced). As for lower/upper case selection for `dlyb` prefix of the variables' names, it seems you need to use lowercase: `find_package(dlib)`, `dlib_INCLUDE_DIRS`, `dlib_LIBRARIES`. – Tsyvarev Dec 16 '15 at 20:18
  • I changed the lower/upper case to dlib, dlib_INCLUDE_DIRS and dlib_LIBRARIES. I have also added the following lines of code to manage RPATH: set( MACOSX_RPATH ON ) list( APPEND CMAKE_INSTALL_RPATH ${CMAKE_PREFIX_PATH}/lib ) set( CMAKE_BUILD_WITH_INSTALL_RPATH TRUE ) Now, when running this command: otool -l executable | grep LC_RPATH -A2 It appears in RPATH the installation path of the dynamic libraries correctly: path /Users/.../INSTALL/lib But there must be something else because it isn't able to find the dylib yet – Edu Dec 17 '15 at 09:07
  • Hmm, actual library's file is `libdlib.dylib`, but filename, embedded into executable, is `libdlib.18.18.0.dylib`, isn't it? I have seen several questions on SO releated to such difference. – Tsyvarev Dec 17 '15 at 12:16

0 Answers0