一、常用模块
1. 构建完成后自动调用windeployqt
打包
shell
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND "${QT_PATH}/bin/windeployqt.exe" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.exe"
)
- 需要将
QT_PATH
设为Qt的安装目录。 - 还可以用
--dir
指定打包时dll存放的目录。 - 打包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"
)