TExcelTAD
Home › Modern CMake (with CTest & CPack) › Include Directories & Linking

Include Directories & Linking

Two commands connect targets to each other and to the outside world:

  • target_include_directories(<target> <scope> <dirs>...) adds -I paths.
  • target_link_libraries(<target> <scope> <items>...) adds link dependencies, and (this is the important part) also brings in the usage requirements of every target it links.

The <scope> keyword (PRIVATE, PUBLIC or INTERFACE) is the subject of the next lesson. In this one we use PUBLIC for a library's own headers and PRIVATE for everything else, and look at what each command produces.

A library with a public include directory

The usual layout puts a library's public headers under include/<libname>/, so users write #include <geometry/area.hpp> and the library name acts as a namespace for header files:

bash · try it yourself
mkdir -p shapes/geometry/include/geometry shapes/geometry/src && cd shapes
cat > geometry/include/geometry/area.hpp <<'SRC'
#pragma once
namespace geometry { double circle_area(double r); }
SRC
cat > geometry/src/area.cpp <<'SRC'
#include <geometry/area.hpp>
#include <numbers>
double geometry::circle_area(double r) { return std::numbers::pi * r * r; }
SRC
cat > main.cpp <<'SRC'
#include <cstdio>
#include <geometry/area.hpp>
int main() { std::printf("area(2) = %.4f\n", geometry::circle_area(2.0)); }
SRC
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Shapes LANGUAGES CXX)
add_library(geometry STATIC geometry/src/area.cpp)
target_include_directories(geometry PUBLIC geometry/include)
target_compile_features(geometry PUBLIC cxx_std_20)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE geometry)
CM
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build -v | grep -E '^\[' | sed 's/ -MD.* -o / … -o /'
./build/app
[1/4] /usr/bin/c++  -I/home/learner/shapes/geometry/include -std=gnu++20 -fno-diagnostics-color … -o CMakeFiles/geometry.dir/geometry/src/area.cpp.o -c /home/learner/shapes/geometry/src/area.cpp
[2/4] : && /opt/cmake/bin/cmake -E rm -f libgeometry.a && /usr/bin/ar qc libgeometry.a  CMakeFiles/geometry.dir/geometry/src/area.cpp.o && /usr/bin/ranlib libgeometry.a && :
[3/4] /usr/bin/c++  -I/home/learner/shapes/geometry/include -std=gnu++20 -fno-diagnostics-color … -o CMakeFiles/app.dir/main.cpp.o -c /home/learner/shapes/main.cpp
[4/4] : && /usr/bin/c++  -Wl,--dependency-file=CMakeFiles/app.dir/link.d CMakeFiles/app.dir/main.cpp.o -o app  libgeometry.a && :
area(2) = 12.5664

Read the command lines (shortened with sed):

  • area.cpp was compiled with -I/home/learner/shapes/geometry/include and -std=gnu++20, the library's own settings.
  • main.cpp got the same two flags, although app never mentioned the include directory or C++20. It inherited them by linking geometry, because both were declared PUBLIC.
  • The link line lists libgeometry.a.

Relative paths in target_include_directories are relative to the current source directory, and CMake turns them into absolute paths.

What you can link

target_link_libraries accepts several kinds of item:

Item Example Result
a target in this build geometry, Shapes::geometry its file on the link line plus its usage requirements
an imported target Threads::Threads, ZLIB::ZLIB the same, for a library found by find_package
a plain library name m, dl -lm, -ldl
a full path /opt/sdk/lib/libsdk.a that file
a linker flag -Wl,--as-needed passed through (prefer target_link_options)

Prefer targets: a target carries include paths, definitions and transitive dependencies, a plain name carries nothing.

bash · try it yourself
mkdir -p sys && cd sys
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Sys LANGUAGES CXX)
find_package(Threads REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE Threads::Threads m)
CM
cat > main.cpp <<'SRC'
#include <cmath>
#include <cstdio>
#include <thread>
int main() {
    double r = 0;
    std::thread t([&] { r = std::sqrt(2.0); });
    t.join();
    std::printf("sqrt(2) computed on a thread: %.6f\n", r);
}
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build -v | grep -o 'main.cpp.o -o app.*'
./build/app
main.cpp.o -o app  -lm && :
sqrt(2) computed on a thread: 1.414214

m became -lm. Threads::Threads added nothing on this system: since glibc 2.34 the thread functions live in libc itself, and CMake's FindThreads detected that (CMAKE_HAVE_LIBC_PTHREAD). On an older system the same line adds -pthread. That is the point of linking the target: the project states "I need threads" and CMake works out the flags.

Order and cycles

You can list dependencies in any order; CMake orders the link line so that every library comes before the libraries it depends on, as static linking requires. If two static libraries depend on each other, CMake repeats them on the link line to resolve the cycle (controlled by the LINK_INTERFACE_MULTIPLICITY property), but a cycle usually means the code wants to be one library.

Two older forms to avoid

cmake
include_directories(geometry/include)   # applies to every target in this directory and below
link_libraries(geometry)                # same, for linking
target_link_libraries(app geometry)     # no PRIVATE/PUBLIC keyword: the "plain" signature

The directory-wide commands leak settings into targets that don't need them, and the keyword-less signature can't be mixed with the keyword form on the same target. The Anti-Patterns field notes show the problems they cause.

Test yourself

app links geometry PRIVATE, and geometry declares its include dir PUBLIC. Does main.cpp (in app) get -I.../geometry/include?

  1. No, app must add it itself
  2. Yes, linking a target brings in its PUBLIC/INTERFACE usage requirements
  3. Only if app is also PUBLIC
  4. Only for static libraries

Why link Threads::Threads instead of writing -pthread yourself?

  1. It's shorter
  2. The imported target adds whatever the platform needs (nothing, -pthread or -lpthread)
  3. -pthread is deprecated
  4. Threads::Threads is header-only

What does target_link_libraries(app PRIVATE m) add to the link line when m is not a target?

  1. Nothing
  2. -lm
  3. m.a
  4. An error