#000 – How C++ actually runs

Before we write a single line of C++, it’s important to understand how our programs get executed, because they are ultimately executed on hardware. However, a computer’s CPU doesn’t understand C++ so … what happens?

Three things to take away:

  • C++ is a compiled language.
  • The compiler translates C++ source code into machine code.
  • Understanding the build process makes compiler errors, linker errors, and executables less mysterious.

When you write C++, you are writing human-readable source code. C++ is a mid-level programming language.

#include <iostream>

int main(){
    std::cout << "Hello, C++\n";
}

This is meaningful to a programmer, however the CPU doesn’t know what to do with this.

The CPU executes low-level machine instructions: operations such as moving data, comparing values, jumping to another instruction, or adding two numbers in registers.

Therefore, the C++ program must pass through a translation process before it can run. In high-level languages this is done through compiling or interpreting. C++ uses the former.

Here’s a flowchart explaining what happens to the code we write.

// The C++ Pipeline

C++ source code
      ↓
Preprocessing
      ↓
Compilation
      ↓
Object files
      ↓
Linking
      ↓
Executable program
      ↓
CPU executes machine code

Compilation is not the whole build

The pipeline above reveals that compiling doesn’t not mean the entire program is ready to run. Rather our C++ programs undergo several phases before it reaches physical hardware.

In real C++ projects, the build process has multiple stages.

StageWhat it does
PreprocessingHandles #include#define, and conditional compilation
CompilationConverts source code into object code
LinkingCombines object files and libraries into one executable

Errors such as compiler and linker errors can occur at during this process.

C++ is compiled, not interpreted

C++ is usually described as a compiled language.

The compiler acts as a bridge between the programmer and the CPU. It enforces the syntax of the C++ language. When the compiler throws an error, it’s saying:

I cannot translate this program because the source code violates the rules of C++

In addition to translating our C++ source code into machine code, the compiler optimises our program. In fact, it does it better than humans!

That means the translation happens before the program runs. The result is an executable file that the operating system can load and run directly.

This differs from an interpreted language, where another program — the interpreter — reads and executes the source code or an intermediate form at runtime.

A simplified comparison:

ApproachWhat happens
CompiledSource code is translated before execution
InterpretedCode is processed as the program runs

Compilation has an important advantage: the program can be heavily checked and optimised before execution.

In Conclusion…

C++ source code is not what the CPU runs. Your program must be preprocessed, compiled, and linked into an executable containing machine code.

Once you understand that pipeline, C++ becomes less mysterious.