Setting Up a C++ Compiler
To run C++ you need a compiler that turns source code into an executable. This lesson shows how to install GCC, Clang or Visual Studio, compile from the terminal with the right options, and pick an editor.
The main compilers
| Compiler | Command | Typical platform |
|---|---|---|
| GCC | g++ | Linux; also Windows via MSYS2 or WSL |
| Clang | clang++ | macOS (default); Linux; Windows |
| MSVC | cl | Windows, through Visual Studio |
On most Unix-like systems the command c++ runs the default C++ compiler. Note the ++: compiling C++ with gcc or clang (the C drivers) leaves out the C++ standard library and gives confusing link errors.
Installing
Linux
# Debian, Ubuntu sudo apt update sudo apt install build-essential # g++, make and friends # Fedora sudo dnf install gcc-c++ make
macOS
xcode-select --install # Apple Clang
Windows
- Visual Studio Community with the "Desktop development with C++" workload — a full IDE and compiler, the most common choice on Windows.
- WSL — run
wsl --install, then follow the Linux steps for exactly the environment used in this course. - MSYS2 — provides
g++and a Unix-style terminal for native Windows programs.
Check the version
g++ --version clang++ --version
This course's C++20 code needs GCC 11 or newer, Clang 16 or newer, or Visual Studio 2022. The C++23 lesson needs GCC 14 or Clang 18 for features such as std::print.
Compiling and running
Save this as hello.cpp:
#include <iostream> int main() { std::cout << "Hello from my own computer!\n"; }
g++ -std=c++20 -Wall -Wextra -pedantic hello.cpp -o hello ./hello
Hello from my own computer!
| Option | Meaning |
|---|---|
-std=c++20 | Use the C++20 standard (some compilers still default to C++17) |
-Wall -Wextra -pedantic | Enable the important warnings |
-o hello | Name the executable |
-g | Include debugging information |
-O2 | Optimise for speed (for releases) |
With MSVC, the equivalent is cl /std:c++20 /W4 /EHsc hello.cpp in a "Developer Command Prompt".
Editors and IDEs
- Visual Studio Code with the C/C++ or clangd extension — light and popular.
- CLion — a full IDE built around CMake (lesson 49).
- Visual Studio — the standard IDE on Windows, with an excellent debugger.
- Xcode on macOS, Qt Creator, or any editor plus a terminal.
Online compilers
Compiler Explorer (godbolt.org) compiles and runs C++ in the browser with any compiler version — great for quick experiments and for seeing the machine code your C++ turns into. Add -std=c++20 to the compiler options.
Try it yourself
Install a compiler, save this program as main.cpp, and compile it with warnings on.
7 apples shared by 3 friends: 2 each, 1 left over