> Modern CMake

Build C/C++ projects with modern CMake 3.20+ best practices. Targets, FetchContent, presets.

fetch
$curl "https://skillshub.wtf/skillshub-team/catalog-batch5/cmake-modern?format=md"
SKILL.mdModern CMake

Modern CMake

Minimal Project

cmake_minimum_required(VERSION 3.20)
project(MyApp VERSION 1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_library(mylib src/lib.cpp)
target_include_directories(mylib PUBLIC include)

add_executable(myapp src/main.cpp)
target_link_libraries(myapp PRIVATE mylib)

FetchContent Dependencies

include(FetchContent)
FetchContent_Declare(fmt GIT_REPOSITORY https://github.com/fmtlib/fmt.git GIT_TAG 10.2.1)
FetchContent_MakeAvailable(fmt)
target_link_libraries(myapp PRIVATE fmt::fmt)

Build

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --parallel
ctest --test-dir build

Presets (CMakePresets.json) for reproducible dev/release builds

Best: Use target_* commands, not global include_directories()

┌ stats

installs/wk0
░░░░░░░░░░
first seenMar 18, 2026
└────────────

┌ tags

└────────────