Post

CMake

CMake

CMake is an open-source, cross-platform tool designed to manage the build process of software projects. Unlike traditional makefiles, CMake allows developers to create platform-independent build configurations that can generate native build files for various systems, such as Make, Ninja, Visual Studio, and more.
If you’re new to build systems, you might wonder why CMake is worth learning. Here are some of its standout features:

  • Portability: Write your build scripts once, and CMake takes care of adapting them to different platforms and compilers.
  • Flexibility: Support for complex build scenarios, custom build targets, and integration with other tools.
  • Ease of Use: With a simple syntax and powerful commands, you can configure builds for projects of any size.
    In this post, I’ll share what I’ve learned about CMake—starting with the basics of setting up a project, writing a CMakeLists.txt file, and understanding how to use CMake commands effectively.

Introduction

Let’s dive in and demystify CMake together! Before diving into CMake, let’s briefly talk about build systems.

When you write code in C or C++, the compiler (like gcc or clang) needs to know:

  • Which files to compile
  • What flags to use
  • How to link libraries
  • Where to put the output

Manually writing all this for each file and configuration is inefficient. That’s where build systems come in.

Some popular build systems:

  • Make: One of the oldest and most common tools (Makefile)
  • Ninja: A small, fast build system focused on performance
  • CMake: A meta build system — it generates build files for tools like Make or Ninja

CMake doesn’t build your code directly. Instead, it generates build scripts that can be used by make, ninja, or other IDE.

Installation

CMake is available in most Linux distributions via package managers.
To install CMake on Ubuntu or Debian-based systems:

1
2
sudo apt update
sudo apt install 

To verify the installation:

1
cmake --version

You should see output like:

1
cmake version 3.25.1

If you need a newer version than what your package manager provides, you can:

  1. Download from the official site: https://cmake.org/download
  2. Build from source
  3. Use Snap or pip for alternate installs

Since CMake can generate build files for multiple tools, you may want to install Ninja and Make.

Install Ninja:

1
sudo apt install ninja-build

Check version:

1
ninja --version

Install Make:

1
sudo apt install build-essential

This will also install compilers like gcc and tools required to compile C/C++ code.

Here’s a minimal CMake project layout:

1
2
3
my_project/
├── CMakeLists.txt
└── main.cpp

Example main.cpp:

1
2
3
4
5
6
#include <iostream>

int main() {
    std::cout << "Hello, CMake!" << std::endl;
    return 0;
}

Basic CMakeLists.txt:

1
2
3
4
cmake_minimum_required(VERSION 3.10)
project(MyProject)

add_executable(main main.cpp)

🧪 Build with CMake and Make

Navigate to your project directory and build it:

1
2
3
4
mkdir build
cd build
cmake ..
make

The make command will compile your code using the Makefiles generated by CMake.

To run:

1
./main

🎯 Coming Up Next

Now that you’ve installed CMake and seen how it works at a basic level, in the next post we’ll look into:

  • How CMakeLists.txt works
  • Using variables and conditions
  • Linking libraries
  • Generating builds with Ninja or Makefile

Stay tuned and happy building! 🛠️

This post is licensed under CC BY 4.0 by the author.