How to build with CMake

Key takeaways:

  • CMake simplifies building complex C++ projects across platforms.

  • Ensure CMake is installed before starting.

  • CMakeLists.txt configures project settings, C++ standard, and source files.

  • Create a separate build/ directory to keep build files separate.

  • Use cmake .. to generate platform-specific build files and compile with make.

  • Run the built executable from the terminal.

CMake is a powerful cross-platformSupported on multiple platforms with mostly the same usage. build systemA file or structure that is used to create a build or executable for a given set of source files. that simplifies managing and building complex projects. We’ll explore the fundamental steps to build a project using CMake.

Setting up the basics

Before diving into the build process, ensure CMake is installed on your system. You can install it using your package manager or download it from the official CMake website.

Directory structure

Organize your project with a clear directory structure. This will make things simpler when creating your CMakeLists.txt file later. Consider a project directory with the following components:

project-root/
├── include/
│ └── some-header.h
├── src/
│ └── main.cpp
└── CMakeLists.txt
The include directory includes code being imported into the source files (the `src` directory)

The include/ directory contains the header files, while the src/ directory holds the source code, such as main.cpp. A well-organized structure like this makes it easier to manage files as your project scales. It also helps in quickly locating resources, improving maintainability. With this structure, users should feel more confident handling even complex builds, and future collaborations or extensions to the project will be smoother.

Configuring CMakeLists.txt

Now, let’s create a simple CMakeLists.txt file in the project root to configure the build:

cmake_minimum_required(VERSION 3.15)
project(MyProject)
# Set C++ standard
set(CMAKE_CXX_STANDARD 11)
# Include directories
include_directories(include)
# Add source files
add_executable(output src/main.cpp)

In the CMakeLists.txt file:

  • Line 1: Specifies the CMake version needed.

  • Line 2: Names your project (here it’s MyProject).

  • Line 5: Sets the C++ standard to 17.

  • Line 8: Tells the compiler where to find header files.

  • Line 11: Defines the executable and its source file.

By configuring this correctly, you ensure CMake can efficiently manage your project’s build process.

Configuring and generating build system files

Create a build directory to keep build artifactsFiles generated during the build process. separate from source files:

mkdir build
cd build

Run CMake from within the build directory to generate platform-specific build system files:

cmake ..

Building the project

Now, use the generated build system (e.g., Makefile) to compile the project:

On Unix-based systems:

make

On Windows:

cmake --build . --config Release

Execution

Now that the project has been built, simply run the generated executable in the terminal:

./output

Example code

Let’s consider a simple example using the directory structure and configuration above. We can test the commands we’ve covered, such as setting up the CMakeLists.txt, configuring the build system, and building the project.

Here’s how the flow works:

  1. Create the directory structure.

  2. Write the example C++ code in main.cpp.

  3. Configure the build using CMake.

  4. Build the project using the appropriate commands for your system.

This example will be a Unix-based system. Click “Run” and execute the above commands in the widget below:

cmake_minimum_required(VERSION 3.15)
project(MyProject)

# Set C++ standard
set(CMAKE_CXX_STANDARD 11)

# Include directories
include_directories(include)

# Add source files
add_executable(output src/main.cpp)
Use the commands mentioned above in the terminal to build and execute this project

Conclusion

Building a project with CMake involves creating a CMakeLists.txt file, configuring it appropriately, and then using CMake to generate build system files. This separation allows for platform-independent builds and easy maintenance of complex projects.

The provided example demonstrates a minimal project setup, but CMake is highly versatile, accommodating various project structures and configurations. As projects grow in complexity, additional CMake features can be leveraged to handle dependencies, testing, and more.

CMake provides a robust and flexible solution for building software projects, empowering developers to manage complex build processes with ease.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can we build projects using CMake?

Create a CMakeLists.txt file to configure your project and organize your files. Then, create a separate build/ directory, run cmake .. from the build/ directory to generate build files, and compile the project using a build command (e.g., make).


Is CMake only for C++?

No, CMake supports multiple programming languages, including C, C++, Fortran, and others.


Does CMake need a compiler?

Yes, CMake generates build files for a specific compiler, so a compatible compiler must be installed on your system.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved