← Retour au cours: Modern C++ from scratch

Installing the compiler and your first program

Vidéo bientôt disponible

C++ is a compiled language: you write text in a source file and a program called the compiler translates it into an executable that the processor understands directly. That is why C++ is so fast and why it is used in games, browsers, operating systems and embedded software. In this course we use the C++20 standard, and we mention C++23 when it adds something useful. The good news is that modern C++ is much friendlier than its reputation suggests.

Choosing and installing a compiler

There are three main compilers. g++ (part of GCC) and clang++ are common on Linux and macOS; MSVC comes with Visual Studio on Windows. On Windows you can also install MSYS2 to get g++, or use WSL to run Linux inside Windows. On Ubuntu or Debian it is enough to run sudo apt install g++ . On macOS, type xcode-select --install to get clang++. To check that everything works, open a terminal and run g++ --version (or clang++ --version). If you see a version number of 10 or higher you are ready; for full C++20 support g++ 11 or newer is better.

Your first program

cpp
#include <iostream>

int main() {
    std::cout << "Hello, C++!" << std::endl;
    return 0;
}

Let us go line by line. #include <iostream> asks the preprocessor to copy in the declarations of the input and output library. int main() is the function where every program starts; it returns an integer that the operating system reads as an exit code, and 0 means everything went well. std::cout is the standard output stream (the screen), the << operator sends data into it and std::endl adds a line break. The std:: prefix says that cout lives in the standard library namespace. Every statement ends with a semicolon; forgetting it is the most common beginner mistake.

Compiling and running

Save the code as hello.cpp. In the terminal compile with g++ -std=c++20 -Wall -Wextra hello.cpp -o hello . The option -std=c++20 picks the standard, -Wall and -Wextra turn on useful warnings and -o hello names the executable. Then run it with ./hello (on Windows, hello.exe). With clang++ the command is identical. If the compiler shows errors, read the first one calmly: it gives the file, the line and the probable cause; the following ones are often a consequence of the first.

cpp
#include <iostream>

int main() {
    // Several outputs can be chained with <<
    std::cout << "Language: " << "C++" << '\n';
    std::cout << "Standard: " << 20 << '\n';
    return 0;
}

A typical mistake: writing cout without the std:: prefix gives 'cout was not declared in this scope'. The fix is to write std::cout. Another frequent error is using single quotes for a text: 'Hello' is not valid, because single quotes are for one character and double quotes are for strings. Practice by causing these errors on purpose so you learn to recognize the compiler messages.

✎ Mes notes

Enregistré seulement dans ce navigateur. Connectez-vous pour le garder dans votre compte.0/5000 caractères

Suivante →