cmake install_name_tool absolute path for library on Mac OSX
When I compile OpenCV for Mac OSX 10.8 using XCode 4.5.2 and cmake 2.8.9 my installed libraries aren't configured with the correct corresponding installation path.
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=RELEASE -D WITH_QT=ON -D WITH_OPENGL=ON -DCMAKE_INSTALL_PREFIX=/opt/local ../opencv.ram/
The CMAKE_INSTALL_PREFIX=/opt/local puts the libraries in the correct place after a "make install" but the embedded path names are configured with a relative path name such as "lib/libopencv" and not "/opt/local/lib/libopencv" as desired.
otool -D /opt/local/lib/libopencv_core.2.4.3.dylib /opt/local/lib/libopencv_core.2.4.3.dylib: lib/libopencv_core.2.4.dylib
The libraries in the build location after the make command but before the "make install" are configured with the correct absolute path name corresponding to the build tree location:
otool -D /Volumes/ramdisk/osx_ffmpeg/lib/libopencv_core.2.4.3.dylib /Volumes/ramdisk/osx_ffmpeg/lib/libopencv_core.2.4.3.dylib: /Volumes/ramdisk/osx_ffmpeg/lib/libopencv_core.2.4.dylib
The relative path seems to be set during installation in the generated cmake_install.cmake file via the install_name_tool command as shown here.
grep -C1 install_name /Volumes/ramdisk/osx_ffmpeg/modules/core/cmake_install.cmake NOT IS_SYMLINK "${file}") EXECUTE_PROCESS(COMMAND "/usr/bin/install_name_tool" -id "lib/libopencv_core.2.4.dylib"
Basically I'd like to configure the installed libraries with correct embedded absolute path names, so that I can link to them directly without using DYLD_LIBRARY_PATH or the like. Presumably this is the desired default build behavior? Perhaps my build syntax is missing something.
I have found a number of links to folks who have remedied this with post installation scripts using the install_name_tool command to modify the path names, such as shown in the link below, but I'm guessing there is a way to avoid this.
install_name_tool -id pwd
/opencv/lib/libopencv_core.2.1.dylib opencv/lib/libopencv_core.2.1.dylib
If I omit the CMAKE_INSTALL_PREFIX=/opt/local cmake option the libraries are installed in the default /usr/local/lib location but still have a relative embedded path name.
UPDATE: I haven't found a cmake fix for this but did create a simpler post installation bash script that should fix the install path for opencv libraries and their dependencies:
CV_LIB_PATH=/opt/local/lib
find ${CV_LIB_PATH} -type f -name "libopencv*.dylib" -print0 | while IFS="" read -r -d "" dylibpath; do
echo install_name_tool -id "$dylibpath" "$dylibpath"
install_name_tool -id "$dylibpath" "$dylibpath"
otool -L $dylibpath | grep libopencv | tr -d ':' | while read -a libs ; do
[ "${file}" != "${libs[0]}" ] && install_name_tool -change ${libs[0]} ${CV_LIB_PATH}/`basename ${libs[0]}` $dylibpath
done
done
I'm running into the same issue with Qt5 and macdeployqt, which complains about wrong paths (the relative ones mentioned above) to the opencv .dylib. Any update on this? None of the proposed solution here solved the problem. The relative paths are: lib/libopencv_core.3.0.dylib (compatibility version 3.0.0, current version 3.0.0) lib/libopencv_highgui.3.0.dylib (compatibility version 3.0.0, current version 3.0.0) lib/libopencv_imgproc.3.0.dylib (compatibility version 3.0.0, current version 3.0.0)
A simple solution is
cmake -D BUILD_SHARED_LIBS=OFF
. Static libraries do not use relative path.