Compréhension de « CMakeLists »
CMake
In our introductory tutorial, we used CMake as the build system for our application, but we only really paid close attention to one of our CMakeLists.txt
files. Here, we're going to go over how it works in a bit more detail.
« CMake » est utile car il nous permet d'automatiser la plupart des choses devant être faites avant la compilation.
CMakeLists.txt
You might remember this CMakeLists.txt
file from the first tutorial:
|
|
The first line, cmake_minimum_required(VERSION 3.16)
sets the version of CMake we will be calling.
Après cela, « projet(helloworld) » définit le nom du projet.
Then we get to a section where we include a number of necessary CMake and KDE settings by using extra-cmake-modules. You shouldn't worry too much about these lines for now and we won't need to change them in this tutorial.
The following section is important, because it specifies which dependencies we'll be bringing in at compile time. Let's look at the first:
|
|
- find_package() finds and loads the external component.
- The first word is the framework, Qt.
${QT_MAJOR_VERSION}
is a convenience variable provided by extra-cmake-modules that lets us choose the Qt version to be used, 5 or 6, depending on whether we use the CMake flag-DBUILD_WITH_QT6=ON
. REQUIRED
tells CMake that these dependencies are indeed required and that it shall exit with an error if the package can not be found.NO_MODULE
switches CMake into the Config mode. We don't need to worry about that at the moment.- « COMPONENTS » est un paramètre précédant les composants spécifiques de l'environnement de développement que nous allons inclure.
- Chaque mot après « COMPONENTS » fait référence à un composant spécifique.
Note
If you are looking to add any components listed in the KDE API documentation to your application, you may check the right sidebar for how to add the component with CMake. For instance, for
Kirigami2
, you will find something like find_package(KF5Kirigami2)
, which with the addition of extra-cmake-modules becomes something like find_package(KF5 COMPONENTS Kirigami2)
.
Pay close attention to your included components, as omitting ones used in our code will stop our application from compiling.
The final line lets CMake print out which packages it has found. And above that, add_subdirectory(src)
points CMake into the src/
directory, where it finds another CMakeLists.txt
file:
|
|
Comme le gros du travail est fait dans le premier fichier, celui-ci est beaucoup plus court.
- add_executable() takes care of generating our executable.
- target_sources() lets us add files that will be used by our new executable.
- target_link_libraries() dynamically links the libraries used in our code to our executable. Kirigami is not included here because we are using only its QML module.
- install() puts our executable in its right place by using
${KDE_INSTALL_TARGETS_DEFAULT_ARGS}
, a convenience variable provided by KDEInstallDirs in extra-cmake-modules that installs executables and libraries in their right place for you without needing to specify the absolute path, similarly to GNUInstallDirs.
Note
Note that these libraries should match the components that we included in our previousCMakeLists.txt
file - otherwise these components will not be included and our application won't compile.Cette configuration sera utile lors du développement de la plupart des applications avec Kirigami.