How to Set Up C++: Install a Compiler and Write Your First Program
Before you can write C++, you need two things: a compiler (which turns your .cpp source file into a runnable program) and a code editor to write in. This guide walks you through the full setup on Windows, Mac, and Linux — and gets you to a working “Hello, World!” in under 15 minutes.
Option 0: Try It Online First (No Install Needed)
If you just want to experiment before committing to a local setup, use an online compiler:
- onlinegdb.com — full compiler with debugger, no account needed
- godbolt.org — shows you assembly output alongside your C++ code, great for learning
These are fine for learning and small snippets. When you’re ready to build real projects, come back and do the local setup below.
Setup on Windows
Step 1: Install the Compiler (MinGW-w64)
Windows doesn’t come with a C++ compiler. The most straightforward option for beginners is MinGW-w64, which gives you g++ (the GCC compiler for Windows).
-
Go to winlibs.com and download the latest GCC release for Windows (choose the
x86_64UCRT version,.zipformat) -
Extract the zip to
C:\mingw64 -
Add
C:\mingw64\binto your system PATH:- Search for “Environment Variables” in the Start menu
- Click “Environment Variables”
- Under “System variables”, find
Path, click Edit - Click New and add
C:\mingw64\bin - Click OK on all dialogs
-
Open a new Command Prompt and verify it worked:
g++ --version
You should see something like g++ (GCC) 13.x.x.
Step 2: Install VS Code
- Download VS Code and install it
- Open VS Code, go to Extensions (
Ctrl+Shift+X), search for C/C++ and install the Microsoft extension - Optionally install C/C++ Extension Pack for extra features
Step 3: Write and Run Your First Program
- Create a new folder called
cpp-projectssomewhere on your computer - Open VS Code, click File → Open Folder, and open that folder
- Create a new file called
hello.cpp - Paste this code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
- Open the terminal in VS Code (
Ctrl+`````) - Compile and run:
g++ hello.cpp -o hello
./hello
You should see Hello, World! printed in the terminal. You’re up and running.
Setup on Mac
Step 1: Install the Compiler (Xcode Command Line Tools)
Mac comes with Apple’s clang compiler, which is fully compatible with modern C++. Install it via the Command Line Tools package:
- Open Terminal (search for it in Spotlight with
Cmd+Space) - Run:
xcode-select --install
- A dialog will appear — click Install and wait for it to finish (takes a few minutes)
- Verify the install:
g++ --version
On Mac, g++ is aliased to clang++. Either command works.
Step 2: Install VS Code
- Download VS Code and drag it to your Applications folder
- Open VS Code, go to Extensions (
Cmd+Shift+X), search for C/C++ and install the Microsoft extension
Step 3: Write and Run Your First Program
- Create a folder called
cpp-projectsin your home directory - Open VS Code and open that folder (File → Open Folder)
- Create
hello.cppand paste:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
- Open the integrated terminal (
Ctrl+`````) and run:
g++ hello.cpp -o hello
./hello
Hello, World! should appear. You’re ready to code.
Setup on Linux
Linux is the most straightforward platform for C++ development.
Step 1: Install g++
Open a terminal and run the appropriate command for your distribution:
Ubuntu / Debian / Linux Mint:
sudo apt update && sudo apt install g++ build-essential
Fedora / RHEL:
sudo dnf install gcc-c++ make
Arch Linux:
sudo pacman -S gcc base-devel
Verify:
g++ --version
Step 2: Install VS Code (optional)
VS Code is available for Linux via code.visualstudio.com. Install the C/C++ extension as described above. Alternatively, any text editor works — nano, vim, gedit, etc.
Step 3: Write and Run
// hello.cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
g++ hello.cpp -o hello
./hello
Understanding What Just Happened
When you ran g++ hello.cpp -o hello, here’s what the compiler did:
- Preprocessed your file — expanded
#include <iostream>to pull in the iostream header - Compiled your
.cppsource into an object file - Linked it with the standard library to produce the final executable
hello
The -o hello flag tells the compiler what to name the output file. Without it, the output is called a.out on Mac/Linux or a.exe on Windows.
Useful Compiler Flags for Beginners
As you start writing more code, these flags are worth knowing:
# Enable warnings — catches common mistakes
g++ hello.cpp -o hello -Wall -Wextra
# Compile for C++17 (recommended modern standard)
g++ hello.cpp -o hello -std=c++17
# Both together (recommended default)
g++ hello.cpp -o hello -std=c++17 -Wall -Wextra
Adding -Wall -Wextra is particularly valuable when learning — the compiler will warn you about things that aren’t errors but are often bugs.
Next Steps
Now that your environment is working, the logical next step is understanding exactly what every line of that Hello World program means. The C++ Hello World explained line by line breaks it down completely.
Or if you want the full structured path from here, follow the C++ learning roadmap →