C++ Intermediate · Lesson 22 of 27
Targets, linking and include directories
Libraries as targets, target_link_libraries and PUBLIC vs PRIVATE.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain targets, linking and include directories in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
Modern CMake is target-based. A target (library or executable) carries its own sources, include directories, compile options and dependencies. target_link_libraries(app PRIVATE robot_math) both links and inherits the library's public include folders.
PUBLIC means the library and everything that links to it need the setting; PRIVATE means only the library itself; INTERFACE means only the users. Put settings on targets, never in global variables, so that every package builds the same way on every machine.
Guided lesson
Work through these steps
- 1.
Read the targets, linking and include directories example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
add_library(robot_math src/robot_math.cpp)
# Define a library target from its source file.
target_include_directories(robot_math PUBLIC include)
# Give the library, and everything that links it, the include/ folder.
target_compile_features(robot_math PUBLIC cxx_std_17)
# Require C++17 for the library and its users.
target_compile_options(robot_math PRIVATE -Wall -Wextra -Wpedantic)
# Warnings for the library's own code only.
add_executable(robot_app src/main.cpp)
# The application target.
target_link_libraries(robot_app PRIVATE robot_math)
# Link the library and inherit its PUBLIC include folder and features.What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
Watch it run · Build and run
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Targets, linking and include directories. Expected result: Built target robot_math Built target robot_app
Practice code
Now type it yourself
Copy the starter into the file named in its first line and replace every TODO. Then configure and build: cmake -S . -B build && cmake --build build
# CMakeLists.txt (src/robot_math.cpp, include/robot_math.hpp, src/main.cpp)
cmake_minimum_required(VERSION 3.16)
project(robot_suite LANGUAGES CXX)
# TODO: create the robot_math library and give it PUBLIC include/ and cxx_std_17
add_executable(robot_app src/main.cpp)
# TODO: link robot_app PRIVATE to robot_mathExpected output
Built target robot_math
Built target robot_appStuck? Show the full solution (try it yourself first)
# CMakeLists.txt (src/robot_math.cpp, include/robot_math.hpp, src/main.cpp)
cmake_minimum_required(VERSION 3.16)
project(robot_suite LANGUAGES CXX)
add_library(robot_math src/robot_math.cpp)
target_include_directories(robot_math PUBLIC include)
target_compile_features(robot_math PUBLIC cxx_std_17)
add_executable(robot_app src/main.cpp)
target_link_libraries(robot_app PRIVATE robot_math)Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Split robot_math into a library with a public header and a small executable. Put the C++ standard and include path on the library target, and show that main.cpp finds the header without an -I flag of its own.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.