This page looks best with JavaScript enabled

Cmake Guide

 ·  ☕ 2 min read

Cmake Guide

Why Do We Need Cmake?

A build system to help manage dependencies, how to compile the project and other
items are the job of a project build system. However, the C programming language
is designed to work on nearly any hardware. So, the job of setting up the custom
project dependencies and compiling options has usually been on a per project
basis.

Thanks to cmake we can have sanity amidst the chaos! Here are the basic features
that cmake helps with:

  • Multiple build project setup. The main idea behind cmake is to have a cross
    platform make which is a Linux build
    tool that uses makefile’s to manage projects.
    Cmake works on many nearly every operating systems.
  • Cross compiling. Need to build code on your machine designed for a different
    OS or even bare metal hardware? This is easy with cmake, you just need to
    specify a toolchain file.
    As an embedded systems engineer, building code for another system is a daily
    task and cmake makes it possible. In fact the popular microcontrol series
    ESP32 by Espressif
    uses cmake as their build system.

Getting Started Videos

I have found an awesome YouTube video series by vector-of-bool
that has been the most helpful resource for getting started.

Very useful blog post by Dane Bulat

Install Latest Version Linux

Go to Cmake website downloads page

Setup Release And Debug Builds

It is very easy to setup different releases. This stackoverflow
page makes it clear.

CMakeLists.txt Setup

Subdirectory CMakeLists.txt Setup

I found this StackOverflow reference to be the most helpful.
Just create a folder, put source files into it and then add a CMakeLists.txt file
there as well as the root. Make sure to have a line in the root file add_subdirectory("src")

Main Commands

  • To setup project with output going to a directory named build: cmake -S . -B ./build
  • To build project without knowing what build syestem is used: cmake --build ./build where build is directory