Chapter 0

Installing a Compiler Without Losing Your Mind

Mac, Windows, and Linux, each done properly.

Nothing else in this book works until your computer can turn text into a program. That’s what this chapter is for. It’s also the only chapter where you’ll spend an hour and end up with nothing to show anyone.

Let me be straight with you: this is where most people give up on C++. Not because it’s difficult — because it’s dull, the error messages are unhelpful, and nobody warns them first. Consider yourself warned. Everything after this chapter is the good part.

Read only the section for your own machine — macOS, Windows, Linux. The other two are for other people.

What a compiler actually does

A C++ file is a text file. Your computer can’t run it, the same way it can’t run a shopping list. Something has to translate.

That something is a compiler. It reads your text and writes out a program — a real file your machine knows how to execute. So the loop is: you write text, the compiler makes a program, you run the program. You’re going to do that a few hundred times before this book is over, and by chapter 3 it’ll feel like nothing.

The compiler in this book is called g++, and it’s free on all three platforms. When you’re done here you’ll have:

That’s the whole list. Then we stop.

Installing it

macOS

Apple ships a compiler, it just doesn’t install it until you ask. Open Terminal (press ⌘-space, type “terminal”, hit return) and run:

xcode-select --install

A dialog box appears. Click Install, agree to the licence, and wait — it’s a sizeable download and it will look stuck when it isn’t. When the dialog says it’s finished, check that it worked:

g++ --version

You should see a few lines starting with Apple clang version. That’s the right answer, even though you asked for g++.

You’re done installing. Skip the next two sections and carry on at A place to keep your code.

Windows

Windows doesn’t come with a C++ compiler, so we’ll install one called MSYS2. It gives you g++ plus a terminal that behaves like the ones Mac and Linux readers are using, which means every command in this book works for you unchanged.

  1. Download the installer from msys2.org and run it. Accept the default location, C:\msys64.

  2. When it finishes, it opens a terminal window for you. Run this, which updates the freshly-installed packages:

    pacman -Syu

    It may announce that it has to close the terminal to finish. Let it. Then reopen the terminal and run pacman -Syu a second time.

  3. Now install the compiler:

    pacman -S mingw-w64-ucrt-x86_64-gcc

    It’ll ask you to confirm. Press return.

  4. Check it:

    g++ --version

Your files live in C:\msys64\home\ followed by your username. In the terminal, that same folder is called ~. Same place, two names — one for Windows, one for the terminal. That will make more sense in about five minutes.

Carry on at A place to keep your code.

Linux

You know your package manager. On Debian or Ubuntu:

sudo apt update
sudo apt install build-essential

On Fedora, sudo dnf install gcc-c++. On Arch, sudo pacman -S base-devel. Then:

g++ --version

Anything from version 7 upward can handle the C++17 in this book, and if you installed from a current repository you have something much newer.

A place to keep your code

Your terminal is always sitting in a folder, and it only sees the files in that folder. Most first-day confusion is really this one fact in disguise: the file is saved in Downloads, the terminal is somewhere else, and the compiler insists your file doesn’t exist.

So make yourself a folder and go into it:

mkdir cpp
cd cpp

mkdir makes a directory. cd changes into it. Run pwd if you ever want the terminal to tell you where it currently thinks it is — you’ll want that more often than you’d expect.

Something to type in

Any program that saves plain text will do. If you don’t already have a preference, the popular choices are Visual Studio Code, CLion, and Visual Studio (Windows only). This book takes no position on which — it never depends on your editor, only on your terminal, so use whatever you like and switch whenever you want.

Your first program

In your cpp folder, save this as hello.cpp. Type it out rather than pasting it — you’ll learn where the fiddly punctuation lives:

#include <iostream>

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

Don’t worry about what any of it means. Really — not a single line. Chapter 1 takes this exact program apart piece by piece. Right now it’s just something to feed the compiler.

Compiling it

g++ -std=c++17 -Wall -Wextra -g hello.cpp -o hello

That’s the command for the whole book. It never changes except for the filename, so it’s worth knowing what the pieces are:

Part What it does
-std=c++17 Use the 2017 version of C++. Without this you may get an older one.
-Wall -Wextra Tell me about suspicious code, not just outright errors.
-g Keep the notes a debugger needs. Chapter 11 uses them.
hello.cpp The file to read.
-o hello Name the program hello. Leave this off and you get a.out.

If it worked, the compiler says nothing at all. No “success”, no summary, nothing. Silence is the compliment.

Running it

./hello

It prints Hello, world. On Windows the file is actually called hello.exe, but ./hello still finds it.

Breaking it on purpose

One more thing before you go, and it’s the useful one.

Open hello.cpp, delete the semicolon at the end of the std::cout line, save, and compile again. You’ll get something like:

hello.cpp:4:34: error: expected ';' after expression
    4 |     std::cout << "Hello, world\n"
      |                                  ^
      |                                  ;
1 error generated.

Two things to notice, because they’ll save you time for years.

It tells you the file, the line, and the column — hello.cpp:4:34 — and then shows you the line with a caret under the exact spot. It even prints the semicolon it wanted. Compilers suggest fixes more often than people expect.

The wording won’t be identical for everyone. That’s what clang on a Mac says; Windows and Linux readers have GNU g++, which reports the same mistake as expected ';' before '}' token and points at the next line, because that’s where it finally gave up waiting for the semicolon. Either way the lesson holds: the line it names is where the compiler noticed, which can be after where you went wrong. Look up as well as down.

Put the semicolon back, recompile, and confirm it runs. Chapter 2 is entirely about reading these messages, and you’ve now met your first one on your own terms, which is a much better introduction than meeting it at midnight.

When it goes wrong

What you see What it usually is
g++: command not found The install didn’t finish, or you’re in the wrong terminal. On Windows, check you opened MSYS2 UCRT64.
g++ worked yesterday, not today Terminal opened before the install finished. Close it and open a new one.
No such file or directory: hello.cpp You’re in a different folder from your file. Run pwd and ls to see where you are and what’s there.
hello.cpp.txt shows up in ls Your editor added the extension. Rename it.
permission denied when running ./hello The compile failed, so there’s no program to run. Scroll up for the real error.

Before you move on

Every one of those matters for chapter 1. If any is unticked, that’s the thing to sort out now — the next chapter assumes all six.

Project

Your toolchain

Roughly 1 hour, most of it waiting

You’ve already built it, so this one is about proving it lasted.

Close your terminal completely. Open a brand new one, then:

cd cpp
./hello

If it prints Hello, world, your setup is real and it survives being closed. That matters more than it sounds — plenty of people have a compiler that works for exactly one session and don’t discover it for a week.

Then, tomorrow, do it once more from scratch: new terminal, cd cpp, edit hello.cpp to print your own name instead, compile, run. You’ll have done the entire loop unaided, which is the only skill chapter 0 was ever really about.