环境
Qt版本5.13.0 安装完毕 可找国内镜像 下载地址 http://download.qt.io/archive/qt/
cmake 3.17.0 rc1 win64 安装完毕 下载地址 https://cmake.org/download/
opencv 3.4.10 国内镜像下载地址 https://www.bzblog.online/opencv/ win系统下载 zip包即可
操作步骤
1.打开CMAKE,选择opencv的源码文件,再选择一个文件夹存放需要编译的文件。
2.点击 configure 按钮,按如下选项配置,这里我们需要选择特定的Qt的编译器。
3.这里的目录为Qt的安装目录, D:\Qt\Qt5.13.0\Tools\mingw730_32\bin,选择好后配置完成。
配置页面如下:
4.CMake勾选:WITH_OPENGL和WITH_QT两项
去掉 WITH_CUDA 和 BUILD_opencv_ts 两项
之后gennerate
出现配置完成和生成完成的命令后,在编译源码的文件键下按住SHIFT键和鼠标右键,打开powershell窗口。
若是在上述过程中出现 Qt5_DIR-NOTFOUND错误
在源码文件中 找到CMakeLists.txt 在下图位置中加入
set(Qt5_DIR "D:/Qt/Qt5.13.0/5.13.0/mingw73_32/lib/cmake/Qt5") 里面的目录为你qt的安装目录
5.输入 mingw32-make 命令回车
然后等待编译其中可能会出现很多问题。,编译时间比较长。
编译后 在输入 mingw32-make install 进行安装 安装完成后
我们再创建一个存放opencv库的文件夹,在编译后的文件夹里,拷出/install/include lib/ bin/ 这三个文件夹至新文件夹
6.配置Qt.pro文件,在pro文件加入如下:
INCLUDEPATH += E:\opencv\OpencvBuildLib\include \
E:\opencv\OpencvBuildLib\include\opencv \
E:\opencv\OpencvBuildLib\include\opencv2
CONFIG(debug, debug|release): {
LIBS += E:\opencv\OpencvBuildLib\lib\libopencv_*.a
} else:CONFIG(release, debug|release): {
LIBS += E:\opencv\OpencvBuildLib\lib\libopencv_*.a
}
7.随便找的测试Demo,编译运行通过就表示成功了。
测试Demo链接 https://cclplus.coding.net/public/QtCV/QtCV/git/files
若是编译可以通过,但在程序运行时,一直报 Qt:make: Nothing to be done for 'first'错误
说明你生成的库是不正确的,按上述操作再生成一遍即可
mingw32-make 问题解决汇总(可能由于版本冲突等问题,编译库时会出现很多错误,问题错误解决汇总在下面)
1.
modules\videoio\CMakeFiles\opencv_videoio.dir\build.make:187: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj' failed
mingw32-make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj] Error 1
CMakeFiles\Makefile2:2867: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
mingw32-make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2在#include "DShow.h"这行的上面加一行#define NO_DSHOW_STRSAFE
原文链接:https://blog.csdn.net/qq_26625581/article/details/77744057
2.
v:\MinGW-Builds\mingw64\bin\windres.exe: supported targets: pe-x86-64 pei-x86-64 pe-bigobj-x86-64 elf64-x86-64 elf64-l1om elf64-k1om pe-i386 pei-i386 elf32-i386 elf32-iamcu elf64-little elf64-big elf32-little elf32-big plugin srec symbolsrec verilog tekhex binary ihex modules/core/CMakeFiles/opencv_core.dir/build.make:1642: recipe for target `modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj' failed make[2]: *** [modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1 CMakeFiles/Makefile2:1512: recipe for target `modules/core/CMakeFiles/opencv_core.dir/all' failed make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2 Makefile:162: recipe for target `all' failed make: *** [all] Error 2
去掉ENABLE_PRECOMPILED_HEADERS再make(cmake-3.9+版本的bug) generate后 继续mingw-make
原文链接:https://blog.csdn.net/qq_26625581/article/details/77744057
3.
D:\CodeBlocks\MinGW\bin\windres.exe: preprocessing failed.
modules\core\CMakeFiles\opencv_core.dir\build.make:1351: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj' failed
mingw32-make[2]: *** [modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
CMakeFiles\Makefile2:2236: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/all' failed
mingw32-make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
Makefile:178: recipe for target 'all' failed
mingw32-make: *** [all] Error 2将CMake中的 OPENCV/OPENCV_ENABLE_ALLOCATOR_STATS 不打勾 generate后 继续mingw-make