QT基础——qt6.2.4的cmake 流程
创建一个cmake工程
cmake_minimum_required(VERSION 3.16)
project(untitled VERSION 0.1 LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)
qt_add_executable(appuntitled
main.cpp
)
qt_add_qml_module(appuntitled
URI untitled
VERSION 1.0
QML_FILES main.qml
)
set_target_properties(appuntitled 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}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_compile_definitions(appuntitled
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(appuntitled
PRIVATE Qt6::Quick)
qt_add_executable
和qt_add_qml_module
是qt定义的cmake函数,在qt安装目录能找到:
cmake 命令参考 :https://doc-snapshots.qt.io/qt6-dev/cmake-command-reference.html
qt creator实际执行的构建命令:
修整一下:
cmake -Bbuild -GNinja "-DCMAKE_BUILD_TYPE:STRING=Debug" "-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=C:/Qt/Tools/QtCreator/share/qtcreator/package-manager/auto-setup.cmake" "-DCMAKE_PREFIX_PATH:PATH=C:/Qt/6.2.4/mingw_64"
手动敲命令行
找个终端,执行之后就能得到cmake生成的文件
然后执行
ninja -C build
或者执行
cmake --build ./build --target all
都可以编译得到可执行文件
比较重要的是这两条
"-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=C:/Qt/Tools/QtCreator/share/qtcreator/package-manager/auto-setup.cmake"
"-DCMAKE_PREFIX_PATH:PATH=C:/Qt/6.2.4/mingw_64
CMAKE_PROJECT_INCLUDE_BEFORE
这个是用来管理第三方的依赖库的,参考 https://www.qt.io/blog/qt-creator-cmake-package-manager-auto-setup
如果只是一个hello world工程,没有依赖其他任何库,那么这一行也可以省掉,只需要:
就可以编译成功
CMAKE_PREFIX_PATH
是设置find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)
的搜索路径,find_package会搜索CMAKE_PREFIX_PATH
目录及其bin, lib, include
子目录,找到Qt6Config.cmake
然后就能链接Qt6 的Quick组件了
参考: https://cmake.org/cmake/help/latest/command/find_package.html
ninja执行的操作
其实也就是moc、rcc、uic等这些操作 https://www.cnblogs.com/feipeng8848/p/14492801.html
bye