Qt6-CMake模板项目模板
Posted 鱼酱2333
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt6-CMake模板项目模板相关的知识,希望对你有一定的参考价值。
本文内容:
构建CMake+Qt6 控制台应用程序模板。
构建CMake+Qt6 窗口应用程序模板。
构建的项目截图
API
# 添加子项目
add_subdirectory ("XXXX")
# 获取当前文件夹名称
string(REGEX REPLACE ".*/(.*)" "\\\\1" CURRENT_FOLDER $CMAKE_CURRENT_SOURCE_DIR )
# 设置当前文件夹名称
set(TARGET $CURRENT_FOLDER)
#设置项目名称
project($TARGET LANGUAGES CXX)
#设置包含文件
file(GLOB SRC_CODE
"*.h"
"*.cpp"
"*.hpp"
)
//设置输出目录
set(EXECUTABLE_OUTPUT_PATH $PROJECT_SOURCE_DIR/../bin)
CMake控制台子项目模板
#cmake最低版本
cmake_minimum_required(VERSION 3.14)
#项目名称
string(REGEX REPLACE ".*/(.*)" "\\\\1" CURRENT_FOLDER $CMAKE_CURRENT_SOURCE_DIR )
set(TARGET $CURRENT_FOLDER)
project($TARGET LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#C++11标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#Qt依赖包
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt$QT_VERSION_MAJOR COMPONENTS Core REQUIRED)
#自动构建源文件
file(GLOB SRC_CODE
"*.h"
"*.cpp"
"*.hpp"
)
# 链接到程序中
add_executable($PROJECT_NAME $SRC_CODE)
# 链接Qt库
target_link_libraries($PROJECT_NAME Qt$QT_VERSION_MAJOR::Core)
# 设置输出目录
set(EXECUTABLE_OUTPUT_PATH $PROJECT_SOURCE_DIR/../bin)
CMake应用程序模板
cmake_minimum_required(VERSION 3.5)
#设置文件名
string(REGEX REPLACE ".*/(.*)" "\\\\1" CURRENT_FOLDER $CMAKE_CURRENT_SOURCE_DIR )
#设置 项目名
set(PROJECT_NAME $CURRENT_FOLDER)
#设置项目名
project($PROJECT_NAME VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#开启UI文件编译、MOC功能、RCC功能
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
#C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#输出目录
set(EXECUTABLE_OUTPUT_PATH $PROJECT_SOURCE_DIR/../bin)
#找到 链接库
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt$QT_VERSION_MAJOR COMPONENTS Widgets REQUIRED)
#自动添加文件
file(GLOB PROJECT_SOURCES
"*.h"
"*.cpp"
"*.hpp"
"*.ui"
"*.qrc"
"*/*.h"
"*/*.cpp"
"*/*.hpp"
"*/*.ui"
"*/*.qrc"
)
if($QT_VERSION_MAJOR GREATER_EQUAL 6)
qt_add_executable($PROJECT_NAME
MANUAL_FINALIZATION
$PROJECT_SOURCES
)
# Define target properties for android with Qt 6 as:
# set_property(TARGET $PROJECT_NAME 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($PROJECT_NAME 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($PROJECT_NAME
$PROJECT_SOURCES
)
endif()
endif()
target_link_libraries($PROJECT_NAME PRIVATE Qt$QT_VERSION_MAJOR::Widgets)
set_target_properties($PROJECT_NAME PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION $PROJECT_VERSION
MACOSX_BUNDLE_SHORT_VERSION_STRING $PROJECT_VERSION_MAJOR.$PROJECT_VERSION_MINOR
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable($PROJECT_NAME)
endif()
CMake总项目模板
#项目名
project ("coder")
# 包含子项目。
add_subdirectory ("examples")
add_subdirectory ("robber")
以上是关于Qt6-CMake模板项目模板的主要内容,如果未能解决你的问题,请参考以下文章