Hi~ This is Weekly Qt, dedicated to sharing valuable Qt knowledge.This will be a series of articles aimed at smoothly introducing everyone to building Qt projects using CMake.qmake is a very important tool in Qt, with a long history and a good reputation. Compared to Makefile, it is very easy to get started, allowing developers to focus more on code creation. Over the years, C++ build tools have also developed significantly,and CMake has become the mainstream build tool for C++ projects, with many open-source projects using it.Qt has maintained its leading position because it continues to evolve,so starting from Qt6, Qt’s build tool has evolved to CMake, which can also help us integrate other C/C++ third-party libraries more easily, improving development efficiency.01 OverviewQt6 has also developed many convenient function encapsulations based on CMake, completing calls like MOC/UIC or automatically packaging and deploying exe files, helping to efficiently build Qt projects.This series of articles will guide you through using CMake functions in Qt6, covering the following usages:
- Building console projects with QCoreApplication
- Building window applications with QWidgets
- Managing resource files with QWidgets
- Creating static/dynamic libraries with QWidgets
- Building multi-directory projects with QWidgets
- Automatic internationalization translation with QWidgets
- Building Qt Quick applications
- Custom QML modules for Qt Quick
- Internationalization translation for Qt Quick
- Packaging and deploying QWidgets / Qt Quick
02 Building a Qt Command Line Program with CMake
Getting started with CMake begins with the simplest Qt application. We don’t necessarily have to learn all the syntax of CMake from scratch; we can start by imitating and solving problems as we encounter them, like clearing all the squares in Minesweeper, establishing a smoother learning curve.
The author is using the latest LTS Qt6.8, running example code on Windows 11.
Next, let’s look at the structure of a command line program:
CMakeLists.txt
main.cpp
There is only one cpp file, and the project is created by Qt Creator (hereafter referred to as QTC), which helps us directly use project templates. The project templates created by different versions of QTC may vary slightly. Let’s take a look at the CMakeLists.txt file:
# Configure the minimum required CMake version
# Minimum version requirements refer to https://doc-snapshots.qt.io/qt6-6.8/cmake-supported-cmake-versions.html
cmake_minimum_required(VERSION 3.16)
# Set Project Name and Default Project Version. This parameter tells CMake that this program is written in C++
project(BuildConsoleApp LANGUAGES CXX)
# Qt6 requires support for C++ version 17 or higher compilers.
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# CMake finds Qt6 and imports modules. REQUIRED means essential; without these modules, the build cannot proceed.
# If the corresponding modules are found, some variables may also be set. Module-related variables can be found here: https://doc-snapshots.qt.io/qt6-6.8/cmake-variable-reference.html#module-variables
# In fact, this CMakeLists.txt is not directly run by cmake.exe on Windows, but by a qt-cmake.bat, for example: C:\Qt\6.5.7\mingw_64\bin\qt-cmake.bat
# In qt-cmake.bat, a toolchain.cmake is set, code:
set toolchain_path=%script_dir_path%\../lib/cmake/Qt6\qt.toolchain.cmake, which specifies the location of the compiler
# If you want to use cmake.exe directly for building, you need to set the CMAKE_PREFIX_PATH variable, written in the command line like this:
-DCMAKE_PREFIX_PATH=$HOME/Qt/6.8.2/gcc_64
# Another very important function qt_standard_project_setup, encapsulated internally, configures the necessary content for the qt standard project, such as: CMAKE_AUTOMOC, etc.
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
# Add executable program
add_executable(BuildConsoleApp main.cpp)
# Add libraries to link with the executable program, automatically including the corresponding module header files
target_link_libraries(BuildConsoleApp Qt${QT_VERSION_MAJOR}::Core)
include(GNUInstallDirs)
install(TARGETS BuildConsoleApp LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
The summary steps are as follows:
- qt-cmake.bat will run this CMakeLists.txt file, preparing some things for Qt
- project configures the project name, which will be displayed in the QTC project view
- find_package introduces Qt modules, similar to qmake’s QT+=
<span>add_executable</span><span> adds an executable target while specifying the name of the exe</span><span>target_link_libraries</span><span> adds linked libraries</span>
main.cpp is quite simple:
#include <QCoreApplication>
int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);
return a.exec();
}
Running it will start a process.
References
[1]https://doc.qt.io/qt-6.8/en/cmake-manual.html
[1]https://doc.qt.io/qt-6.8/en/qmake-manual.html
Welcome to submit articles to [email protected], no formatting requirements. Your best practices deserve to be seen!
Related Reading
Comprehensive Guide to Qt Debugging Tool QDebug
Qt and More Programming Languages Integration?
New TreeModel Added to QML