How to compile and run C++ on Windows, Mac and Linux

Short answer: Windows → install Visual Studio Community (or MinGW's g++); Mac → one Terminal command installs Apple's clang++; Linux → your package manager installs g++. All three are free. No browser needed — but for quick practice you can always use our in-browser playground instead.

The online playground is perfect for lessons, but sooner or later you need a real compiler on your machine: bigger programs, std::vector and the full standard library, real debugging. Here's the shortest path for each system.

Windows: Visual Studio Community (recommended)

  1. Download Visual Studio 2022 Community (free) from visualstudio.microsoft.com.
  2. In the installer, check the workload "Desktop development with C++". This single checkbox brings the MSVC compiler, the debugger and the Windows SDK.
  3. Install, launch, then File → New Project → Console App (C++). It generates a Hello World with a main ready to go.
  4. Press F5 to compile and run.

Lighter alternative: if you prefer the command line, install MSYS2 and get the actual g++:

pacman -S mingw-w64-ucrt-x86_64-gcc
g++ --version   # verify

Mac: Xcode command-line tools (one command)

You don't need the full Xcode IDE. Open Terminal and run:

xcode-select --install

A dialog pops up; click Install. In a few minutes you have clang++ (Apple's clang, gcc-compatible flags). Test it:

clang++ --version

Linux: one package-manager line

sudo apt install g++        # Debian/Ubuntu
sudo dnf install gcc-c++    # Fedora
g++ --version               # verify

Compile and run from the terminal (every system)

Save this as main.cpp:

#include <iostream>

int main() {
    std::cout << "It compiles!" << std::endl;
    return 0;
}

Then, in the folder where the file lives:

g++ main.cpp -o main        # compile: produces the executable "main" (main.exe on Windows)
./main                      # run it        (Windows: main.exe  or  .\main.exe)

The flags worth adopting from day one:

g++ -std=c++20 -Wall main.cpp -o main
#    └─ modern standard   └─ turn on warnings — always

What actually happens: compile → link → run

That one g++ command secretly does two jobs: the compiler translates your .cpp into machine code (object files), and the linker stitches them together into an executable. This explains the two error families in our compiler errors guide: error: messages come from the compiler (language rules), undefined reference comes from the linker (a function was promised but never delivered).

The setup errors everyone hits

Do I need an IDE?

No — a text editor plus the terminal is a complete setup, and it teaches you what the IDE was doing for you. But a good middle ground: VS Code (free) with the official C/C++ extension — you keep the simple g++ commands above, and get syntax highlighting, click-to-error and debugging on top. Visual Studio (Windows) and Xcode (Mac) remain the "everything included" options.

Which standard should I use?

Practice

  1. Compile and run main.cpp above with -Wall — clean output, no warnings.
  2. Break it on purpose: remove a semicolon, meet error: expected ';', fix it. That error and its friends are decoded in the common errors guide.
  3. Try std::vector and std::string — the full standard library works on your desktop even though our browser playground covers the teaching core only.

Related: start with the Hello World lesson, then what int main() is, and 10 common compiler errors for when the build fails.