目录如下:
CMakeLists.txt内容如下:
#最低CMake版本 cmake_minimum_required(VERSION 3.5) #项目名 set(PROJECT_NAME QtCmakeVsOpenCV) project(${PROJECT_NAME}) #set(CMAKE_BUILD_TYPE Debug) set(CMAKE_BUILD_TYPE Release) #set模块 set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) #exe生成路径,CMAKE_CURRENT_SOURCE_DIR指CMakeLists.txt所在的路径 #set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/build/) #C++版本 set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) #查找依赖的Qt文件 find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED) #查找OpenCV库 find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) #生成exe所需源文件,注意将所有代码文件都放在src文件夹中 aux_source_directory(src DIR_SRCS) add_executable(${PROJECT_NAME} ${DIR_SRCS}) #链接lib库生成exe,注意此句放到最后 target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets ${OpenCV_LIBS})