Include Directories & Linking
Two commands connect targets to each other and to the outside world:
target_include_directories(<target> <scope> <dirs>...)adds-Ipaths.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:
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.cppwas compiled with-I/home/learner/shapes/geometry/includeand-std=gnu++20, the library's own settings.main.cppgot the same two flags, althoughappnever mentioned the include directory or C++20. It inherited them by linkinggeometry, because both were declaredPUBLIC.- 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.
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/appmain.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
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" signatureThe 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?
- No, app must add it itself
- Yes, linking a target brings in its PUBLIC/INTERFACE usage requirements
- Only if app is also PUBLIC
- Only for static libraries
Why link Threads::Threads instead of writing -pthread yourself?
- It's shorter
- The imported target adds whatever the platform needs (nothing, -pthread or -lpthread)
- -pthread is deprecated
- Threads::Threads is header-only
What does target_link_libraries(app PRIVATE m) add to the link line when m is not a target?
- Nothing
-lmm.a- An error