Skip to content

Beginner · First steps: compiling, output, variables and input · topic 1 of 42 · 25 min

What C++ is and how compiling works

Explanation

C++ is a fast, compiled language used for games, browsers, trading systems and devices. A compiler such as g++ or clang++ turns your .cpp text file into a program the computer runs directly. You compile once, then run the program as many times as you like.

Why learn C++?

C++ was created by Bjarne Stroustrup in the 1980s as "C with classes". Today it runs inside things you use every day: the Chrome and Firefox browsers, game engines like Unreal Engine, databases like MySQL, stock-exchange trading systems, car software and the firmware in many devices. People choose C++ when a program must be very fast or must control memory and hardware closely.

C++ is also the most popular language in competitive programming (Codeforces, CodeChef, ICPC) because it is fast and its standard library has ready-made data structures. Many Indian college placement tests accept C++, so it is a strong first or second language for students.

Compiled, not interpreted

A computer's processor only understands machine code: numbers that mean "add these", "jump there". Python reads your code line by line while it runs, using a program called an interpreter. C++ works differently. A compiler reads your whole source file first and translates it into machine code, saved as an executable file (a program you can run, like hello.exe on Windows or ./hello on Linux and macOS).

Think of it like this: Think of a cook who gets a recipe in French. An interpreter is a friend who translates each line aloud while the cook works, every time. A compiler translates the whole recipe into Hindi once and hands over the printed copy. Cooking from the printed copy is much faster, but if you change the recipe you must translate it again.

The steps inside "compiling"

  1. Preprocessing: lines starting with #, like #include <iostream>, are handled first. #include pastes the contents of another file (a header) into yours.
  2. Compiling: each .cpp file is turned into an object file (.o or .obj) of machine code.
  3. Linking: the linker joins your object files with the libraries you used (like the code behind std::cout) into one executable.

Usually one command does all three steps. You only notice them when something goes wrong: a compile error means your code broke a language rule; a linker error (for example "undefined reference to ...") means you declared a function but the linker could not find its body.

Getting a compiler

Your computerEasy optionCompile command
WindowsMSYS2 with g++ (MinGW-w64), or Visual Studio Community with "Desktop development with C++"g++ hello.cpp -o hello.exe
Linux (Ubuntu)sudo apt install build-essentialg++ hello.cpp -o hello
macOSxcode-select --install (gives clang++)clang++ hello.cpp -o hello
bash
g++ --version
g++ -std=c++20 -Wall -Wextra -O2 hello.cpp -o hello
./hello
Check the compiler, compile with warnings on, then run the program (on Windows type hello.exe).

What the flags mean

  • -std=c++20 chooses the version of the C++ language. This course uses C++17 and C++20, which every modern compiler supports.
  • -Wall -Wextra turn on warnings: hints about code that is legal but probably wrong. Always use them while learning.
  • -O2 asks the compiler to optimise (make the program faster). Leave it out while debugging.
  • -o hello names the output file. Without it, g++ makes a.out (Linux/macOS) or a.exe (Windows).
  • -g adds debug information so a debugger like gdb can show your source lines.

Common mistakes

  • "g++ is not recognized": the compiler is installed but its folder is not in PATH. Reopen the terminal after installing, or add the bin folder to PATH.
  • Editing the .cpp file and running the old program: you must compile again after every change.
  • Ignoring warnings: a warning like "unused variable" or "comparison of integers of different signs" often points to a real bug.
Tip: If installing is hard at first, you can practise in a free online compiler such as Compiler Explorer (godbolt.org) or an online judge's editor. Install a real compiler soon, though: jobs and contests expect it.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

What C++ is and how compiling works · 1/6

C++: fast and compiled

Welcome to C++. In this lesson you will learn what C++ is and how a C++ file becomes a program.

Prefer a video by a teacher? Find free videos on “how c++ compilation works preprocessor compiler linker g++ beginners” on YouTube ↗

In real life: A trading firm's order engine

Stock-trading firms in Mumbai and Bengaluru build parts of their order engines in C++. When a price changes, their program must react in microseconds (millionths of a second). Compiled C++ with -O2 runs close to the speed of the hardware, with no interpreter in between, so they compile once and run the same fast program all day.

bash
g++ -std=c++20 -O2 -Wall order_engine.cpp -o order_engine
./order_engine
Compile with optimisation for production, then run.

Practice (0/3 done)

Task 1 · Question

Which command compiles hello.cpp with C++20 and warnings on, and names the program hello?

Task 2 · Question

You get "undefined reference to `add(int, int)`" after compiling. Which step failed?

Task 3 · Practice

Install a C++ compiler on your computer (MSYS2 g++ or Visual Studio on Windows, build-essential on Linux, Xcode tools on macOS). Then compile and run a program that prints your name.

Quick quiz

Question 1

1.What does a C++ compiler produce from your source code?

Question 2

2.What does #include <iostream> do?

Question 3

3.Why add -Wall -Wextra?

Question 4

4.You changed hello.cpp but the program still prints the old text. Why?

Question 5

5.Which flag asks the compiler to optimise the program for speed?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does a C++ compiler produce from your source code?

Answer: Machine code in an executable file

The compiler translates C++ into machine code, saved as a program you can run directly.

What does #include <iostream> do?

Answer: Pastes the iostream header into your file before compiling

The preprocessor replaces #include with the contents of that header, which declares std::cout and std::cin.

Why add -Wall -Wextra?

Answer: To get warnings about code that is legal but probably wrong

Warnings catch likely bugs early, such as unused variables or suspicious comparisons.

You changed hello.cpp but the program still prints the old text. Why?

Answer: You did not compile again

The executable is made at compile time. Changes in the source appear only after you compile again.

Which flag asks the compiler to optimise the program for speed?

Answer: -O2

-O2 (capital letter O) turns on optimisation. Lowercase -o names the output file.

What is "undefined behaviour"?Related

Answer: Code whose result the C standard does not define, so anything can happen

Crashes, garbage or silent bugs.

From What C is and why it still matters

Which stage expands #include and #define?Related

Answer: Preprocessing

gcc -E shows the preprocessor output.

From Compiling with gcc

What is a compiler?Related

Answer: A program that turns source code into machine code before it runs

A compiler translates your code into a file the computer can run directly. Rust's is called rustc.

From What Rust is and why learn it

Which game engine is written in and scripted with C++?Related

Answer: Unreal Engine

Unreal Engine is a major C++ game engine.

From Careers with C++ and your next steps

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “First steps: compiling, output, variables and input” before its exam: Your first program: main, cout and namespaces, Variables, types and constants, Reading input and formatting output.
  2. Module 1 exam: First steps · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Decisions, loops and functions. 42 topics left in the course before the final exam.