CMake + QT 环境配置

1.系统环境

系统:win10
QT:5.15.2
IDE: Visual Studio2022

2. CMakeList.txt配置

cmake_minimum_required(VERSION 3.12)
project(demo)

#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

################################## 配置QT5环境 #################################
# 自动调用moc,uic,rcc处理qt的扩展部分
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
# 设置Qt5的cmake模块所在目录,如果不设置将使用系统提供的版本
set(CMAKE_PREFIX_PATH C:/Qt/5.15.2/msvc2019_64/lib/cmake)
find_package(Qt5 COMPONENTS Core Widgets Gui SerialPort REQUIRED)
set(QT5_LIBS Qt5::Core Qt5::Widgets Qt5::Gui Qt5::SerialPort)

################################## 第三方库头文件配置 #################################
# --- OpenCV --- #
set(CV_INCLUDE_DIR "D:/library/opencv/build/include")
################################## 第三方库.lib文件路径 #################################
set(CV_LIB_DIR "D:/library/opencv/build/x64/vc15/lib")
################################### 第三方库.lib文件名 ##################################
IF(${CMAKE_BUILD_TYPE} STREQUAL "Release")
    set(CV_LIB opencv_world430)
ELSEIF(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    set(CV_LIB opencv_world430d)
ENDIF()
###################################### 链接第三方库 #####################################

# --- 配置opencv --- #
include_directories(${CV_INCLUDE_DIR})
include_directories(${CV_INCLUDE_DIR}/opencv2)
link_directories(${CV_LIB_DIR})
link_libraries(${CV_LIB})


# ---------------------------------------- 添加资源文件 ---------------------------------------- #
AUX_SOURCE_DIRECTORY(./src ROOT_SOURCES)
SET(RC_SOURCES src/images/images.qrc)

SET(PROJECT_SOURCES ${ROOT_SOURCES} ${RC_SOURCES})

# src/MainWindow.qrc必须单独写进去,不然图片会失效
IF(${CMAKE_BUILD_TYPE} STREQUAL "Release")
   add_executable(${PROJECT_NAME} WIN32 ${PROJECT_SOURCES})
    #让程序以管理员模式运行
   set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\"")
ELSEIF(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} )
ENDIF()

#链接静态库
target_link_libraries(${PROJECT_NAME} ${QT5_LIBS} ${CV_LIB})

#指定二进制文件位置
IF(${CMAKE_BUILD_TYPE} STREQUAL "Release")
    SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/release)
ELSEIF(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/debug)
ENDIF()

你可能感兴趣的:(C++,CMake,qt,opencv,c++)