This lesson is being piloted (Beta version)

Your first CMakeLists.txt file

Overview

Teaching: 10 min
Exercises: 10 min
Questions
  • How little can I get away with in my CMakeLists?

Objectives
  • Know how to write a CMakeLists.txt file

Writing a CMakeLists

The following file is fine for the following examples:

/* simple.c or simple.cpp */
#include <stdio.h>

int main() {
    printf("Hello, World!");
    return 0;
}

This file can be compiled with C or C++.

Starting off

This is the simplest possible CMakeLists:

cmake_minimum_required(VERSION 3.11)

project(MyProject)

add_executable(myexample simple.cpp)

Let’s look at the three lines:

  1. The cmake_minimum_required command sets the policies so that the build is exactly like it would be on the listed version of CMake - in other words, CMake “dumbs itself down” to the version you request for any features that could produce a different build.
  2. You need to be working on a project, and it needs at least a name. CMake assumes a CXX (that’s C++) and C mixed project if you don’t give any LANGUAGES.
  3. You need at least one library or executable to do anything interesting.

Those commands have a few extra arguments that you can give:

cmake_minimum_required(VERSION 3.11...3.15)

project(MyProject
  VERSION
    1.0
  DESCRIPTION
    "Very nice project"
  LANGUAGES
    CXX
)

add_executable(myexample simple.cpp)
  1. You can specify a range of minimum versions - this will cause the minimum version to be set to the highest supported value in that range.
  2. Projects can have versions, descriptions, and languages.

Try it out

Build and run the example code with a CMakeLists.txt similar to the one above.

git clone https://github.com/henryiii/cmake_workshop.git
cd cmake_workshop/code/00-intro

Solution

# This is required in all CMakeLists
# Selecting a nice minimum version and range
cmake_minimum_required(VERSION 3.11...3.15)

# We can call the project anything we want
# Listing the language(s) avoids the C + CXX default
project(MyExample00 LANGUAGES C)

# We need an executable target
add_executable(simple_example
  simple_example.c
)

More reading

Key Points

  • Write CMakeLists.txt files