Introduction

  • CMake is a tool for controlling the build process, which creates scripts to turn C++ source code into a specific project.
  • Examples:
    • Generates vcxproj files for Microsoft Visual Studio 2017, toolchain, and MSVC 19
    • Generates project for MinGW

If you want to know how to setup a complete C++ project, go to Cpp Dev Tools Setup.

Benefits & Drawbacks

Benefits

Using CMAKE, devloppers can have :
– Cross-platform capabilities: CMake supports various operating systems, allowing developers to build their projects on Windows, macOS, and Linux.
– Out-of-source builds: It maintains a clean source directory by enabling builds in separate directories.
– Multiple build systems: It can generate build files for different build systems like Makefiles, Ninja, Visual Studio, and Xcode.
– IDE independence: Developers can use their preferred IDEs or editors; CMake is not tied to any specific development environment.
– Large community support: As a widely-used tool, CMake benefits from an extensive community providing documentation, tutorials, and forums for support.
– Modularization: CMake allows for creating modular projects using CMakeLists.txt files in various directories.
– Customizable builds: Users can create custom build types and have control over compilation flags.
– Dependency resolution: CMake automatically handles dependencies between libraries and executables within a project.
– Simplified variable management: It provides a straightforward way of defining and utilizing variables to manage build processes.
– Testing support: It includes CTest, a testing tool that helps with running tests and reporting results.
– CPack integration: CMake comes with CPack, which helps to create binary and source packages for easy distribution of the software.
– Find modules: CMake modules can automatically locate libraries, executables, and other required software on the system.

Drawbacks

  • Learning curve: New users may find CMake’s scripting language and concepts challenging to grasp, requiring a period of learning and adaptation.
  • Complexity: As projects grow, CMake scripts can become intricate and difficult to manage without a clear organization or modular approach.
  • Documentation: Though extensive, CMake’s documentation can be overwhelming or unclear, making it hard for beginners to find relevant information.
  • Boilerplate code: Setting up a new CMake project typically requires writing boilerplate code, which can be viewed as tedious.
  • Overhead: For small or simple projects, the power and features of CMake may introduce unnecessary overhead.
  • Error messages: CMake’s error reporting can be cryptic and often does not point directly to the source of the issue, complicating troubleshooting.
  • Version compatibility: Differences between CMake versions can lead to compatibility issues, especially when using newer CMake features.
  • Redundancy: Frequent need to re-run the configuration and generation steps upon changes to the build system or project dependencies.
  • GUI limitations: The cmake-gui tool might be less intuitive than expected and does not expose all the functionalities present in the command line interface.
  • Layer of abstraction: CMake abstracts away the native build systems, sometimes making it difficult to perform tasks that are straightforward in the native system.
  • Regular updates to the CMakeLists.txt file are required when:
    • Adding header files (.h or .hpp).
    • Including source files (.cpp).
    • Integrating static libraries (.lib).
    • Connecting external libraries in general.

Installation

To get started with CMake:

  • Download CMake from this page, choosing Windows x64 Installer for Windows.
  • Ensure the “Add CMake to PATH for all users” option is selected.

Usage (GUI)

  • Launch cmake-gui.exe.
  • Enter the source code directory
  • Designate where to place binary files; typically, create a build or cmake-build folder
  • Click Configure for initial project setup.
  • Pick a generator from CMake-GUI’s dropdown.
    • Generators include:
      • Command-line build tools.
      • IDE build tools.
      • Extra generators.
  • For generator target platforms, Win32 is standard; x64 is also an option.
  • Retain default settings and click Finish.

💡 Generators are a key part of CMake-GUI’s configuration to create build scripts. If configuration is incomplete, “Generate” remains inactive and the interface permits CMakeLists.txt edits. Variables in red are newly added or changed, not necessarily errors.

  • Update CMake script variables like CMAKE_CONFIGURATION_TYPE and library paths as needed.
  • After alterations in the red section, hit Configure again, possibly leading to more variables to fill in.
  • When satisfied, click Generate.

For CMake integration through the command line in different IDEs:

Written by

Albert Oplog

Hi, I'm Albert Oplog. I would humbly like to share my tech journey with people all around the world.