使用 opencv 进行 qt5 部署,如何修复 lib 安装路径?
qt5 deployment with opencv, how to fix the lib install paths?
此处已部分解决类似问题 dyld: Library not loaded: lib/libopencv_core.3.0.dylib Reason: image not found
导致解决方案 there 对我不起作用。
我使用 Qt 5,openCV 3 Mac OS X 10.10.5。
当我使用 macdeployqt 时,此过程没有 openCV .dylib 文件的正确路径。它抱怨:
ERROR: no file at "/lib/libopencv_core.3.0.dylib"
ERROR: no file at "/lib/libopencv_highgui.3.0.dylib"
ERROR: no file at "/lib/libopencv_imgproc.3.0.dylib"
这与错误的文件路径有关,给出的是相对的但解释为绝对的,根据:
$ otool -L myApp.app/Contents/MacOS/myApp
myApp.app/Contents/MacOS/myApp:
(...)
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)
(...)
当然,当应用程序在另一台计算机上运行时会崩溃,并抱怨上面缺少库。
鉴于上述解决方案不起作用,我该如何解决这个问题?或者我如何 "better" 在这种情况下应用上面的解决方案? (我可能没有正确调整它们)。
我尝试了 3 种不同的 opencv 3 安装:CMake、homebrew 和 macports。问题仍然相同。
工作中的好心帮助我更好地理解了如何调整上述使用 install_name_tool 的解决方案。因为上述解决方案不适用于我的案例,所以这里有一个脚本,其中包含详细描述每个步骤的注释。此脚本假定您已经使用过 macdeployedqt,并且您有一个 .app 文件。在链接 opencv_core、opencv_highgui 和 opencv_imgproc 库时,此修复适用于 opencv 3。对于您自己的情况,您需要将 LIB_DIR 和 APP_DIR 更改为(分别)您的 opencv 库文件 (.dylib) 所在的目录,以及您的 .app 包的路径(这里是我的应用名为 Qtfits_openmp)。当然,可以有一个更紧凑的脚本来解决这个问题,但我希望详细的步骤能帮助任何人适应 his/her 自己的情况。
#! /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
此处已部分解决类似问题 dyld: Library not loaded: lib/libopencv_core.3.0.dylib Reason: image not found 导致解决方案 there 对我不起作用。
我使用 Qt 5,openCV 3 Mac OS X 10.10.5。 当我使用 macdeployqt 时,此过程没有 openCV .dylib 文件的正确路径。它抱怨:
ERROR: no file at "/lib/libopencv_core.3.0.dylib"
ERROR: no file at "/lib/libopencv_highgui.3.0.dylib"
ERROR: no file at "/lib/libopencv_imgproc.3.0.dylib"
这与错误的文件路径有关,给出的是相对的但解释为绝对的,根据:
$ otool -L myApp.app/Contents/MacOS/myApp
myApp.app/Contents/MacOS/myApp:
(...)
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)
(...)
当然,当应用程序在另一台计算机上运行时会崩溃,并抱怨上面缺少库。
鉴于上述解决方案不起作用,我该如何解决这个问题?或者我如何 "better" 在这种情况下应用上面的解决方案? (我可能没有正确调整它们)。
我尝试了 3 种不同的 opencv 3 安装:CMake、homebrew 和 macports。问题仍然相同。
工作中的好心帮助我更好地理解了如何调整上述使用 install_name_tool 的解决方案。因为上述解决方案不适用于我的案例,所以这里有一个脚本,其中包含详细描述每个步骤的注释。此脚本假定您已经使用过 macdeployedqt,并且您有一个 .app 文件。在链接 opencv_core、opencv_highgui 和 opencv_imgproc 库时,此修复适用于 opencv 3。对于您自己的情况,您需要将 LIB_DIR 和 APP_DIR 更改为(分别)您的 opencv 库文件 (.dylib) 所在的目录,以及您的 .app 包的路径(这里是我的应用名为 Qtfits_openmp)。当然,可以有一个更紧凑的脚本来解决这个问题,但我希望详细的步骤能帮助任何人适应 his/her 自己的情况。
#! /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