Show contents
Chapter 1 of 4 · 15 min
Installing a compiler and compiling
By the end of this chapter, you'll be able to
- Install a C++ compiler on your computer
- Check the installation with g++ --version
- Compile a .cpp file into an executable and run it
- Read the first error displayed by the compiler
Compiling means translating once and for all
Some languages, like Python, are read and run line by line every time they're launched. C++ works in two stages:
- the compiler reads your source file (
hello.cpp), checks that it follows the rules of the language, then translates it into an executable full of machine instructions; - you then run that executable, as many times as you like, without the compiler.
The compiler is also your first reviewer: it refuses to translate an incorrect program and explains why.
The processor carries out the instructions and the text appears.
Text description of the animation
- You write your program in a text file whose name ends in .cpp.
- You ask g++ to compile it. It starts by checking the code.
- At the slightest problem, it stops and gives the file, the line and the cause. No executable is produced.
- If everything is correct, the executable is created. You run it with ./hello.
- The processor carries out the instructions and the text appears.
Installing the compiler
We use g++, the C++ compiler from the free GCC project. The examples in this course are checked with g++ 14, following the C++20 standard.
- Linux (Ubuntu, Debian):
sudo apt install build-essential. - Linux (Fedora):
sudo dnf install gcc-c++. - macOS: type
xcode-select --installin the Terminal. You get the Clang compiler, which also answers to the nameg++and accepts the same options. - Windows: install MSYS2 from its official website (msys2.org), open the "MSYS2 UCRT64" terminal, then type
pacman -S mingw-w64-ucrt-x86_64-gcc.
Then check the installation:
g++ --version
If a version number appears, you're ready. Otherwise, close and reopen your terminal, then try again.
Your first program
Create a file hello.cpp in a text editor (VS Code for example):
#include <iostream>
int main() {
std::cout << "Hello from C++!\n";
return 0;
}
In the terminal, go into the file's folder (cd path/to/folder), then compile and run:
g++ -std=c++20 -Wall -Wextra -o hello hello.cpp
./hello
Hello from C++!
-std=c++20: follow the C++20 standard;-Wall -Wextra: show the warnings, the messages that point out suspicious code. Always keep them on;-o hello: name the executablehello(hello.exeon Windows);./hello: run the program in the current folder.
Your turn
Worked exercise
Worked exercise: your profile
Write a program profile.cpp that displays three lines: your first name, your country and what you'd like to build in C++. Compile it with warnings turned on, then run it.
Hint 1
hello.cpp and write three lines std::cout << "...\n";.Hint 2
g++ -std=c++20 -Wall -Wextra -o profile profile.cpp.Show the solution and common mistakes
#include <iostream>
int main() {
std::cout << "First name: Yuki\n";
std::cout << "Country: Japan\n";
std::cout << "Project: a small platform game\n";
return 0;
}
First name: Yuki
Country: Japan
Project: a small platform game
Common mistakes:
- Compiling with
gccinstead ofg++: compilation may fail at the final step (undefined reference to std::cout), becausegccdoesn't add the C++ library. - Running
./profile.cpp: you run the executable./profile, not the source file.
Summary
- 1C++ is compiled: g++ translates the .cpp file into an executable.
- 2You compile with g++ -std=c++20 -Wall -Wextra -o name name.cpp, then run ./name.
- 3After every change, you must recompile.
- 4Faced with an error, read the first line containing error: file, line, column, cause.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.