Lesson 1 of 6
In Module 1 you ran g++ hello.cpp -o hello and then ./hello. Two commands, two very
different things. This lesson is about what happened in between.
Your processor doesn't speak C++
The chip inside your computer executes machine code — numbers encoding tiny operations like
"add these two registers," "copy this byte," "jump to that address." It has no concept of
std::cout, of a for loop, or of a function name.
C++ exists for you, not for the processor. So something has to translate one into the other, and that something is the compiler.
hello.cpp hello
(text you wrote) ───► (machine code the CPU runs)
g++
Compiled versus interpreted, concretely
Python doesn't do this. When you run python script.py, a program called the interpreter reads
your source and acts on it as it goes, every time you run it. The source is the program.
C++ translates ahead of time, once. The result is a standalone file. Try this — you already have
hello from Module 1:
ls -l hello hello.cpp
You'll see two files of very different sizes. hello.cpp is a few hundred bytes of text you can
open and read. hello is much larger and is not text at all — it's your program in the CPU's
own language, plus the bookkeeping the operating system needs to load it.
That file doesn't need g++ to run. It doesn't need the source. Copy it to another machine with
the same OS and processor and it runs there too.
What you get, and what you pay
This is the central trade-off of the language:
What you get
- Speed. The translation work happens once, at build time. At run time the CPU executes its own instructions directly, with no interpreter in the middle.
- Errors found before running. The compiler must understand your entire program to translate it. If you misspell a name or pass the wrong type, it says so — before a single line executes.
- No runtime needed. You ship one file. The user needs no C++ installed at all.
What you pay
- A build step. Every change means recompiling before you can see the result.
- Build times. Hello World is instant; a large project can take minutes.
- Platform-specific output. A binary compiled on Linux won't run on Windows. The source is portable; the compiled result isn't.
That second point in the first list is worth sitting with. In Python, a typo in a rarely-used branch is a bug that ships and crashes for a user six months later. In C++, that same typo very often stops the build. The compiler is not being difficult — it's doing the work of a test suite you didn't have to write.
Try it
Delete your compiled hello file with rm hello, then run ./hello again. Then recompile and
run it once more. What does the difference tell you about where your program actually lives?
Checkpoint
- Processors execute machine code, not C++ — the compiler translates one to the other.
- Compilation happens once, ahead of time, and produces a standalone executable file.
- You pay a build step and lose binary portability; you gain speed and errors caught before anything runs.
- Deleting the executable doesn't delete the program's source, and vice versa — they're separate files with separate jobs.
Questions about this lesson
Log in to ask a question.