Beginner · First steps: compiling, output, variables and input · topic 1 of 42 · 25 min
What C++ is and how compiling works
Explanation
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).
The steps inside "compiling"
- Preprocessing: lines starting with #, like #include <iostream>, are handled first. #include pastes the contents of another file (a header) into yours.
- Compiling: each .cpp file is turned into an object file (.o or .obj) of machine code.
- 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 computer | Easy option | Compile command |
|---|---|---|
| Windows | MSYS2 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-essential | g++ hello.cpp -o hello |
| macOS | xcode-select --install (gives clang++) | clang++ hello.cpp -o hello |
g++ --version
g++ -std=c++20 -Wall -Wextra -O2 hello.cpp -o hello
./helloWhat 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.
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
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.
g++ -std=c++20 -O2 -Wall order_engine.cpp -o order_engine
./order_enginePractice (0/3 done)
Which command compiles hello.cpp with C++20 and warnings on, and names the program hello?
You get "undefined reference to `add(int, int)`" after compiling. Which step failed?
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
Read more (free)
Related help
Related topics
- Your first program: main, cout and namespacesFirst steps: compiling, output, variables and input · same module
- Variables, types and constantsFirst steps: compiling, output, variables and input · same module
- Reading input and formatting outputFirst steps: compiling, output, variables and input · same module
- What C is and why it still mattersC Programming (from zero to systems) · Getting started with C
- Compiling with gccC Programming (from zero to systems) · Getting started with C
- What Rust is and why learn itRust Programming · Getting started with Rust
Questions people ask about this
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.
Which stage expands #include and #define?Related
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.
Which game engine is written in and scripted with C++?Related
Answer: Unreal Engine
Unreal Engine is a major C++ game engine.
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
- 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.
- Module 1 exam: First steps · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
- Then the next module, Decisions, loops and functions. 42 topics left in the course before the final exam.