C++ 23 cheatsheet
C++ 23: CMake
7 common C++ 23 tasks.
A minimal CMakeLists.txt
A project with C++23 and one executable target.
cmake_minimum_required(VERSION 3.20)
project(app LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(app main.cpp util.cpp)
Configure a build
Generate the build system in a separate build folder. Release mode turns on optimization.
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
Run the tests
Run the tests registered with add_test and print output for failures.
ctest --test-dir build --output-on-failure
Export compile commands
Write compile_commands.json, which editors and clang-tidy use.
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
Link a library
Link a target against another target or an installed package.
find_package(fmt REQUIRED)
target_link_libraries(app PRIVATE fmt::fmt)
Download a dependency
Fetch and build a dependency such as GoogleTest at configure time.
include(FetchContent)
FetchContent_Declare(googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.15.2.zip)
FetchContent_MakeAvailable(googletest)