Modern C++ • C++20 baseline
Project Builds with CMake
Describe targets and language requirements in a reproducible build configuration.
A build system records how a project is compiled and linked. CMake targets are preferable to global compiler flags because each target can declare its own sources, include paths, and standard requirements. Keep generated build output outside the source tree.
Key points
- Declare the C++ standard requirement on the target.
- Use target-scoped include directories and compile options.
Example
cmake_minimum_required(VERSION 3.20)
project(hello_cpp LANGUAGES CXX)
add_executable(hello main.cpp)
target_compile_features(hello PRIVATE cxx_std_20)
What to expect
Configure with cmake -S . -B build, then build with cmake --build build.
Practice exercise
Add a library target and link it to the executable with target_link_libraries.
Key idea: Build configuration is part of the project’s documented interface and should be tested in CI.