Ubuntu Linux for Robotics · Lesson 25 of 96
Installing the C++17 toolchain: g++ and clang
Compiler versions, -std=c++17, warnings, clang and clang-tidy, and switching compilers.
Estimated time · 45–60 min
Learning goals
What you will understand
- Explain installing the c++17 toolchain: g++ and clang in your own words
- Use it in a small workstation example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
ROS 2 C++ code is built with C++17. g++ 7 or newer supports it fully; on Ubuntu 22.04 and later the default g++ is recent enough. Select the standard with -std=c++17 (or set CMAKE_CXX_STANDARD 17 in CMake) and always enable warnings with -Wall -Wextra -Wpedantic.
clang is an optional second compiler with different diagnostics; clang-tidy and clang-format are the linting and formatting tools. Switch compilers in CMake with -DCMAKE_CXX_COMPILER=clang++ or the CXX environment variable. Building with both finds portability bugs early.
Guided lesson
Work through these steps
- 1.
Open a terminal and record the current state before changing anything.
- 2.
Read the installing the c++17 toolchain: g++ and clang commands and predict what each one does.
- 3.
Run the commands one at a time and compare each output with your prediction.
- 4.
Write the commands that worked in a setup note you can repeat later.
Worked example
Read it before you run it
g++ --version
# Confirm a modern g++.
sudo apt install clang clang-tidy clang-format
# Install clang and its tools.
printf '#include <iostream>\nint main() { std::cout << __cplusplus << "\\n"; }\n' > std.cpp
# Write a tiny program that prints the C++ standard number.
g++ -std=c++17 -Wall -Wextra -Wpedantic std.cpp -o std && ./std
# Build and run it as C++17; prints 201703.
clang++ -std=c++17 std.cpp -o std_clang && ./std_clang
# Build with clang too.
cmake -S . -B build -DCMAKE_CXX_COMPILER=clang++
# Configure a CMake project to use clang.What the example is doing
- 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
- 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
- 3.Comments beginning with # explain safety checks and are not executed by the shell.
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 Installing the C++17 toolchain: g++ and clang. Expected result: 201703
Practice code
Now type it yourself
Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.
d=$(mktemp -d) && cd "$d"
cat > std.cpp <<'EOF'
#include <iostream>
int main() { std::cout << __cplusplus << '\n'; }
EOF
# TODO: compile std.cpp as C++17 with -Wall -Wextra -Wpedantic into ./std
./stdExpected output
201703Stuck? Show the full solution (try it yourself first)
d=$(mktemp -d) && cd "$d"
cat > std.cpp <<'EOF'
#include <iostream>
int main() { std::cout << __cplusplus << '\n'; }
EOF
g++ -std=c++17 -Wall -Wextra -Wpedantic std.cpp -o std
./stdExpected result
How you know it worked
- Each command completes without an unexplained error.
- Your notes contain the command, the important output, and what it proves.
Practical exercise
Apply the lesson
Build the same program with g++ and clang++ as C++17, introduce an unused variable and compare the warnings, and run clang-format on the file.
Before you continue
- You recorded the starting state
- Every command and output is understood
- The change can be repeated from your notes
If something goes wrong
Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.
Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.