Installing a C++ compiler on any operating system comes down to one idea. You add a compiler to your PATH, and then the terminal can find it. Linux gives you the whole toolchain in one package, macOS gives it to you through the Xcode command line tools, and Windows lets you pick between the Microsoft compiler and a Unix-like one that behaves the same everywhere.
This is the first lesson in the Getting Started module, and the next lesson in this module is about "What Does Your First C++ Program Look Like?".
Installing a C++ compiler on Windows, Linux and macOS at a glance
Before the details for each platform, it helps to know the shape of the answer. A C++ compiler is a program called g++, clang++ or cl.exe. It reads a source file and it writes an executable. It is not part of Windows, macOS or Linux by default, which is why a fresh laptop usually answers "command not found" the first time you try it. Every fix below ends with the same two steps. You install the toolchain, and then you confirm that the terminal can find it.
There are three mainstream choices, and it is worth telling them apart before you start.
- GNU Compiler Collection (g++) — this is the default on most Linux distributions. It is available on macOS through Homebrew or the Xcode tools, and it is available on Windows through MSYS2 or WSL. It has very good standard library support, and it is the compiler that most online tutorials assume.
- LLVM/Clang (clang++) — this is the default on Apple platforms. It gives faster error messages and stricter warnings. The C++ standard library it ships with is usually the same GNU one.
- Microsoft Visual C++ (cl.exe) — this ships with Visual Studio on Windows, and it is the only option that is guaranteed to produce binaries that plug into the Microsoft toolchain directly.
When you are learning C++ from scratch, pick one and stay with it. Most tutorials, most compilers and most compiler flags translate almost one to one between them, and switching toolchains later costs far more time than it saves you now.
Installing a C++ compiler on Windows
Windows has three reasonable routes, and they are not equally painful. The Visual Studio route is the most integrated one. You download the Community edition, you run the installer, and you tick the workload called "Desktop development with C++". That single tick installs the MSVC compiler, the Windows SDK, the debugger and an editor, and it sets up the environment for you.
The route that most closely matches a Linux or macOS course is MSYS2. It gives you a Unix-like shell, a package manager called pacman, and a current g++.
# Windows: install MSYS2 from the official installer, then run this in the MSYS2 UCRT64 shell
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
# confirm the toolchain landed on the PATH
g++ --version
clang++ --versionThe third route is the Windows Subsystem for Linux. If you already use WSL, then you install the build tools inside your Linux distribution and you never have to think about Windows compilers again. The catch is that your source files live inside the WSL filesystem, and editing them from a Windows editor over a network share works but is noticeably slower.
If you want the shortest possible path to a working compiler on Windows then Visual Studio is the answer. If you want the compiler that your course assumes, then install MSYS2 or use WSL.
Installing a C++ compiler on macOS
macOS is the easiest of the three. The Xcode command line tools contain the Clang compiler and the standard library, and installing them takes one command from a terminal.
# macOS: install the Xcode command line tools (Apple's compiler, clang++)
xcode-select --install
# confirm
clang++ --versionIf the dialog that appears asks you for permission, then you accept it. The download is a few hundred megabytes and it takes a few minutes. If you would rather have GNU g++ on macOS, then Homebrew installs it in seconds and links it as g++ alongside the Apple compiler.
# macOS: install GNU g++ through Homebrew as well
brew install gcc
brew install --c++ gcc
g++-13 --versionHomebrew deliberately does not overwrite the Apple compiler, so you can keep both and choose per project.
Installing a C++ compiler on Linux
Linux distributions ship a C++ compiler in their package repository. The exact package name is different on each one, but the shape of the command is the same everywhere.
# Debian, Ubuntu, Mint
sudo apt update
sudo apt install build-essential
# Fedora, RHEL, CentOS Stream
sudo dnf install gcc gcc-c++ make
# Arch, Manjaro
sudo pacman -S base-develThe package usually installs several tools at once. You get the C compiler gcc, the C++ compiler g++, the linker, the archiver and make. You want all of them, because a C++ program needs more than a compiler. It needs a linker to combine your code with the standard library.
The same idea applies on a minimal container image. A Debian slim image needs the same build-essential package, and a Fedora image needs the gcc-c++ one.
# inside a minimal container
apt-get update && apt-get install -y build-essential
g++ --versionVerifying the installation
Verification has two halves, and beginners routinely check only the first one. You ask the compiler for its version, and then you ask it to build and run something trivial.
# 1. which compiler, and which version
g++ --versionThe second half is a real program, compiled and run in one step so that a failed compile can never be mistaken for a successful run.
#include <iostream>
int main() {
std::cout << "the toolchain works\n";
return 0;
}g++ -std=c++17 -Wall -Wextra hello.cpp -o hello && ./helloA missing header is a compile problem, an undefined reference is a link problem, and a missing shared library is a run time problem, so the message names the stage that failed.
The build step should print nothing at all, and the exit code should be zero. Silence from a compiler is success. Any output at all means it had something to say, and that is usually a warning or an error. The compiled program above is the real proof, because it exercises the full chain. It runs the preprocessor, the compiler proper, the assembler and the linker.
What to do when the compiler is not found
If the shell reports that g++ is not recognised, then the binary exists somewhere but it is not on the PATH. You find out where it lives first, and then you decide whether to move the PATH or to move the binary.
# Linux and macOS: is it installed but not on the PATH?
ls /usr/bin/g++* /usr/local/bin/g++* 2>/dev/null
which -a g++
# Windows (PowerShell)
where.exe g++
Get-Command g++ -ErrorAction SilentlyContinueIf the compiler is installed under a path such as /usr/local/bin, then you add that directory to your PATH permanently instead of exporting it for one shell session.
# Linux and macOS: make the change stick
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrcOn Windows, the equivalent is the System Properties dialog. You open Environment Variables, you add the compiler's bin folder to the user PATH, and then you open a brand new terminal afterwards. The new window step is not optional, because already-open terminals keep the old PATH that they were started with.
Which language standard should you compile with?
Modern C++ is versioned, and the standard flag is how you tell the compiler which version of the language to accept. You should compile with C++17 or later for anything you write today.
g++ -std=c++17 -Wall -Wextra -O2 main.cpp -o programThe flags in that command are worth understanding even at this early stage. -std=c++17 selects the language version. -Wall turns on the common warnings, -Wextra adds more of them, and -O2 asks for optimised machine code. You can drop any one of them, but you should never ship code that was compiled without at least -Wall.
Checking what the compiler supports
The version string tells you which compiler you have. A small program tells you which language features it accepts. The __cplusplus macro expands to a number that describes the standard in use.
#include <iostream>
int main() {
std::cout << "__cplusplus: " << __cplusplus << "\n";
#if __cplusplus >= 201703L
std::cout << "C++17 or later is available\n";
#else
std::cout << "this compiler defaults to an older standard\n";
#endif
return 0;
}The output on a modern compiler reads like this.
__cplusplus: 201703
C++17 or later is availableThe #if directive is a preprocessor conditional, and it works in every part of a C++ file. Because the standard is known before compilation begins, a compiler can even be asked to reject code that uses a newer feature.
Quick reference
- Windows: Visual Studio with the C++ workload, MSYS2 with pacman, or WSL with build-essential. All three work.
-
macOS:
xcode-select --installgives you clang++, and Homebrew adds GNU g++ alongside it. - Linux: one package gives you the compiler, the linker and make. That is build-essential on the Debian family and gcc-c++ on the Fedora family.
-
Verify with a real build, and not just with
--version. Compile a trivial program and check the exit code. - If the command is not found, then the compiler is installed but it is not on the PATH. Find it, and then fix the PATH.
-
Compile with
-std=c++17and-Wall -Wextrafrom your very first project, so that warnings surface early.
Building something a little larger
Once the smoke test works, the next step is a program that does real work.
#include <iostream>
#include <string>
double to_fahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32.0;
}
int main() {
std::cout << "21.5C is " << to_fahrenheit(21.5) << "F\n";
return 0;
}The output confirms the arithmetic.
21.5C is 70.7FTry it yourself: install a compiler using the route for your platform, and then run the three step verification. Print the version, compile the smoke test, and run it. Then deliberately break the PATH for a single shell session, confirm the "command not found" message, restore it, and note which folder on your machine actually holds the compiler binary.