Toolchain & First Program

Scope: You will install a C++ compiler, write a first program called hello.cpp, and learn the commands that turn source code into a running application. Everything is explained step by step — no terminals experience needed.

What you need

Writing C++ requires three pieces of software: a compiler that translates C++ source code into machine instructions, a text editor where you type the code, and a terminal where you run commands. The most popular free compilers are g++ (from the GNU project) and clang++ (from LLVM). This roadmap uses g++, which works identically on Windows, macOS and Linux.

Install a compiler

Pick the section for your operating system. Each command must be typed in a terminal (on Windows this is a Git Bash, PowerShell or MSYS2 shell; on macOS and Linux it is the built-in Terminal).

Windows

The simplest complete toolchain is MSYS2, a free package manager. Install it from msys2.org, then open the MSYS2 UCRT64 shell and install the compiler:

pacman -Syu          # update the package list
pacman -S mingw-w64-ucrt-x86_64-gcc   # install g++ for 64-bit Windows

The mingw-w64 toolchain installs g++, the linker, and the standard library into a folder you must add to your PATH. An alternative that avoids path configuration is the Windows Subsystem for Linux (WSL), inside which you follow the Linux instructions below.

macOS

Apple ships the LLVM compiler (clang++, compatible with g++ commands) through the Xcode Command Line Tools. Open a terminal and run:

xcode-select --install   # installs clang++, the linker and the standard library

Linux

Most distributions have g++ in their package manager:

sudo apt install g++        # Debian, Ubuntu and Mint
sudo dnf install gcc-c++    # Fedora

Verify the installation

Confirm the compiler works by asking for its version. You should see a version number that supports at least C++17 (GCC 8 or newer is enough).

g++ --version

Your first program

Create a folder for your C++ projects, for example cpp-lessons, and inside it a text file named hello.cpp. Type the program below exactly — every symbol matters, including the semicolons.

// hello.cpp — prints a greeting to the console.
#include <iostream>      // std::cout lives in this library

int main() {              // execution starts here
    std::cout << "Hello, world!\n";  // \n means "new line"
    return 0;             // 0 = success, sent to the operating system
}

The file extension .cpp tells the compiler this is C++ source code. Save the file and move to the terminal — it is time to compile.

Compile and run

Compile

The command below invokes g++, asks for the C++17 standard, enables two warning levels (-Wall -Wextra — always use them), and names the output executable hello:

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

If everything is correct the command prints nothing — silence means success. If there are errors, the terminal shows the file name, the line number, and a message such as expected ';' before '}'. Fix the reported line and compile again.

Run

The compiled program is now a separate file. Run it from the same folder:

./hello          # macOS and Linux
hello.exe        # Windows (Git Bash: ./hello.exe)

You should see Hello, world! on the screen. Congratulations — you have completed the full cycle: write, compile, run.

What the compiler does

A compiler is not one tool but a pipeline of several stages. Understanding the pipeline explains many error messages you will meet later.

The build pipeline: source code, preprocessor, compiler, object file, linker, executable.

The stages that turn hello.cpp into a runnable program.

  • Preprocessor — resolves #include lines and text macros.
  • Compiler — translates the C++ source into machine instructions and stores them in an object file.
  • Linker — combines your object file with the standard library and produces the final executable.

When you compile a single-file program, g++ performs all three stages automatically. Later, in the build systems lesson, you will drive these stages on projects with many files.

Common beginner errors

Everyone makes these at the start. Recognizing them saves you time:

  • Missing semicolon — every statement ends with ;. The error usually points at the next line.
  • Wrong case — C++ is case-sensitive: Main is not main, cout is not Cout.
  • Missing #include <iostream> — without it, std::cout is unknown to the compiler.
  • Undefined name — count instead of cout produces 'count' was not declared.
  • Run without compiling — always compile after editing; the executable is a snapshot of the last successful build.

Editors and IDEs

Any text editor works for C++, but a few tools make the experience much smoother. You can start with the simplest option and upgrade later — the language and compiler stay the same.

  • VS Code (free) with the C/C++ extension: syntax colors, error underlines and a terminal inside the window.
  • Qt Creator (free): a full IDE tuned for C++ with an integrated debugger.
  • CLion (paid): professional IDE from JetBrains, popular in industry.

Online playgrounds

When you do not want to install anything, a browser-based compiler works for small exercises. The two tools below are the most useful for learning:

  • Compiler Explorer — compiles in your browser and shows the machine code, ideal for understanding what C++ becomes.
  • OnlineGDB — a full editor, compiler and debugger in one page.

Practice

A short exercise cements the toolchain. Change the message, rebuild, and run again:

  1. Modify hello.cpp so it prints your name: Hello, Ada!
  2. Add a second line: std::cout << "I am learning C++.\n";
  3. Remove the semicolon on purpose, compile, and read the error message.
  4. Fix it, recompile, and run. Notice that the old executable did not change until you recompiled.