Skip to content
KALANBLON
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:

  1. 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;
  2. 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.

Step 5 of 5
Text description of the animation
  1. You write your program in a text file whose name ends in .cpp.
  2. You ask g++ to compile it. It starts by checking the code.
  3. At the slightest problem, it stops and gives the file, the line and the cause. No executable is produced.
  4. If everything is correct, the executable is created. You run it with ./hello.
  5. 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 --install in the Terminal. You get the Clang compiler, which also answers to the name g++ 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 executable hello (hello.exe on 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
Start from hello.cpp and write three lines std::cout << "...\n";.
Hint 2
The compile command is the same, with other names: 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 gcc instead of g++: compilation may fail at the final step (undefined reference to std::cout), because gcc doesn't add the C++ library.
  • Running ./profile.cpp: you run the executable ./profile, not the source file.

Summary

  1. 1C++ is compiled: g++ translates the .cpp file into an executable.
  2. 2You compile with g++ -std=c++20 -Wall -Wextra -o name name.cpp, then run ./name.
  3. 3After every change, you must recompile.
  4. 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.