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)
- Download Visual Studio 2022 Community (free) from visualstudio.microsoft.com.
- In the installer, check the workload "Desktop development with C++". This single checkbox brings the MSVC compiler, the debugger and the Windows SDK.
- Install, launch, then File → New Project → Console App (C++). It generates a
Hello Worldwith amainready to go. - 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
'g++' is not recognized/command not found— the compiler installed, but the terminal can't find it. On Windows this is almost always the PATH: MSYS2'sucrt64\binfolder must be in your PATH variable, and you must open a new terminal after changing it.g++: error: main.cpp: No such file or directory— you're in the wrong folder.cdinto the directory where the file actually is (or give the full path).- Program runs then instantly closes on Windows — the console window closes when
mainreturns. Run it from a terminal instead of double-clicking, or set a breakpoint; avoid the oldsystem("pause")hack. - Antivirus flags your freshly built
main.exe— harmless unsigned executables sometimes trip heuristics; add an exclusion for your projects folder.
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?
- Learning in 2026:
-std=c++20— modern features, universally supported by current compilers. - Following old tutorials: they usually mean C++11/14; anything from C++11 onward will run them.
- Job interviews: know C++11 core features (auto, range-for, smart pointers, lambdas); C++17/20 are increasingly expected.
Practice
- Compile and run
main.cppabove with-Wall— clean output, no warnings. - Break it on purpose: remove a semicolon, meet
error: expected ';', fix it. That error and its friends are decoded in the common errors guide. - Try
std::vectorandstd::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.