版本
Qt 6.6.0 win11 x86_64
安装 CMake
网址: https://cmake.org/download/
选择对应的版本下载
将 cmake.exe 所在的 bin 目录添加到环境变量中
安装 openCV
网址: https://opencv.org/releases/
选择对应的版本下载 Source资源包
- 解压 “opencv-4.9.0.zip”, 将其解压的 “opencv-4.9.0” 存放到指定路径 “Path” (例如在我的电脑上 “Path” 则为 “I:\opencv-4.9.0”)
- 在解压的文件夹下新建一个 build 目录
-
打开 /cmake/bin/cmake-gui.exe
在红线处入 “Path”
选择将其编译在 build 目录下
- 点击 configure
出现配置对话框->在“Specify the generator for this project”内选择“MinGW Makefiles”选项,
在下边的编译器类型中,选择:
“Specify native compilers”,单击“Next”->在编译器选择器选择界面,在C后面选择C编译路径(我的为D:\qt\qt6\Tools\mingw1120_64\bin\gcc.exe"),同理选择C++编译器路径,点击 Finshed, 点击 configure
- 待配置完成后,点击 Add Entry (若下面的列表中已经有 QT_MAKE_EXECUTABLE这一项则直接双击修改即可)
Name: 处填入 QT_MAKE_EXECUTABLE
Type: 选择 PATH
Value: 填入 Qt 的 qmake路径 (我的为 “D:\qt\qt6\6.4.3\mingw_64\bin\qmake.exe”)
Description: 空
- 在下面的列表中搜索 WITH_QT WITH_OPENGL,将其勾选;搜索 WITH_IPP,将其取消勾选
- 设置 Qt5Concurrent_DIR, Qt5Core_DIR, Qt5Gui_DIR, Qt5Test_DIR,
Qt5Widgets_DIR, Qt5OpenGL_DIR, CMAKE_BUILD_TYPE (我的是Qt6)
使用 m i n g w 编译时 使用 mingw 编译时 使用mingw编译时,不要勾选 BUILD_opencv_world ,否则很可能会编译失败
- 点击 configure
-
进入 “Path”\下的 build 目录, 打开一个 CMakeDownloadLog的文本文件。(我的电脑上是 “I:\opencv-4.9.0\build\CMakeDownloadLog.txt”)
例如:#do_copy "opencv_videoio_ffmpeg.dll" "9b755ecbbade0a5b78332e9b4ef2dd1b" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fbac408a47977ee4265f39e7659d33f1dfef5216/ffmpeg/opencv_videoio_ffmpeg.dll" "C:/openCV/3rdparty/ffmpeg" #missing "C:/openCV/3rdparty/ffmpeg/opencv_videoio_ffmpeg.dll" #check_md5 "H:/opencv-4.9.0/opencv-4.9.0/.cache/ffmpeg/9b755ecbbade0a5b78332e9b4ef2dd1b-opencv_videoio_ffmpeg.dll" #mismatch_md5 "H:/opencv-4.9.0/opencv-4.9.0/.cache/ffmpeg/9b755ecbbade0a5b78332e9b4ef2dd1b-opencv_videoio_ffmpeg.dll" "d41d8cd98f00b204e9800998ecf8427e" #delete "H:/opencv-4.9.0/opencv-4.9.0/.cache/ffmpeg/9b755ecbbade0a5b78332e9b4ef2dd1b-opencv_videoio_ffmpeg.dll" #cmake_download "H:/opencv-4.9.0/opencv-4.9.0/.cache/ffmpeg/9b755ecbbade0a5b78332e9b4ef2dd1b-opencv_videoio_ffmpeg.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fbac408a47977ee4265f39e7659d33f1dfef5216/ffmpeg/opencv_videoio_ffmpeg.dll" #try 1 # timeout on name lookup is not supported # getaddrinfo(3) failed for raw.githubusercontent.com:443 # Could not resolve host: raw.githubusercontent.com # Closing connection 0 #
里面提示编译时因网络下载,缺失部分文件
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/fbac408a47977ee4265f39e7659d33f1dfef5216/ffmpeg/opencv_videoio_ffmpeg.dll" "C:/openCV/3rdparty/ffmpeg"
第一行带有 raw 的网址则是我们需要下载的文件,将其复制并在浏览器中打开#missing
这一行表明了缺失文件应该存储的地方,将下载的文件 复制粘贴到这即可
- 点击generate
- build 目录下,打开终端,执行 mingw32-make,便开始编译
- 编译无错后,在终端中输入 mingw32-make.exe install 会打包编译后的文件,执行完成后, build 目录下会有一个生成一个 install 目录,包含了编译好的 OpenCV
- 添加环境变量
将 install 目录的 x64架构 的 mingw 版本下的 bin 和 lib 添加到环境变量中
Qt6 下使用 OpenCV
创建 CMake 构建的 Qt6 项目
添加如下语句
所跟的路径如下,请根据自己电脑设定set(OpenCV_DIR "I:/opencv-4.9.0/build/install/x64/mingw/lib")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${OpenCV_LIBS})
{PROJECT_NAME} 是项目的名字
例如 project(untitled VERSION 0.1 LANGUAGES CXX)
这里的 {PROJECT_NAME} 就是 untitled
也可根据 CMake Modules 添加的 opencv模块下的 OpenCVConfig.cmke 对 主目录的 CMakeLists.txt 更该
效果展示
文章来源:https://www.toymoban.com/news/detail-820573.html
我的 主目录 CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(untitled VERSION 0.1 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH "I:/opencv-4.9.0/build/install/x64/mingw/bin")
set(OpenCV_DIR "I:/opencv-4.9.0/build/install/x64/mingw/lib")
set(CMAKE_PREFIX_PATH "D:/qt/qt6/6.4.3/mingw_64/lib/cmake")
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(OpenCV REQUIRED)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(untitled
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET untitled APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(untitled SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(untitled
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(untitled PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(untitled PRIVATE ${OpenCV_LIBS})
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.untitled)
endif()
set_target_properties(untitled PROPERTIES
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
include(GNUInstallDirs)
install(TARGETS untitled
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(untitled)
endif()
注意事项
使用MSVC编译时可以选择 BUILD_opencv_world 选项,但是使用 mingw64 编译时,选择 BUILD_opencv_world 总会失败,因此在使用mingw64的时候取消该选项。文章来源地址https://www.toymoban.com/news/detail-820573.html
参考
- https://juejin.cn/post/7171322133040070686
- https://www.cnblogs.com/Alliswell-WP/p/OpenCV_Qt_CMake.html
- https://www.weizhiyong.com/archives/1904
到了这里,关于使用 Qt6-mingw 编译 OpenCV 源码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!