Writing and running C++ code needs two things: a compiler that turns source code into a runnable program, and a text editor to type code in. This lesson walks through installing both on macOS, Linux, and Windows, then verifying everything works by compiling a one-line program.
C++ is a compiled language. Source code goes into a .cpp file, the compiler reads it, and the compiler produces an executable file. That is the whole pipeline at the highest level.
That is all the setup is. Get a compiler, get an editor, write a file, compile, run. An IDE (Integrated Development Environment) bundles all of this into one application with extras like autocompletion and a built-in debugger. It is not required to start, but it is common.
Three compilers do most of the work in the C++ world. They all read the same C++ standard, so code written for one usually compiles on the others, but each has a different home turf.
| Compiler | Native Platform | Install Method | Maintained By |
|---|---|---|---|
| GCC (g++) | Linux | Package manager (apt, dnf), MSYS2 on Windows | GNU Project / Free Software Foundation |
| Clang (clang++) | macOS | Xcode Command Line Tools, package managers | LLVM Project (used by Apple, Google) |
| MSVC | Windows | Visual Studio installer | Microsoft |
No single favorite is required. Use whichever compiler is native to the OS. On macOS, Clang is typical; on Linux, GCC; on Windows, MSVC, MinGW (which packages GCC), or Linux tools through WSL.
For this course, any of the three works. Examples show g++ commands because they are the most common, but clang++ on macOS accepts the same flags.
On macOS, the fastest path is the Xcode Command Line Tools. They include Clang, the linker, headers, and a few related utilities. The full multi-gigabyte Xcode app is not required for this course; the Command Line Tools alone are enough.
Open the Terminal app and run:
A dialog pops up asking whether to install the tools. Click Install and wait. The download is around 500 MB.
When it finishes, clang++ is available. For GCC (some projects assume it), install it through Homebrew:
Homebrew installs GCC alongside Clang, but it names the binary with a version suffix to avoid clashing with the system g++. The command is g++-14 instead of plain g++. This is a Homebrew quirk, not a GCC one.
On Linux, the package manager does all the work. The package name varies by distribution.
Ubuntu and Debian:
The build-essential meta-package pulls in g++, gcc, make, and the C/C++ standard library headers. It is the canonical "build C and C++ code" install on Debian-based systems.
Fedora, RHEL, and CentOS Stream:
Arch Linux:
For Clang on Linux, every major distribution packages it. On Ubuntu the command is sudo apt install clang. Both can be installed side by side; they do not interfere with each other.
Windows has more options than macOS or Linux, and the right one depends on the use case.
Option 1: Visual Studio (recommended for IDE users). Microsoft's full IDE comes with the MSVC compiler. Download Visual Studio Community (the free edition) from . In the installer, pick the Desktop development with C++ workload. This installs MSVC, the Windows SDK, and the IDE.
Option 2: MSYS2 (recommended for terminal users). MSYS2 provides a Unix-like shell on Windows and ships GCC through its package manager. Download it from , run the installer, then in the MSYS2 terminal:
This installs g++ and the make build tool. After installation, add C:\msys64\ucrt64\bin to the Windows PATH so g++ works from any terminal.
Option 3: WSL (recommended for a Linux workflow). Windows Subsystem for Linux runs a real Linux environment inside Windows. Open PowerShell as administrator and run:
Once WSL installs (it ships Ubuntu by default), open the Ubuntu app and follow the Linux instructions above. Code, compile, and run inside the Linux environment. Files can still be edited from Windows-side tools like VS Code through the WSL extension.
The install is not complete until the terminal proves it. Open a fresh terminal (close any windows that were already open before the install) and run:
Output (Linux with GCC):
On macOS, the same command often works because g++ is wired to Clang, but the more accurate check is:
Output (macOS):
If either command prints a version number, the compiler is on the PATH and ready. If it prints command not found, the install did not complete or the terminal still has the old environment loaded. The Troubleshooting section at the end of this lesson covers the fix.
C++ can be written in any text editor. A useful editor highlights syntax, autocompletes, and shows errors as code is typed. Four options cover most needs.
| Editor / IDE | Cost | Platforms | Best For |
|---|---|---|---|
| VS Code + C/C++ extension | Free | macOS, Linux, Windows | Beginners. Lightweight, cross-platform, huge extension ecosystem. |
| CLion | Paid (free for students) | macOS, Linux, Windows | Heavy C++ work. Strong refactoring, built-in CMake support. |
| Visual Studio | Free (Community edition) | Windows | Windows-only projects, MSVC users. |
| Vim / Neovim / Emacs | Free | macOS, Linux, Windows | Terminal-first developers who prefer keyboard-driven workflows. |
For this course, VS Code with Microsoft's C/C++ extension is the recommended starting point. It runs on every OS, costs nothing, and the setup is one extension install away. Open VS Code, click the Extensions icon in the sidebar, search for "C/C++" by Microsoft, and install.
CLion is excellent but costs money once the student license runs out. Visual Studio is the standard choice on Windows for Windows-only work. Vim and similar editors work well once configured, but the configuration alone can take a weekend.
Time to prove the whole chain works. Create a folder somewhere on the machine, say cpp-course, and inside it make a file called hello.cpp:
The meaning of every line is covered later. For now, save the file and open a terminal in that folder.
Compile the file:
This command does three things. It tells the compiler to use the C++17 standard (-std=c++17), it reads the source file (hello.cpp), and it writes the compiled program to a file named hello (-o hello). If the command finishes with no output, the compile succeeded. Compilers are quiet on success and loud on failure.
Now run the program:
On Windows in PowerShell, the run command is .\hello.exe instead of ./hello, because Windows uses a different path separator and adds the .exe extension automatically.
The -std=c++17 flag tells the compiler which version of the C++ language to use. C++ has been updated several times since 1998, and each version adds new features. Without the flag, the compiler picks a default standard, and that default differs between compiler versions. Pinning it explicitly means the code compiles the same way for everyone.
Common values:
| Flag | Standard | Year |
|---|---|---|
-std=c++11 | C++11 | 2011 |
-std=c++14 | C++14 | 2014 |
-std=c++17 | C++17 | 2017 |
-std=c++20 | C++20 | 2020 |
-std=c++23 | C++23 | 2023 |
This course uses -std=c++17 as the baseline. C++17 is widely supported across compilers and provides most of the modern features used in later lessons. When a topic specifically requires C++20 (concepts, ranges, coroutines), that lesson notes it.
A few flags are worth knowing. They are not needed for "hello world", but they appear in real projects.
| Flag | What It Does |
|---|---|
-Wall | Turns on most common warnings. Catches typos, unused variables, and likely bugs. |
-Wextra | Turns on extra warnings beyond -Wall. More noise, more catches. |
-g | Includes debug information so you can step through the program in a debugger. |
-O2 | Optimizes the code for speed. Use for release builds, not while debugging. |
-std=c++17 | Picks the C++ standard the compiler should follow. |
A reasonable command line during development looks like this:
The standard is pinned, warnings turned on so the compiler points out anything suspicious, and debug info baked in so a debugger can map machine instructions back to source lines. For a release build, swap -g for -O2.
-O2 typically makes the program two to three times faster than the default (-O0), but compilation itself takes longer and stepping through the optimized code in a debugger gets confusing because the compiler reorders and removes instructions. Use -O0 -g while debugging, -O2 when shipping.
For sharing a snippet or running C++ without installing anything, online compilers run C++ in the browser.
| Service | URL | Best For |
|---|---|---|
| Compiler Explorer (godbolt) | https://godbolt.org | Seeing the assembly your code compiles to. Picks any compiler and version. |
| Wandbox | https://wandbox.org | Quick experiments with multiple C++ standards and compilers. |
| Coliru | https://coliru.stacked-crooked.com | Sharing a runnable link. Minimal UI, fast feedback. |
Use these to try a one-off snippet, share code in an interview prep group, or check whether a feature works on a specific compiler version. They are not a replacement for a local setup once files include multiple sources or read input from disk, but for testing language features they are useful.
Three problems hit nearly everyone the first time. Knowing the shape of each one saves an hour of confusion.
Problem 1: `command not found: g++` (or `clang++`).
The installer ran, but a fresh g++ --version says the command does not exist. Two likely causes.
First, the terminal session was opened before the install finished, so it still has the old PATH. Close the terminal and open a new one.
Second, the compiler is not on the PATH at all. On Windows with MSYS2, this usually means the step of adding C:\msys64\ucrt64\bin to the system PATH was skipped. Open the Windows environment variables UI, add it, and open a new terminal. On macOS, xcode-select --install should handle the PATH automatically, but if it did not, run xcode-select -p to confirm the tools are installed.
Problem 2: `g++: error: hello.cpp: No such file or directory`.
The compiler cannot find the file. The fix is almost always one of two things.
Either the command was run from the wrong directory (run pwd on macOS/Linux or cd on Windows to see the current location, then cd into the folder that contains hello.cpp), or the filename was mistyped. C++ source files end in .cpp (or sometimes .cc or .cxx), and the casing matters on Linux and macOS but not on Windows.
Problem 3: `./hello: command not found` after a successful compile.
The compile worked but running the program fails. On macOS and Linux, this usually means the ./ prefix was missing. Typing just hello makes the shell search the PATH for a command named hello, which does not exist. The ./ tells the shell "look in the current directory."
On Windows in PowerShell, the equivalent is .\hello.exe. PowerShell intentionally refuses to run executables from the current directory without that prefix, for security reasons.