Step-by-Step Guide – Installing C++ on Windows, Mac, and Linux
diagram of Installing C++ cplus on Windows, Mac, Linux
Introduction
To write and run C++ programs, you need a C++ compiler installed on your computer. The most commonly used compiler is GCC (GNU Compiler Collection), which is cross-platform and free. This guide will walk you through installing C++ on Windows, macOS, and Linux.
Visit the MinGW official website.
Download the mingw-get-setup.exe installer.
Run the installer and choose mingw32-gcc-g++ package.
Complete installation.
Go to Control Panel → System → Advanced System Settings.
Click Environment Variables.
Under System variables, edit Path and add:
C:\MinGW\bin
Click OK to save.
Open Command Prompt and type:
g++ --version
If installed correctly, it will display the GCC version.
Open Terminal.
Run:
xcode-select --install
Accept the license agreement and wait for installation.
Check GCC version by running:
g++ --version
This confirms that the C++ compiler is installed.
Open the terminal and update the package list:
sudo apt update # For Ubuntu/Debian
sudo dnf update # For Fedora
sudo apt install build-essential # Ubuntu/Debian
sudo dnf install gcc-c++ # Fedora
sudo pacman -S gcc # Arch Linux
g++ --version
This should show the installed version of GCC.
Once installed, create a simple C++ file named hello.cpp:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Compile and run it:
g++ hello.cpp -o hello
./hello
You should see: Hello, World!
With a compiler installed on your system, you are now ready to write and execute C++ programs. This setup is the first step towards mastering C++ programming on Windows, macOS, and Linux.
Meta Description:
A clear, copy‑paste step‑by‑step guide to install a C++ toolchain on Windows, macOS, and major Linux distributions. Includes verification, a sample program, and troubleshooting tips.
Meta Keywords:
install C++, install g++, install MSVC, MSYS2, Xcode command line tools, build-essential, WSL
C++ development requires a compiler (GCC/Clang/MSVC), optional build tools, and optionally an IDE or editor. Below are simple, tested steps for each platform.
Option A — Visual Studio (MSVC) — recommended for Windows developers
Download the Visual Studio Community installer from the official site (choose "Desktop development with C++" workload).
Run the installer and select the "Desktop development with C++" workload. You can also add optional components such as CMake, Clang tools, and Windows SDKs.
After install, open Developer Command Prompt for Visual Studio from the Start menu.
Verify MSVC is available:
cl
You should see the MSVC version and usage help. To compile:
cl /EHsc hello.cpp
Option B — MinGW-w64 (GCC) via MSYS2 — lightweight, compatible with many Unix-style workflows
Download and install MSYS2 from https://www.msys2.org.
Open the MSYS2 MinGW 64-bit shell and update packages:
pacman -Syu
# close and re-open the MSYS2 shell when prompted
pacman -Su
Install the mingw-w64 toolchain (64-bit):
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
In the MSYS2 MinGW 64-bit shell, verify:
g++ --version
Compile:
g++ -std=c++17 hello.cpp -o hello.exe
./hello.exe
Option C — WSL (Windows Subsystem for Linux)
Enable WSL and install a Linux distro (Windows 10/11):
wsl --install -d ubuntu
Open the installed distro and install build tools:
sudo apt update && sudo apt install build-essential gdb -y
Verify and compile as on Linux (see below).
Option A — Xcode Command Line Tools (Clang)
Open Terminal and run:
xcode-select --install
Verify compiler:
clang --version
# or
g++ --version # this may show Apple clang
Compile:
clang++ -std=c++17 hello.cpp -o hello
./hello
Option B — Homebrew + GCC (optional if you prefer GNU toolchain)
Install Homebrew (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install GCC:
brew install gcc
Homebrew GCC installs versioned binaries like g++-13 — verify:
g++-13 --version
Compile with the GNU compiler:
g++-13 -std=c++17 hello.cpp -o hello
./hello
(Commands below assume a terminal with sudo privileges.)
Debian / Ubuntu / Linux Mint
sudo apt update
sudo apt install build-essential gdb -y
Verify:
g++ --version
Compile:
g++ -std=c++17 hello.cpp -o hello
./hello
Fedora / Red Hat / CentOS (dnf)
sudo dnf groupinstall "Development Tools" -y
sudo dnf install gcc-c++ gdb -y
Arch Linux
sudo pacman -Syu
sudo pacman -S base-devel gcc gdb
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Compile with a modern standard and warnings enabled:
# Using GCC/Clang
g++ -std=c++17 -Wall -Wextra -O2 hello.cpp -o hello
./hello
# MSVC (in Developer Command Prompt)
cl /EHsc /std:c++17 hello.cpp
Visual Studio (Windows) — full-featured IDE with MSVC.
CLion (cross-platform, commercial) — excellent CMake support.
Visual Studio Code + C/C++ extension (free) — lightweight and configurable.
Eclipse CDT, Code::Blocks — other cross-platform options.
"g++: command not found" — ensure you installed build tools and that the toolchain bin folder is in PATH (Windows MinGW/MSYS2).
MSVC not recognized — use the "Developer Command Prompt for Visual Studio" which sets environment variables.
Permissions / Antivirus — Windows Defender or third‑party AV may block executables created during testing; allow the folder or disable temporarily.
Using the right toolchain on macOS — g++ often points to Apple Clang; use g++-<version> if you installed GNU GCC with Homebrew.
When in doubt, check versions: g++ --version, clang --version, or cl.
Q: Which compiler should I use?
A: For Windows, MSVC (Visual Studio) is common. For cross-platform and Linux/macOS, GCC or Clang are standard. Use the toolchain that matches your deployment target.
Q: How do I use CMake?
A: Install CMake (sudo apt install cmake or brew install cmake or via Visual Studio installer) and create a CMakeLists.txt. Use cmake to configure and cmake --build . to build.
Q: How do I target a specific C++ standard?
A: Use compiler flags: -std=c++17, -std=c++20 (GCC/Clang) or /std:c++17 (MSVC).
Keep your toolchain updated, pick an editor/IDE you like, and practice compiling small programs until you're comfortable with the terminal-based workflow. Happy coding!