How C++ becomes a program
Starting out10 min readLesson 1 of 6
1.1 From source to executable
A C++ program starts as text in one or more source files. The compiler turns that text into machine instructions, and the linker joins the results into an executable file. A function declaration without a definition can pass compilation and then fail at link time because these are separate stages.
C++ is statically typed. The compiler checks names, types, and function calls before the program runs. You find many mistakes earlier than you would in a language that waits until runtime.
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
return 0;
}Output
Hello, world!
1.1.1 What each stage does
- Preprocessing expands
#includelines, macros, and conditional blocks. Headers such as<iostream>bring declarations into the source. - Compilation translates each source file into an object file. Syntax errors and type errors appear here.
- Linking connects object files and libraries to make the final executable. Missing definitions and duplicate symbols appear here.
A translation unit is one source file after preprocessing. Projects with several source files compile each translation unit separately, which keeps rebuilds manageable.
#include <iostream>
#define BUILD 3
int main() {
std::cout << "Build " << BUILD << "\n";
std::cout << "Preprocessing finished.\n";
return 0;
}Output
Build 3 Preprocessing finished.
1.1.2 Linking and libraries
The standard library ships with the compiler and supplies common code such as input and output streams, strings, and math functions. A library is a collection of compiled code plus declarations that tell the compiler what can be called.
When the linker cannot find a definition, the error often mentions the function name. Check the spelling, the parameter types, and whether every source file reached the build command. A library can also need an extra flag on the command line.
#include <cmath>
#include <iostream>
int main() {
double root = std::sqrt(81.0);
std::cout << "Square root: " << root << "\n";
return 0;
}Output
Square root: 9
1.1.3 Build a first program
Save the source as hello.cpp. The exact command depends on the toolchain, and the standard flag selects the language version.
- GCC or Clang:
g++ -std=c++17 -Wall -Wextra hello.cpp -o hello - MSVC from a developer prompt:
cl /EHsc /std:c++17 hello.cpp
Run ./hello on Linux or macOS, or hello.exe on Windows. The flags -Wall and -Wextra turn on common warnings. A warning reports code that still compiled, and it often marks a real mistake.
1.1.4 Read warnings
An unused variable may be harmless in a practice file and suspicious in a longer function. A signed and unsigned comparison can misbehave when a negative value turns into a large unsigned number.
Fix warnings while the code is small. Rebuild with warnings enabled after changes, and avoid adding new warnings to a file that had none.
#include <iostream>
int main() {
int unused = 42;
std::cout << "Warnings point at suspicious code.\n";
return 0;
}Output
Warnings point at suspicious code.
1.1.5 Read the first error
Compiler messages usually include the file, line, and a short description. Read the first error before the ones below it, because one missing semicolon can trigger several later complaints. Typical beginner messages include:
expected ';' before ...: a statement or class definition is missing a semicolon.'cout' was not declared in this scope: the header or thestd::prefix is missing.undefined reference to ...: a function is declared and called, but no definition reached the linker.
The build process also reports an exit code; 0 usually means success. Copy the exact command and the first error when you ask for help.
1.1.6 Standards and compiler choice
The examples in this course use C++17 as their baseline. C++20 adds language and library features, and most examples also build with -std=c++20. On MSVC the equivalent flag is /std:c++20. Each compiler offers small extensions, so test code with the standard you plan to use.
An editor can help with indentation, and the build command remains the source of truth. Keep a note with the exact command that works on your machine.
1.1.7 Practice
- Save the hello program, compile it with warnings enabled, and run it.
- Delete a semicolon and rebuild. Record the first error.
- Add an unused variable and compare the warning output.
- Restore the file and write down the build command you used.
A failed build still teaches you which stage rejected the code. Read the message before changing several lines at once.
