CodeTheRobot

C++ Intermediate · Lesson 25 of 27

Using external libraries

find_package, imported targets, FetchContent and pkg-config.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain using external libraries 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

find_package(Name REQUIRED) locates an installed library and gives you an imported target such as Threads::Threads or Eigen3::Eigen. Link to that target and CMake adds the right include folders and flags. ROS 2 packages are found the same way: find_package(rclcpp REQUIRED).

When a library is not installed, FetchContent downloads and builds it inside your project, pinned to a version or commit for reproducibility. Prefer the system package (apt) for stable libraries and pin versions for anything you fetch.

Guided lesson

Work through these steps

  1. 1.

    Read the using external libraries example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

find_package(Threads REQUIRED)
# Find the platform's thread library; stop with a clear error if it is missing.
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
# Find Eigen (linear algebra) version 3.3 or newer; install with sudo apt install libeigen3-dev.
add_executable(pose_app src/pose.cpp)
# An executable that uses both libraries.
target_link_libraries(pose_app PRIVATE Threads::Threads Eigen3::Eigen)
# Link the imported targets; include paths come along automatically.
include(FetchContent)
# Enable the FetchContent module.
FetchContent_Declare(catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.5.2)
# Describe a dependency to download, pinned to an exact release tag.
FetchContent_MakeAvailable(catch2)
# Download and add it to this build.

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 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.

sourcecompilerprogramBUILD AND RUN

Demo for Using external libraries. Expected result: Built target pose_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  (needs: sudo apt install libeigen3-dev)
cmake_minimum_required(VERSION 3.16)
project(pose LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
# TODO: find Threads and Eigen3 (REQUIRED, NO_MODULE for Eigen)
add_executable(pose_app src/pose.cpp)
# TODO: link pose_app PRIVATE to the imported targets Threads::Threads and Eigen3::Eigen

Expected output

Built target pose_app
Stuck? Show the full solution (try it yourself first)
# CMakeLists.txt  (needs: sudo apt install libeigen3-dev)
cmake_minimum_required(VERSION 3.16)
project(pose LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
find_package(Threads REQUIRED)
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
add_executable(pose_app src/pose.cpp)
target_link_libraries(pose_app PRIVATE Threads::Threads Eigen3::Eigen)

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

Install libeigen3-dev, use find_package and Eigen3::Eigen to multiply two 3×3 matrices in a small program, and build it with CMake. Then remove the package and read the CMake error message.

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

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.