Cmake Modern

Modern CMake

ComeOnOliver Updated 61 repo stars

File contents

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()

ComeOnOliver/skillshub/tree/main/skills/skillshub-team/catalog-batch5/cmake-modern commit 54f024d82c

Frequently asked questions

npx skillmds@latest add comeonoliver/cmake-modern