CodeTheRobot

C++ Intermediate · Lesson 23 of 27

Static vs shared libraries

add_library STATIC and SHARED, position-independent code, and runtime paths.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain static vs shared 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

A static library (libx.a) is copied into the executable at link time: one self-contained file, but every executable carries its own copy. A shared library (libx.so) is loaded at run time and shared between programs, so updating it fixes all of them, but it must be found when the program starts.

ROS 2 packages are shared libraries by default, because nodes and plugins load them at run time. In CMake, add_library(x STATIC ...) or SHARED forces the choice; BUILD_SHARED_LIBS flips the default. Use ldd to see which shared libraries an executable needs.

Guided lesson

Work through these steps

  1. 1.

    Read the static vs shared 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

add_library(robot_static STATIC src/robot_math.cpp)
# Build librobot_static.a, copied into executables at link time.
add_library(robot_shared SHARED src/robot_math.cpp)
# Build librobot_shared.so, loaded when the program starts.
set_target_properties(robot_shared PROPERTIES POSITION_INDEPENDENT_CODE ON)
# Shared libraries need position-independent code.
target_link_libraries(robot_app PRIVATE robot_shared)
# Link the application to the shared version.
# ldd build/robot_app
# In a shell: list the shared libraries the program needs at run time.
# LD_LIBRARY_PATH=build ./build/robot_app
# In a shell: tell the loader where to find the .so if it cannot.

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 Static vs shared libraries. Expected result: librobot_static.a and librobot_shared.so appear in build/

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  (same source file built both ways)
cmake_minimum_required(VERSION 3.16)
project(libs LANGUAGES CXX)
# TODO: build one STATIC and one SHARED library from src/robot_math.cpp
add_executable(robot_app src/main.cpp)
# TODO: link the application to the shared library
# Check it: ldd build/robot_app | grep robot_shared

Expected output

librobot_static.a and librobot_shared.so appear in build/
Stuck? Show the full solution (try it yourself first)
# CMakeLists.txt  (same source file built both ways)
cmake_minimum_required(VERSION 3.16)
project(libs LANGUAGES CXX)
add_library(robot_static STATIC src/robot_math.cpp)
add_library(robot_shared SHARED src/robot_math.cpp)
add_executable(robot_app src/main.cpp)
target_link_libraries(robot_app PRIVATE robot_shared)
# Check it: ldd build/robot_app | grep robot_shared

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

Build the same code as a static and as a shared library, link an executable to each, compare file sizes with ls -l, and run ldd on both to see the difference.

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.