Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære CMake Workflow Overview | Getting Started with CMake
Introduction to CMake

bookCMake Workflow Overview

When you first set up a C project with CMake, your directory is simple usually just your source files and a CMakeLists.txt file.

# my_project/
# ├── CMakeLists.txt
# └── main.c

After you run CMake, a new build directory appears. This directory contains all the files CMake generates, such as CMakeCache.txt, the CMakeFiles directory, and platform-specific build files (like a Makefile). Once you build the project, you will also see the compiled executable inside the build directory.

# my_project/
# ├── CMakeLists.txt
# ├── main.c
# └── build/
#    ├── CMakeCache.txt
#    ├── CMakeFiles/
#    ├── Makefile
#    ├── cmake_install.cmake
#    └── main   (compiled executable)
Note
Note

Keeping all build artifacts in a separate directory helps you keep your source tree clean and organized.

Each step in the workflow is represented in the CMakeLists.txt file. The cmake_minimum_required and project commands are part of the configuration step, where CMake checks your setup and prepares to generate build files.

# Step 1: Minimum CMake version and project name
cmake_minimum_required(VERSION 3.10)   # (Configure step)
project(MyProject C)                   # (Configure step)

# Step 2: Add the executable target
add_executable(main main.c)            # (Generate step)

# Step 3: Build happens after generation by running your build tool (e.g., make)
# (No direct CMake command in this file for the build step)

The add_executable command is processed during the generation step, where CMake creates the necessary files for your chosen build system. The actual build step—compiling your code into an executable—is triggered after generation, using the build system files CMake produced.

Step 1: Write a CMakeLists.txt file
expand arrow

Create a CMakeLists.txt file in your project directory. This file will define how CMake should configure and build your project.

Step 2: Configure the Project with CMake
expand arrow

Run CMake to configure your project. During this step, CMake checks your setup and prepares for build file generation based on your CMakeLists.txt.

Step 3: Generate Build Files
expand arrow

CMake generates build files for your platform, such as a Makefile for Unix systems or a Visual Studio solution for Windows.

Step 4: Build the Project
expand arrow

Use your platform's build tool (for example, make) to build the project and produce the executable from the generated build files.

This sequence ensures your build environment is always up to date and matches your project's requirements.

Compared to other build systems, CMake stands out because it generates native build files for a wide range of platforms and IDEs from the same CMakeLists.txt. Unlike simple Makefile-based systems, CMake can target Unix Makefiles, Ninja, Visual Studio, and more, letting you build your project on nearly any system without rewriting your build scripts. Its flexibility and portability make it a popular choice for modern C projects.

question mark

What is the correct order of steps in the typical CMake workflow?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain the difference between the configure, generate, and build steps in more detail?

What are some common mistakes beginners make when setting up a CMake project?

How do I add more source files or libraries to my CMake project?

bookCMake Workflow Overview

Stryg for at vise menuen

When you first set up a C project with CMake, your directory is simple usually just your source files and a CMakeLists.txt file.

# my_project/
# ├── CMakeLists.txt
# └── main.c

After you run CMake, a new build directory appears. This directory contains all the files CMake generates, such as CMakeCache.txt, the CMakeFiles directory, and platform-specific build files (like a Makefile). Once you build the project, you will also see the compiled executable inside the build directory.

# my_project/
# ├── CMakeLists.txt
# ├── main.c
# └── build/
#    ├── CMakeCache.txt
#    ├── CMakeFiles/
#    ├── Makefile
#    ├── cmake_install.cmake
#    └── main   (compiled executable)
Note
Note

Keeping all build artifacts in a separate directory helps you keep your source tree clean and organized.

Each step in the workflow is represented in the CMakeLists.txt file. The cmake_minimum_required and project commands are part of the configuration step, where CMake checks your setup and prepares to generate build files.

# Step 1: Minimum CMake version and project name
cmake_minimum_required(VERSION 3.10)   # (Configure step)
project(MyProject C)                   # (Configure step)

# Step 2: Add the executable target
add_executable(main main.c)            # (Generate step)

# Step 3: Build happens after generation by running your build tool (e.g., make)
# (No direct CMake command in this file for the build step)

The add_executable command is processed during the generation step, where CMake creates the necessary files for your chosen build system. The actual build step—compiling your code into an executable—is triggered after generation, using the build system files CMake produced.

Step 1: Write a CMakeLists.txt file
expand arrow

Create a CMakeLists.txt file in your project directory. This file will define how CMake should configure and build your project.

Step 2: Configure the Project with CMake
expand arrow

Run CMake to configure your project. During this step, CMake checks your setup and prepares for build file generation based on your CMakeLists.txt.

Step 3: Generate Build Files
expand arrow

CMake generates build files for your platform, such as a Makefile for Unix systems or a Visual Studio solution for Windows.

Step 4: Build the Project
expand arrow

Use your platform's build tool (for example, make) to build the project and produce the executable from the generated build files.

This sequence ensures your build environment is always up to date and matches your project's requirements.

Compared to other build systems, CMake stands out because it generates native build files for a wide range of platforms and IDEs from the same CMakeLists.txt. Unlike simple Makefile-based systems, CMake can target Unix Makefiles, Ninja, Visual Studio, and more, letting you build your project on nearly any system without rewriting your build scripts. Its flexibility and portability make it a popular choice for modern C projects.

question mark

What is the correct order of steps in the typical CMake workflow?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
some-alt