cmake Qt模板

一、常用模块

1. 构建完成后自动调用windeployqt打包

shell 复制代码
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND "${QT_PATH}/bin/windeployqt.exe" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.exe"
)
  1. 需要将QT_PATH设为Qt的安装目录。
  2. 还可以用--dir指定打包时dll存放的目录。
  3. 打包QML项目需要用--qmldir指定.qml源文件路径。

2. 构建完成后自动拷贝第三方库

以curl为例:

shell 复制代码
find_package(curl REQUIRED MODULE)
target_link_libraries(${PROJECT_NAME} PUBLIC
		...
        curl
)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:curl> $<TARGET_FILE_DIR:${PROJECT_NAME}>
)

二、模板

1. Windows下CMake Qt Widgets项目

这里采用了FILE GLOB自动添加所有cpp、h,这样有利有弊,也可以直接把文件写在add_executable(),看个人喜好。

目录结构是这样的:

shell 复制代码
.
├── 3rdparty
│   ├── libcurl-vc-x64-release-dll-ipv6-sspi-schannel
│   │
│   └── libcurl-vc-x86-release-dll-ipv6-sspi-schannel
│          
├── include
├── src
└── ui
shell 复制代码
cmake_minimum_required(VERSION 3.20)
project(FreeFTPClient LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

## 添加一些第三方库
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/3rdparty/libcurl-vc-x64-release-dll-ipv6-sspi-schannel")
## 设置Qt库安装路径
set(QT_PATH "E:/Qt/6.5.3/msvc2019_64")
set (CMAKE_PREFIX_PATH ${QT_PATH})
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets StateMachine)
find_package(curl REQUIRED MODULE)

FILE(GLOB INCLUDE "include/*.h" "include/*.hpp")
FILE(GLOB UI "ui/*.ui")
FILE(GLOB SRC "src/*.cpp")

set(CMAKE_AUTOUIC_SEARCH_PATHS "./ui")
set(PROJECT_SOURCES ${INCLUDE} ${UI} ${SRC})

add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE FALSE)
target_link_libraries(${PROJECT_NAME} PUBLIC
        Qt6::Core
        Qt6::Gui
        Qt6::Widgets
        Qt6::StateMachine
        curl
)

target_include_directories(${PROJECT_NAME} PRIVATE "./include")

## 自动调用windeployqt找齐运行所需的dll
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:curl> $<TARGET_FILE_DIR:${PROJECT_NAME}>
        COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/windeployqt"
        COMMAND "${QT_PATH}/bin/windeployqt.exe" --dir "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.exe"
)

2. windows QML项目

shell 复制代码
cmake_minimum_required(VERSION 3.20)
project(FreeFTPClient LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(QT_PATH "E:/Qt/6.5.3/msvc2019_64")
set(CMAKE_PREFIX_PATH ${QT_PATH})
find_package(Qt6 REQUIRED COMPONENTS
        Core
        Gui
        Widgets
        StateMachine
        Network
        Qml
        Quick
        QuickControls2
)

add_executable(${PROJECT_NAME}
        App.cpp
        qml.qrc
)
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE FALSE)
target_link_libraries(${PROJECT_NAME} PUBLIC
        Qt6::Core
        Qt6::Gui
        Qt6::Widgets
        Qt6::StateMachine
        Qt6::Network
        Qt6::Qml
        Qt6::Quick
        Qt6::QuickControls2
)

target_include_directories(${PROJECT_NAME} PRIVATE "./include")

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/windeployqt"
        COMMAND "${QT_PATH}/bin/windeployqt.exe" --qmldir "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.exe"
)
相关推荐
小爬虫程序猿1 分钟前
Java爬虫:在1688上“照片快递”上传图片
java·开发语言·爬虫
Json_181790144802 分钟前
小红书笔记详情API接口系列(概述到示例案例)
开发语言·python
热爱生活热爱你23 分钟前
Qt5 读写共享内存,已验证,支持汉字的正确写入和读取
开发语言·qt
老菜鸟的每一天28 分钟前
Qt Modbus 2 通信实现
qt·modbus·rtu
TheoBrien1 小时前
JUC(java.util.concurrent) 的常⻅类
java·开发语言
淡淡蛋痛1 小时前
D3.js 入门指南
开发语言·javascript·信息可视化
wrx繁星点点1 小时前
创建型模式-建造者模式:构建复杂对象的优雅解决方案
java·开发语言·数据结构·数据库·spring·maven·建造者模式
王大锤43911 小时前
golang的多表联合orm
开发语言·后端·golang
好看资源平台2 小时前
快速上手 Rust——实用示例
开发语言·后端·rust
许野平2 小时前
Rust:文档注释 //! 和 ///
开发语言·后端·rust·注释·文档注释