CMake
is a group of tools that allow to build, test, and package applications. Just like Qt, it is available on all major development platforms. It is also supported by various IDE's, including Qt Creator.In this section we will show the most basic way to use Qt in a CMake project. We will walk through an example project that is utilizing Qt Widgets.
Build a C++ GUI executable
A
CMake
project is defined by files written in the CMake language. The main file is called CMakeLists.txt
, and is usually placed in the same directory as the actual program sources.Here is a typical
CMakeLists.txt
file for an application written in C++ and using Qt and Qt Widgets:Let's go through the content.
cmake_minimum_required()
specifies the minimum CMake version that the application requires. Qt itself requires at least CMake version 3.16.Ideally the new maintainer(s) will already be experienced with Qt and CMake/C. Lisandro confirmed their plan change ahead of Qt 6.0 and also relayed the news to the Qt development list in hopes of finding new and qualified individuals to take over in ensuring great Qt6 packaging for Debian. The next major Qt version, Qt6, will be using CMake internally as its build system, so the CMake integration with Qt will likely get tighter and more versatile in the long-term.
project()
sets a project name and the default project version. The LANGUAGES
argument tells CMake that the program is written in C++.Qt 6 requires a compiler supporting C++ version 17 or newer. Enforcing this by setting the
CMAKE_CXX_STANDARD
, CMAKE_CXX_STANDARD_REQUIRED
variables will let CMake print an error if the compiler is too old.Qt applications typically make use of the Meta-Object Compiler (moc), Resource Compiler (rcc), and User Interface Compiler (uic) that come with Qt. Setting the
CMAKE_AUTOMOC
, CMAKE_AUTORCC
, and CMAKE_AUTOUIC
variables to ON
will let CMake automatically set up rules so that the respective compilers are called transparently, when required.Qt Cmake Qml
This tells CMake to look up Qt 6, and import the
Widgets
module. There is no point in continuing if CMake
cannot locate the module, so we do set the REQUIRED
flag to let CMake abort in this case.If successful, the module will set some CMake variables documented in Module variables. It furthermore imports the
Qt6::Widgets
target that we use below.For
find_package
to be successful, CMake
must find the Qt installation. There are different ways you can tell CMake
about Qt:- Set the
CMAKE_PREFIX_PATH
environment variable to include the Qt 6 installation prefix. - Set the CMake cache variable
CMAKE_PREFIX_PATH
to include the Qt 6 installation prefix.
Note that Qt Creator will handle this transparently for you.
add_executable()
tells CMake that we want to build an executable (so not a library) called helloworld
as a target. The target should be built from C++ code (mainwindow.cpp
, main.cpp
), a Qt Designer file (mainwindow.ui
), and a Qt Resource System file (resources.qrc
).Note that you typically do not list header files here. This is different from qmake, where header files need to be explicitly listed so that they are processed by the Meta-Object Compiler (moc).
Qt6 Cmake Windows
Finally,
target_link_libraries
tells CMake that the helloworld
executable makes use of Qt Widgets by referencing the Qt6::Widgets
target imported by the find_package()
call above. This will not only add the right arguments to the linker, but also makes sure that the right include directories, compiler definitions are passed to the C++ compiler. The PRIVATE
keyword is not strictly necessary for an executable target, but it is good practice to specify it. If helloworld
was a library rather than an executable, then either PRIVATE
or PUBLIC
should be specified (PUBLIC
if the library mentions anything from Qt6::Widgets
in its headers, PRIVATE
otherwise).Further reading
The official CMake Documentation is an invaluable source for working with CMake.
The book Professional CMake: A Practical Guide provides a great introduction to the most relevant CMake features.
© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Qt6::Core
Compiles big binary resources into object code |
Creates an RCC file from a list of Qt resource files |
Creates and finalizes an application target of a platform-specific type |
Compiles binary resources into source code |
Defines a build target that runs androiddeployqt to produce an APK |
Configures the target binary's name to include an architecture-specific suffix |
Generates the deployment settings file needed by androiddeployqt |
Handles various common platform-specific tasks associated with Qt targets |
Calls moc on an input file |
Specifies a custom set of plugins to import for a static Qt build |
Creates .moc files from sources |
Qt Cmake Ts
Qt6::DBus
Generates an adaptor class for a D-Bus interface |
Generates C++ sources implementing an interface for a D-Bus interface description file |
Generates C++ sources implementing interfaces for D-Bus interface description files |
Generates a D-Bus interface from a header file |
Qt6::LinguistTools
Compiles Qt Linguist .ts files into .qm files |
Sets up the Qt Linguist translation toolchain |
Qt6::RemoteObjects
Qt6::Widgets
Qt6 Build
Qt6::Scxml
© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.