CMake : A Quick Primer

Introduction

CMake is a build system generator that allows you to create build scripts (like Makefiles, Visual Studio projects, etc.) in a platform-independent way. It helps manage the build process for C/C++ projects.

Key Concepts

  • CMakeLists.txt: The main configuration file for CMake.
  • cmake command: Used to generate build files.
  • make command: Used to compile the project (on Unix-like systems).

Code Sample

In this chapter, and beyond, we will be actively working with CMake. Grab your favourite text editor, and C++ compiler and get ready to code.

  1. Create a file main.cpp:
#include <iostream>
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
  1. Create a CMakeLists.txt file in the same directory:
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)

add_executable(HelloWorld main.cpp)
  1. Build and run the project:
mkdir build
cd build
cmake ..
cmake --build .
./HelloWorld
  1. Output:
Hello, World!

Quiz

What is the purpose of cmake_minimum_required?

The purpose of `cmake_minimum_required` is to specify the minimum CMake version required to compile. If the current version is lower, it will throw an error.

What does add_executable do?

`add_executable` specifies the target executable name and associated source files.

Why do we create a build directory?

We create a `build` directory to separate the source from the build files.