C++ Beginner · Lesson 01 of 22
Compilers & the build process
GCC, Clang and MSVC; what happens between source code and a running program.
Estimated time · 45–75 min
Learning goals
What you will understand
- Explain compilers & the build process 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
C++ source code is not run directly. A compiler translates it to machine code. GCC (g++) and Clang (clang++) are the usual compilers on Ubuntu and are what ROS 2 uses; MSVC is Microsoft's compiler on Windows. They accept the same language, but their warnings and error messages differ, so it helps to try both.
This course uses C++17. Always tell the compiler the standard with -std=c++17 and turn warnings on with -Wall -Wextra -Wpedantic. Warnings are early feedback: fix them from the first build.
Guided lesson
Work through these steps
- 1.
Read the compilers & the build process 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
g++ --version
# Print the GCC version; C++17 needs GCC 7 or newer.
clang++ --version
# Print the Clang version (optional; install with sudo apt install clang).
g++ -std=c++17 -Wall -Wextra -Wpedantic main.cpp -o robot_app
# Compile main.cpp as C++17 with extra warnings; -o names the executable robot_app.
clang++ -std=c++17 -Wall -Wextra main.cpp -o robot_app_clang
# Build the same file with Clang and compare its messages with GCC's.
./robot_app
# Run the program you built from the current folder (./ means 'this folder').
echo $?
# Print the exit code of the last command; 0 means the program reported success.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 Compilers & the build process. Expected result: standard: 201703
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <iostream>
int main() {
// TODO: print "standard: " followed by __cplusplus (201703 means C++17)
return 0;
}Expected output
standard: 201703Stuck? Show the full solution (try it yourself first)
#include <iostream>
int main() {
std::cout << "standard: " << __cplusplus << '\n';
return 0;
}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
Write a main.cpp that prints one line. Build it with g++ and with clang++ (if installed) using -std=c++17 -Wall -Wextra -Wpedantic. Add an unused variable, read the warning from each compiler, then remove it.
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.