QT + OpenCV (使用MinGW构建项目)在Windows系统下环境搭建 (免编译)(很详细)
我在使用QT+ OpenCV在win10上搭建使用QT自带的MinGW编译器构建项目的环境时,自己下载release resource文件,然后使用QT或者VS或者Cmake进行编译后,总是出现问题。主要有以下几种:
1、下载的源文件使用QT或者Cmake编译时总会失败,原因不明,有可能是版本问题,也有可能是其他问题,总之没找到原因。使用QT或者Cmake + VS编译成功后,添加了库文件路径,但是就是提示找不到,最后也没解决。
2、找不到库文件。不管怎么进行路径配置都找不到。
3、库文件配置正确,可以成功构建,但是程序运行会崩溃,提示crashed。最初不知道原因,最后的后来发现很可能是环境变量添加有误,或者添加正确,但是系统没有立即生效,需要注销或者重启使其生效。但是在自己编译的文件应用失败后已经删除了配置和文件,懒得去重新测试该方法,所以只是理论上找到了原因。
最后发现在OpenCV官网上下载官方MinGW编译好的源文件,然后自己添加路径,可以使用, 下边就是我对这种方法的成功使用写的详细说明,我自己目前也是这种方法。
首先在 https://github.com/huihut/OpenCV-MinGW-Build 网站下载编译好的文件压缩包,自行选择版本,我自己下载了4.1.0 的64位版本和 3.3.1的版本(3.3.1的版本官网上没标系统位数,自己使用时,使用32位编译器可以,但是64位就不行,我觉得可能没标x64的就是默认32位的,也可能是我理解有误)。
下载完成后,将压缩包解压到一个文件夹内,比如我直接放在了E:\OpenCVLib 文件夹内:
1、直接在工程的 .pro 文件内添加绝对路径和库文件配置,记得把bin文件的路径添加到系统环境变量中,最好注销或者重启下电脑。
2、在该文件夹内新建一个 .pri 的文件(如果新建txt文件重命名 .pri 可以的话也行,不行就在Qt工程里新建一个空白文件,如下图,然后重命名为 .pri 文件)。然后在该文件中添加绝对路径和库文件配置。
最后在工程文件的 .pro 文件内添加一句:
include(E:/OpenCVLib/lib64_410.pri)
或
include(“E:/OpenCVLib/lib64_410.pri”)
附录
下边是 .pri 文件内容仅是格式参考,具体路径根据自己的情况修改。
1、32位绝对路径编译器配置_3_3_1版本
################## 32位版本编译器配置
INCLUDEPATH += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/include\
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/include/opencv\
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/include/opencv2
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_calib3d331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_core331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_dnn331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_features2d331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_flann331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_highgui331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_imgcodecs331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_imgproc331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_ml331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_objdetect331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_photo331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_shape331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_stitching331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_video331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_videoio331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_videostab331.dll
LIBS += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-3.3.1/bin/libopencv_superres331.dll
##############################################
2、64位绝对路径编译器配置_4_1_0版本
################################## 64位版本编译器配置
INCLUDEPATH += E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/include\
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/include/opencv2
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_calib3d410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_core410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_dnn410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_features2d410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_flann410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_highgui410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_imgcodecs410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_imgproc410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_ml410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_objdetect410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_photo410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_stitching410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_video410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_videoio410.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/opencv_ffmpeg410_64.dll
LIBS +=
E:/OpenCVLib/OpenCV-MinGW-Build-OpenCV-4.1.0-x64/x64/mingw/bin/libopencv_gapi410.dll
################################################
3、相对路径示例:
该配置是将bin和include文件夹均复制到工程文件夹里,然后设置的相对路径。
INCLUDEPATH += $$PWD/include\
$$PWD/include/opencv\
$$PWD/include/opencv2
LIBS += $$PWD/bin/libopencv_calib3d331.dll \
$$PWD/bin/libopencv_core331.dll \
$PWD/bin/libopencv_dnn331.dll \
$$PWD/bin/libopencv_features2d331.dll \
$$PWD/bin/libopencv_flann331.dll \
$$PWD/bin/libopencv_highgui331.dll \
$$PWD/bin/libopencv_imgcodecs331.dll \
$$PWD/bin/libopencv_imgproc331.dll \
$$PWD/bin/libopencv_ml331.dll \
$$PWD/bin/libopencv_objdetect331.dll \
$$PWD/bin/libopencv_photo331.dll \
$$PWD/bin/libopencv_shape331.dll\
$$PWD/bin/libopencv_stitching331.dll \
$$PWD/bin/libopencv_superres331.dll \
$$PWD/bin/libopencv_video331.dll \
$$PWD/bin/libopencv_videoio331.dll \
$$PWD/bin/libopencv_videostab331.dll
记得一定要将bin文件的路径添加到系统环境变量中,还一定要注销或重启计算机使其生效