工作中的善良灵魂帮助我更好地理解如何调整使用 install_name_tool 的上述解决方案。因为上述解决方案不适用于我的案例,所以这里有一个脚本,其中包含详细描述每个步骤的注释。该脚本假定您已经使用了 macdeployedqt,并且您有一个 .app 文件。在链接 opencv_core、opencv_highgui 和 opencv_imgproc 库时,此修复适用于 opencv 3。对于您自己的情况,您需要将 LIB_DIR 和 APP_DIR 更改为(分别)您的 opencv 库文件 (.dylib) 所在的目录,以及您的 .app 包的路径(这里我的应用程序称为 Qtfits_openmp)。当然,可以制作一个更紧凑的脚本,但我希望详细的步骤可以帮助任何人适应他/她自己的情况。
#! /bin/bash
LIB_DIR=~/Dev/opencv3_tbb_opencl/lib
APP_DIR=~/Dev/build-Qtfits_openmp-clang_omp-Release/Qtfits_openmp.app
# This script supposes here that you are in the parent directory of the app.
# check to what libs your app is "explicitly" linked to with otool -L $APP_DIR/Contents/MacOS/Qtfits_openmp
# If originally linking with LIBS += -lopencv_core -lopencv_highgui -lopencv_imgproc, you'd need to copy them in the .app with their dependencies.
# However, opencv has the path of symbolic links, ending with 3.0. , so we need to copy the actual files, i.e., with the whole version number 3.0.0.
cp $LIB_DIR/libopencv_core.3.0.0.dylib $APP_DIR/Contents/Frameworks/libopencv_core.3.0.0.dylib
cp $LIB_DIR/libopencv_highgui.3.0.0.dylib $APP_DIR/Contents/Frameworks/libopencv_highgui.3.0.0.dylib
cp $LIB_DIR/libopencv_imgproc.3.0.0.dylib $APP_DIR/Contents/Frameworks/libopencv_imgproc.3.0.0.dylib
# Extra dependencies exist: otool -L libopencv_* will show you all the dependencies that you need to copy.
# Here we will also need to add imgcodecs and videoio to the .app
cp $LIB_DIR/libopencv_imgcodecs.3.0.0.dylib $APP_DIR/Contents/Frameworks/libopencv_imgcodecs.3.0.0.dylib
cp $LIB_DIR/libopencv_videoio.3.0.0.dylib $APP_DIR/Contents/Frameworks/libopencv_videoio.3.0.0.dylib
# With otool -L $APP_DIR/Contents/MacOS/yourApp , look at the .lib files with a wrong path. With the following result:
# 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)
# we need to fix the 3 libraries core, highgui and imgproc which are given here with an incorrect relative path.
# Note that again, opencv kept the basename of the symbolic links (ending with 3.0.dylib), not the basename of the actual files (ending with 3.0.0.dylib)
# So we have to keep things consistent with what we did above.
install_name_tool -change lib/libopencv_core.3.0.dylib @executable_path/../Frameworks/libopencv_core.3.0.0.dylib $APP_DIR/Contents/MacOS/Qtfits_openmp
install_name_tool -change lib/libopencv_highgui.3.0.dylib @executable_path/../Frameworks/libopencv_highgui.3.0.0.dylib $APP_DIR/Contents/MacOS/Qtfits_openmp
install_name_tool -change lib/libopencv_imgproc.3.0.dylib @executable_path/../Frameworks/libopencv_imgproc.3.0.0.dylib $APP_DIR/Contents/MacOS/Qtfits_openmp
# Now we need to fix the path to all the dependencies of each library file.
cd $APP_DIR/Contents/Frameworks
# Change the IDs
for i in libopencv*.dylib; do install_name_tool -id @executable_path/../Frameworks/$i $i; done
# Again, with otool -L libopencv_* , you will see that you need to change their paths so that your copied libraries are pointed to within the app.
# for each opencv lib that is not properly referenced, change it from the absolute or wrong-relative path to @executable_path/../Frameworks/
# fix libopencv_highgui
install_name_tool -change $LIB_DIR/libopencv_imgcodecs.3.0.dylib @executable_path/../Frameworks/libopencv_imgcodecs.3.0.0.dylib libopencv_highgui.3.0.0.dylib
install_name_tool -change $LIB_DIR/libopencv_core.3.0.dylib @executable_path/../Frameworks/libopencv_core.3.0.0.dylib libopencv_highgui.3.0.0.dylib
install_name_tool -change $LIB_DIR/libopencv_imgproc.3.0.dylib @executable_path/../Frameworks/libopencv_imgproc.3.0.0.dylib libopencv_highgui.3.0.0.dylib
install_name_tool -change $LIB_DIR/libopencv_videoio.3.0.dylib @executable_path/../Frameworks/libopencv_videoio.3.0.0.dylib libopencv_highgui.3.0.0.dylib
# fix libopencv_imgproc
install_name_tool -change $LIB_DIR/libopencv_core.3.0.dylib @executable_path/../Frameworks/libopencv_core.3.0.0.dylib libopencv_imgproc.3.0.0.dylib
# fix libopencv_videoi
install_name_tool -change $LIB_DIR/libopencv_imgcodecs.3.0.dylib @executable_path/../Frameworks/libopencv_imgcodecs.3.0.0.dylib libopencv_videoio.3.0.0.dylib
install_name_tool -change $LIB_DIR/libopencv_imgproc.3.0.dylib @executable_path/../Frameworks/libopencv_imgproc.3.0.0.dylib libopencv_videoio.3.0.0.dylib
install_name_tool -change $LIB_DIR/libopencv_core.3.0.dylib @executable_path/../Frameworks/libopencv_core.3.0.0.dylib libopencv_videoio.3.0.0.dylib
# fix libopencv_imgcodecs
install_name_tool -change $LIB_DIR/libopencv_imgproc.3.0.dylib @executable_path/../Frameworks/libopencv_imgproc.3.0.0.dylib libopencv_imgcodecs.3.0.0.dylib
install_name_tool -change $LIB_DIR/libopencv_core.3.0.dylib @executable_path/../Frameworks/libopencv_core.3.0.0.dylib libopencv_imgcodecs.3.0.0.dylib