Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 8 of 10

CMake Basics - Modern Build System

CMake is the de-facto build system for C++ projects. It generates platform-specific build files (Makefile on Linux, .vcxproj on Windows, Ninja files). CMake 3.x (modern CMake) uses **targets** as the central abstraction - every library and executable is a target with its own compile flags, include paths, and linked libraries.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

CMakeLists.txt - Modern Target-Based CMake

CMakeLists.txt - Modern Target-Based CMake
# ─── CMakeLists.txt (project root) ─────────────────────────────
cmake_minimum_required(VERSION 3.16)
project(MyCppApp VERSION 1.0.0 LANGUAGES CXX)

# Set C++17 globally (modern way: target-level is preferred)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)   # No GNU extensions: strict ISO C++

# Export compile commands for clangd/IDE intellisense
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ─── Source files ────────────────────────────────────────────
set(SOURCES
    src/main.cpp
    src/math_utils.cpp
    src/string_utils.cpp
)

# ─── Create executable target ────────────────────────────────
add_executable(myapp ${SOURCES})

# ─── Target-level settings (preferred over global) ───────────
target_compile_features(myapp PRIVATE cxx_std_17)

target_include_directories(myapp PRIVATE
    ${PROJECT_SOURCE_DIR}/include
)

target_compile_options(myapp PRIVATE
    -Wall
    -Wextra
    -Wpedantic
    $<$<CONFIG:Debug>:-g -O0 -fsanitize=address,undefined>
    $<$<CONFIG:Release>:-O2 -DNDEBUG>
)

target_link_options(myapp PRIVATE
    $<$<CONFIG:Debug>:-fsanitize=address,undefined>
)

# ─── Library target ──────────────────────────────────────────
add_library(mathlib STATIC
    src/math_utils.cpp
)

target_include_directories(mathlib PUBLIC
    ${PROJECT_SOURCE_DIR}/include
)
# PUBLIC: myapp automatically gets mathlib's include dirs
target_link_libraries(myapp PRIVATE mathlib)

# ─── Project structure ────────────────────────────────────────
# MyCppApp/
# ├── CMakeLists.txt
# ├── include/
# │   ├── math_utils.h
# │   └── string_utils.h
# └── src/
#     ├── main.cpp
#     ├── math_utils.cpp
#     └── string_utils.cpp

# ─── Build commands ──────────────────────────────────────────
# mkdir build && cd build
# cmake .. -DCMAKE_BUILD_TYPE=Debug    (configure)
# cmake --build .                       (build)
# cmake --build . --target clean        (clean)
#
# Release:
# cmake .. -DCMAKE_BUILD_TYPE=Release
# cmake --build . -j$(nproc)            (parallel)
#
# Run ASAN build:
# cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . && ./myapp

Common Mistakes

  • `include_directories()` is the old global way - always prefer `target_include_directories()` with PRIVATE/PUBLIC/INTERFACE. Global commands affect all targets in the directory, which causes polluted include paths in large projects.
  • Not setting `CMAKE_CXX_EXTENSIONS OFF` - by default CMake allows GNU extensions (like VLAs). Disabling ensures your code is portable standard C++.
  • Building in the source directory (`cmake .`) - always build in a separate directory (`cmake -B build`). In-source builds mix generated files with source files, making `git status` a mess and clean rebuilds hard.

Tip

Tip

Practice CMake Basics Modern Build System in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Cross-platform target configuration, dependency management, and build generation

Practice Task

Note

Practice Task - (1) Write a working example of CMake Basics Modern Build System from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with CMake Basics Modern Build System is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Key Takeaways

  • CMake is the de-facto build system for C++ projects.
  • `include_directories()` is the old global way - always prefer `target_include_directories()` with PRIVATE/PUBLIC/INTERFACE. Global commands affect all targets in the directory, which causes polluted include paths in large projects.
  • Not setting `CMAKE_CXX_EXTENSIONS OFF` - by default CMake allows GNU extensions (like VLAs). Disabling ensures your code is portable standard C++.
  • Building in the source directory (`cmake .`) - always build in a separate directory (`cmake -B build`). In-source builds mix generated files with source files, making `git status` a mess and clean rebuilds hard.

Topics in This Module

0/10 done