Lesson 29 · Reference
Cheat sheet
Every command from this course on one page. Configure, build, targets, dependencies, install, test, package. Copy-paste style.
The whole course, reduced to commands. When you forget the exact syntax, start here.
Configure and build
cmake -S . -B build # configure (out-of-source)
cmake --build build # build everything
cmake --build build --target app # build one target
cmake --build build -v # verbose, show real commands
cmake --install build # install
cmake --install build --prefix /tmp/stage # install to a folder
rm -rf build # clean rebuild
CMakeLists.txt skeleton
cmake_minimum_required(VERSION 4.4)
project(myproj VERSION 1.0.0 LANGUAGES CXX)
add_executable(app src/main.cpp)
Targets
add_library(mylib STATIC src/mylib.cpp) # static library
add_library(mylib SHARED src/mylib.cpp) # shared library
add_library(headerlib INTERFACE) # header-only library
target_include_directories(mylib PUBLIC include) # headers. Consumers inherit.
target_include_directories(mylib PRIVATE src) # private headers
target_link_libraries(app PRIVATE mylib) # link and inherit PUBLIC requirements
target_compile_options(app PRIVATE -Wall) # flags (compiler-specific)
target_compile_features(app PRIVATE cxx_std_20) # C++ standard
target_compile_definitions(app PRIVATE NDEBUG) # -D definitions
Scope keywords. PRIVATE only this target. PUBLIC this target and its consumers. INTERFACE consumers only. (A consumer is any target that uses the library.)
Dependencies
find_package(Threads REQUIRED) # module mode
find_package(fmt 9.0 CONFIG REQUIRED) # config mode
target_link_libraries(app PRIVATE Threads::Threads fmt::fmt)
include(FetchContent)
FetchContent_Declare(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e # hash, always
)
FetchContent_MakeAvailable(googletest)
cmake -S . -B build -DCMAKE_PREFIX_PATH=/opt/fmt # where to look for packages
Options and cache
option(MYPROJ_BUILD_TESTS "Build tests" ON) # ON/OFF switch
if(MYPROJ_BUILD_TESTS) add_subdirectory(tests) endif()
cmake -S . -B build -DMYPROJ_BUILD_TESTS=OFF # override on the command line
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release # build type (single-config)
cmake --build build --config Release # build type (multi-config)
Presets
cmake --list-presets
cmake --preset dev
cmake --build --preset dev
ctest --preset dev
cmake --workflow --preset dev # configure + build + test
Install and package
include(GNUInstallDirs)
install(TARGETS mylib
EXPORT mylibTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(FILES include/mylib/mylib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mylib)
install(EXPORT mylibTargets FILE mylibConfig.cmake NAMESPACE mylib::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mylib)
include(CPack) # plus cmake --build build --target package
Testing
enable_testing()
add_test(NAME test_mylib COMMAND test_mylib)
include(CTest) # BUILD_TESTING switch, top level
ctest --test-dir build
ctest --test-dir build --output-on-failure
ctest --test-dir build -R test_mylib # filter
Debugging
message(STATUS "value = ${VAR}") # print during configure
message(FATAL_ERROR "helpful hint here")
cmake --trace-expand -S . -B build # trace every command
ninja -C build -t commands app # exact commands for a target
The most common fixes
| Symptom | Fix |
|---|---|
find_package cannot find X | -DCMAKE_PREFIX_PATH=<prefix> (lesson 17) |
| Header not found | include dir needs PUBLIC (lesson 9) |
| Policy warning | bump cmake_minimum_required (lesson 6) |
| Weird behavior after editing CMakeLists | rm -rf build (lesson 4) |
CMake 4.x errors on VERSION 3.2 | policy version 3.5 or newer required (lesson 6) |
| Flags applied to third-party code | global to per-target (lesson 7) |
Read the docs
Every command above links to its full docs from its lesson. Start at lesson 1, or use the glossary.