0.4 — Compiling: turning code into a program

📘 Chapter 0⏱ about 1 min read✍️ Beginner friendly
Read one box at a time. No rush.

The extra step#

In some languages, you write code and run it right away. C++ has an extra step in the middle.

  1. You write code in a text file. It ends in .cpp, like main.cpp.
  2. A tool called a compiler checks your code and turns it into a program.
  3. You run the program.
Compiler

A tool that reads your C++ code and turns it into a program your computer can run. It also tells you when your code has mistakes.

Why compile?#

Compiling happens once, before the program runs. All the slow work of "understanding" your code is done ahead of time.

That is one big reason C++ programs are so fast.

The compiler is your helper#

When your code has a mistake, the compiler stops and shows an error message. It says which line has the problem.

Output
main.cpp:5:5: error: expected ';' before 'return'

This one says: "In main.cpp, line 5, I expected a semicolon".

Key idea

Error messages are not a punishment. They are the compiler pointing at the exact line you need to fix.

The linker (quick look)#

After compiling, another tool called the linker joins your code together with ready-made code (like the code for printing text). The result is one finished program.

Most of the time, you press one button and both steps happen. You will rarely think about the linker as a beginner.

Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.