Course outline

C++ / Basic / Lesson 1

Build and understand a C++20 program

Follow source text through compilation and linking, then distinguish a reproducible result from a diagnostic or an unsupported feature.

About 4 min readContent reviewed
On this page

Lesson goals

  • Compile a complete translation unit using an explicit C++20 mode.
  • Explain headers, main, expressions, standard output, and process status.
  • Classify compilation, linking, runtime, and logic failures.

From source file to executable

C++ is a compiled language with a separate standard library. A compiler translates a translation unit, which includes a source file and the headers brought into it. A linker combines object files and resolves references to functions and library facilities. The executable then runs as a process. An editor can highlight code without compiling it, so a green editor indicator is not evidence that your executable was rebuilt. Keep the source, build command, and executable name visible while learning.

A small complete program

Save this example as main.cpp. The iostream header declares stream facilities; std:: qualifies names in the standard namespace. main is the program entry point in a hosted implementation. Inserting a string into std::cout writes characters to standard output, and the escaped newline ends the line. Returning zero reports successful completion to the environment. The printed line and the exit status are separate channels: printing the character zero would not set a successful process status.

cpp One observable line
#include <iostream>
int main() {
    std::cout << "C++20 ready\n";
    return 0;
}
Expected output
C++20 ready

The program writes exactly one line. It uses no external files, input, or platform-specific APIs.

Choose a language and library baseline

For a GCC toolchain, build with g++ -std=c++20 -Wall -Wextra -Wpedantic main.cpp -o lesson, then run ./lesson on a Unix-like shell. These commands depend on your installed toolchain and shell, so they are instructions rather than portable C++ code. The standard flag selects language rules, not a promise that every library feature exists. Consult the compiler and library support tables independently. This course uses C++20, avoids later facilities such as std::expected, and labels dependent snippets explicitly.

Calculate before printing

An expression produces a value or performs an action. Here multiplication is evaluated using integer operands before the result is inserted into the stream. The named constants describe quantities and make the calculation reviewable. Both examples are complete programs and must be compiled separately because each defines main. Predict the result before running; this forces you to separate the mathematical operation from the formatting operation. Change boxes to four and the expected result becomes twelve, while the number of output lines remains one.

cpp An integer expression with a unit
#include <iostream>
int main() {
    const int boxes{3};
    const int items_per_box{3};
    std::cout << boxes * items_per_box << " items\n";
}
Expected output
9 items

Three boxes with three items each produce nine items; the text supplies meaning that the raw integer lacks.

Read evidence in the right order

When a build fails, start at the first useful diagnostic and its file location. Later errors may be consequences of a missing brace or declaration. A linker failure usually means declarations were accepted but a required definition was absent. A program that builds can still violate its intended calculation or execute undefined behavior; compilation is only one check. Record a minimal input and expected output, rebuild after edits, and compare observed behavior. Never assign a guaranteed output to a program whose operations are undefined.

Common errors and fixes

A C++20 feature is rejected.

Why it happens
The build uses an older language mode or an incomplete implementation.
How to fix it
Select -std=c++20 and inspect both support tables.
Prevent it next time
Store the actual compiler invocation with the project.

The executable still prints the old message.

Why it happens
The edited source was not rebuilt or another executable was launched.
How to fix it
Rebuild successfully and run the explicitly named output file.
Prevent it next time
Keep build and run steps reproducible and check failures.

A name such as cout is undeclared.

Why it happens
Its header or namespace qualification is missing.
How to fix it
Include iostream and use std::cout.
Prevent it next time
Include the header that declares each facility directly.

Check your understanding

1. Adapt the boxes program to five packs containing eight cards each. Predict the complete line, and explain whether changing the printed unit alters the arithmetic.

Show worked solution

Use constants packs{5} and cards_per_pack{8}, multiply them, then append " cards\n". The complete line is "40 cards" followed by a newline. Changing only the label to "tickets" changes the explanation shown to a reader, not the integer multiplication. Rebuild before checking the output.

2. Explain a successful compilation followed by an undefined reference to a declared helper function. Which stage needs attention, and what evidence would confirm the repair?

Show worked solution

The linker cannot find the helper definition. Provide a matching definition and link the source or object file containing it. A repaired build must finish without the link error and create the intended executable. Running that executable with an expected result then checks behavior; compilation alone does not establish correctness.

Key takeaways

  • Use explicit C++20 mode and preserve reproducible commands.
  • Separate source correctness, successful linking, and runtime behavior.
  • Predict outputs only for programs with defined operations.

Official references