The source code
You write a C++ program as plain text in a file, here hello.cpp. This is the source code: words and symbols a person can read. Your CPU cannot run this text directly.
Every C++ program has one function called main, and running the program means running main from its first line. std::cout << sends text to the screen. return 0 ends main and reports success.
Building
You type one command, g++ hello.cpp -o hello, and it runs three tools in turn. First the preprocessor handles lines that start with #. #include <iostream> pastes in that file's text, which tells the compiler what std::cout is.
The compiler checks the code against the rules of C++, then translates it into machine code: the numbers your CPU really runs. It saves them in an object file, hello.o. It is not runnable yet: it still needs std::cout's code.
Leave out one ; and the rules are broken. The compiler stops, names the file and line, and writes no object file and no program. This is a compile error: you fix the source and build again.
The code that really puts text on the screen comes ready-made with the compiler, in the C++ standard library. The linker joins hello.o with that library code and writes one file you can run: the executable, hello.
Running
Now type ./hello. The operating system loads the executable into memory and the CPU starts at main. Line 3 prints Hello! in the terminal, and return 0 tells the system the program finished well.
Compiling only checks the rules. A program that compiles can still go wrong while it runs: a wrong answer, or a crash. These are run-time errors, and you find them by running and testing the program.
You compile once and run the executable as often as you like. It is already machine code, so no translator runs alongside it. C++ also lets you decide where data lives in memory and for how long. That is why it is fast.
What happens
;; step 1/9 · source
hello.cpp the source file (.cpp is the usual ending)
1 #include <iostream>
2 int main() {
3 std::cout << "Hello!\n";
4 return 0;
5 }
people read and write this; the CPU cannot run it as it is
The C++ standard describes nine phases of translation; the figure groups them into the three tools you meet in practice. One command such as g++ hello.cpp -o hello runs all three for you. File names differ between systems: object files end in .o on Linux and macOS and .obj on Windows, and the executable is hello.exe on Windows. On Windows with Microsoft's compiler the command is cl /EHsc hello.cpp.