Mastering CMake explains how to use the CMake suite of tools, including CTest and CPack, to develop, build, test, and package software for distribution. It covers use of the command-line and GUI tools on Linux (UNIX), Microsoft Windows, and Mac OS X. This book also contains a guide for converting projects to CMake and writing CMake code to specify build rules to compile sources, create static and shared libraries, link executables, run custom commands, run tests, and install artifacts. It also includes a copy of key portions of the official reference documentation.
There is a really good book called "Mastering CMake". You should read it if you need to write a complex system. This book is related to the version 3.1 (when this post was written, the latest version is 3.15 but the most of the concepts are still valid).
Mastering Cmake Book
If you run cmake --help, you will see all the possible parameters to provide to CMake. Don't worry: we'll learn how to use them in the future. First, I want to show you something that is really interesting: cmake has a set of commands that are totally cross-platform and can be used independently of the operatingsystem where you are running CMake. To get all these commands, just run cmake -E:
Use the Builder property and target.CMakeBuilder object to configure how CMake builds generated code.tc.Builder = target.create('CMakeBuilder');tc.Builder.Generator = 'Ninja';tc.Builder.ToolchainFile = fullfile(pwd, 'ExampleCMakeToolchain.cmake');tc.Builder.SupportedBuildTypes(end+1) = ... target.create('CMakeBuildType', ... 'Name', 'FastMath', ... 'GeneratesDebugSymbols', false, ... 'DebugBuildType', 'FastMathWithDebug');tc.Builder.SupportedBuildTypes(end+1) = ... target.create('CMakeBuildType', ... 'Name', 'FastMathWithDebug', ... 'GeneratesDebugSymbols', true);
Using an editor, create ExampleCMakeToolchain.cmake, a file that contains these lines.# Example CMake toolchain# Configure to use GCCif(WIN32) list(APPEND CMAKE_PROGRAM_PATH "$ENVMW_MINGW64_LOC")endif()set(CMAKE_C_COMPILER "gcc")set(CMAKE_CXX_COMPILER "g++")# Add some custom build configurations: FastMath, and FastMathWithDebugset(CMAKE_C_FLAGS_FASTMATH "-Ofast")set(CMAKE_CXX_FLAGS_FASTMATH "-Ofast")set(CMAKE_C_FLAGS_FASTMATHWITHDEBUG "-Ofast -g")set(CMAKE_CXX_FLAGS_FASTMATHWITHDEBUG "-Ofast -g")For information about CMake toolchain files, see -cmake/chapter/Cross%20Compiling%20With%20CMake.html.
CMake (which includes the executable tools cmake, ctest and cpack with each CMake installation) continues to evolve and improve fairly rapidly from release to release.Kitware (the organization responsible for CMake development) makes three major releases each year (or about every four months).Best practices for using CMake evolve as new versions are released.For example, at the time of this writing, CMake support for Generator Expressions is rapidly evolving and can have a dramatic impact on the best way to write CMakeList.txt files.)The bssw.io editorial team has created this article informed by 13+ years of CMake usage and hundreds of hours of searching for and reading CMake documentation, especially in the years 2021-2022.
Arguably, the best resource for finding up-to-date guidance on how to use CMake is the most recent edition of the book Professional CMake (the 11th edition covering CMake versions up to 3.22 as of this writing).The book contains excellent tutorial material for CMake and yet goes into deep concepts in the CMake language where needed (such as the gotchas and workarounds for dealing with lists and function/macro arguments with data containing quotes ", spaces and semi-colons ;, a frequently misunderstood aspect of CMake).This book covers every basic CMake feature in great detail.It explains modern CMake idioms and best practices.Also, it covers older CMake commands and idioms to avoid and explains why.This book addresses the "why" and big picture questions about CMake in a way that no other material has matched.It also provides really critical low-level details and tips that are absent in even the official CMake reference documentation.This book is self published by the author in a downloadable PDF format with new editions coming out after each new CMake release (i.e. three new editions each year).The current edition of the book costs $30 which includes 9 updates for new editions of the book as they come out.(Therefore, $30 would cover every new edition over a 3 year period which includes 9 new CMake releases or 6 years if only downloading every other updated edition across 18 CMake releases.)The author is a significant contributor to CMake and collaborates closely with Kitware staff.
As a developer contributing to a project using CMake (i.e. editing the project's CMakeLists.txt files), when consulting the official reference documentation, it is advisable to view documentation for only the minimum version of CMake supported by the project (i.e. for the version X.Y listed in the project's top-level CMakeLists.txt file calling cmake_minimum_required(VERSION X.Y)).For example, if minimum version is 3.17, then one would select that version in the drop-down box (taking you to ). This helps to avoid accidentally using CMake features available only in newer versions.
If one does not want to spend the $30 for the book "Professional CMake", then one may consider Kitware's Mastering CMake book.This is an updated, free online version of Kitware's classic "Mastering CMake" paperback book that used to be sold (last updated for CMake 3.1 published in 2015).It also contains tutorial coverage of key CMake concepts and other basic tutorial content (but usually at a less complete and detailed level compared to "Professional CMake").Note that the "guides" provided in the online CMake Reference Documentation are also precisely duplicated in the online "Mastering CMake" book.
This book is great for developers who are new to Qt and Qt Creator, and are interested in harnessing the power of Qt for cross-platform development. If you have basic experience programming in C++, you have what it takes to create engaging cross-platform applications using Qt and Qt Creator!
2ff7e9595c
Comments