C++ - CMake (Advanced Patterns)

Overview

Estimated time: 60–80 minutes

Scale CMake with interface targets, install/export configs, and dependency fetching. Integrate with vcpkg/Conan.

Learning Objectives

  • Use INTERFACE libraries for header-only targets.
  • Export targets and write a package config.
  • FetchContent to pull dependencies at configure time.

Prerequisites

Interface library

add_library(util INTERFACE)
target_include_directories(util INTERFACE include)

FetchContent

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

Common Pitfalls

  • Mixing global and target-specific settings; prefer target properties.