C++: Development Environment Setup
Last updated: 2026-08-26
In lesson 01, we learned about C++ and how it's a compiled language — source code must be translated into machine code by a compiler before it can run.
It's like talking to a foreigner — you need a translator (compiler) first. In this lesson, we'll install that "translator" on your computer.
1. Why Do You Need a Development Environment?
(1) 1.1 Compiled vs Interpreted Languages
| Type | Representative Languages | How They Run | Characteristics |
|---|---|---|---|
| Compiled | C, C++, Rust | Source code → Compiler → Executable → Run directly | Fast execution, slow compilation |
| Interpreted | Python, JavaScript | Source code → Interpreter → Run while interpreting | Fast development, slow execution |
C++ is a compiled language, so you must install a compiler first.
(2) 1.2 What Is a Compiler?
A compiler is a program that:
- Reads your
.cppsource code file - Checks for syntax errors
- Translates it into machine code that the computer understands
- Generates an executable file (
.exeon Windows, no extension on macOS/Linux)
The most commonly used C++ compiler is g++ (part of the GNU Compiler Collection) — it's free, powerful, and cross-platform.
2. Windows: Installing MinGW-w64
(1) 2.1 What Is MinGW-w64?
MinGW-w64 (Minimalist GNU for Windows) is a project that provides the GNU toolchain on Windows, which includes the g++ compiler.
(2) 2.2 Installation Steps (Recommended Method)
Method 1: MSYS2 (Recommended)
MSYS2 is a Linux-like environment for Windows that makes it easy to install g++.
Steps:
-
Download MSYS2 Visit https://www.msys2.org and download the installer (about 50MB)
-
Install MSYS2 Double-click the installer, keep clicking "Next", and install to the default location
C:\msys64 -
Open the MSYS2 MINGW64 terminal Start Menu → search for "MSYS2 MINGW64" → open it
-
Install the g++ compiler In the terminal, enter:
pacman -S mingw-w64-x86_64-gcc
Type Y to confirm and wait for the installation to complete (about 200MB)
- Add to system PATH
- Right-click "This PC" → Properties → Advanced system settings → Environment Variables
- Find
Pathunder "System variables" → Edit → New - Add:
C:\msys64\mingw64\bin - Click OK to save
- Verify the installation Open a new CMD or PowerShell and enter:
g++ --version
If you see a version number (e.g., g++ (Rev10, Built by MSYS2 project) 13.2.0), the installation was successful!
Method 2: Download MinGW-w64 directly (offline version)
If you don't want to install MSYS2, you can download a precompiled MinGW-w64:
- Visit https://winlibs.com
- Download the "UCRT runtime" version Zip archive (about 50MB)
- Extract to
C:\mingw64 - Add
C:\mingw64\binto the system PATH - Open a new CMD and enter
g++ --versionto verify
(3) 2.3 Common Issues
Q: Why does
g++ --versionsay "not recognized as an internal or external command"? A: Three possible reasons: > 1. Not added to PATH (most common) > 2. Didn't restart the terminal after adding PATH (CMD/PowerShell need to be restarted to recognize new PATH) > 3. Wrong path (should beC:\msys64\mingw64\binorC:\mingw64\bin) Q: What's the difference between MSYS2 and MinGW-w64? A: MSYS2 is a complete Linux-like environment, while MinGW-w64 is a toolchain within it. Installing MSYS2 lets you easily usepacmanto install various development tools. Recommended for beginners.
3. macOS: Installing Xcode Command Line Tools
The easiest way to install on macOS is using Xcode Command Line Tools (which includes g++).
(1) 3.1 Installation Steps
- Open Terminal (Applications → Utilities → Terminal)
- Enter the following command:
xcode-select --install
- A dialog will appear — click "Install"
- Wait for the installation to complete (about 5-10 minutes)
- Verify the installation:
g++ --version
If you see Apple clang version X.X.X, the installation was successful!
💡 Tip: On macOS, g++ is actually an alias for clang (Apple's own compiler). It's fully compatible with the C++ standard, so feel free to use it.
4. Linux: Installing g++
Most Linux distributions already have g++ preinstalled, or you can install it with one command using the package manager.
(1) 4.1 Ubuntu / Debian
sudo apt update
sudo apt install g++ -y
(2) 4.2 Fedora / RHEL
sudo dnf install gcc-c++ -y
(3) 4.3 Arch Linux
sudo pacman -S gcc
(4) 4.4 Verify the Installation
g++ --version
5. Compiling and Running Your First C++ Program
With the compiler installed, let's compile and run the "Hello World" program from lesson 01.
▶ Example 1: Compile and run a program with g++ (Difficulty ⭐)
Step 1: Create the source code file
Using any text editor, create a file called hello.cpp with the following content:
#include <iostream>
int main() {
std::cout << "Hello, C++ World!" << std::endl;
return 0;
}
Output:
Hello, C++ World!
Save the file.
Step 2: Compile
Open a terminal, navigate to the file's directory, and enter:
g++ hello.cpp -o hello
| Parameter | Meaning |
|---|---|
hello.cpp |
Source code file |
-o hello |
Specify the output executable name (.exe is automatically appended on Windows) |
If compilation succeeds, it will generate a hello (macOS/Linux) or hello.exe (Windows) file.
Step 3: Run
- Windows:
.\hello.exe
- macOS / Linux:
./hello
🎉 Congratulations! You've successfully compiled and run your first C++ program!
▶ Example 2: Compile and run a program with input (Difficulty ⭐⭐)
Let's write a slightly more complex program to practice the full compile-and-run workflow.
Step 1: Create the source code file
Create greeting.cpp with the following content:
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Please enter your name: ";
std::cin >> name;
std::cout << "Hello," << name << "!Welcome to learn C++!" << std::endl;
return 0;
}
Output:
Please enter your name:
Hello, ! Welcome to C++!
Step 2: Compile
g++ greeting.cpp -o greeting
Step 3: Run
- Windows:
.\greeting.exe - macOS / Linux:
./greeting
Run result:
Please enter your name: MOTO
Hello,MOTO!Welcome to learn C++!
💡 Tip: This program introduces two new concepts:
#include <string>— includes the string librarystd::cin >> name— reads input from the keyboard
Don't worry, lesson 03 will explain these in detail!
▶ Example 3: What to do when compilation errors occur (Difficulty ⭐⭐)
Beginners will definitely encounter errors the first time they compile. Let's learn how to "read" errors first.
Write a program with a syntax error called bug.cpp:
#include <iostream>
int main() {
std::cout << "First compilation" << std::endl
return 0; // ❌ Missing semicolon at the end of the previous line
}
Output:
First compilation
Try compiling:
g++ bug.cpp -o bug
The compiler will report an error:
bug.cpp:5:5: error: expected ';' before 'return'
return 0;
^~~~~~
Three key elements of a compiler error message:
| Element | Example | Meaning |
|---|---|---|
| File name | bug.cpp |
Which file has the error |
| Line number | :5:5 |
Line 5, character 5 |
| Error message | expected ';' before 'return' |
The compiler expected a semicolon but didn't find one before return |
The three most common compilation errors for beginners:
- Missing semicolons — Every statement must end with
; - Mismatched brackets —
{and}must come in pairs - Typos — Writing
coutascout(with an extra space)
6. Recommended IDE / Editor
While you can write C++ with Notepad, a good editor can dramatically boost your productivity.
(1) 6.1 VS Code (Highly Recommended)
VS Code (Visual Studio Code) is a free editor from Microsoft — lightweight, powerful, and cross-platform.
Installation steps:
- Visit https://code.visualstudio.com, download and install
- Open VS Code, click the "Extensions" icon on the left (or press
Ctrl+Shift+X) - Search for and install the following extensions:
- C/C++ (Microsoft official, essential)
- C/C++ Extension Pack (includes debugging tools, recommended)
- Code Runner (one-click run, optional)
Configuring the C++ environment:
- Create a folder (e.g.,
cpp-learning) - Open the folder in VS Code
- Create a new file
hello.cppand enter your code - Press `Ctrl+`` (backtick) to open the terminal
- Enter
g++ hello.cpp -o hello && ./helloto run
💡 Tip: After installing the Code Runner extension, you can press Ctrl+Alt+N to run the current program directly without manually entering commands.
(2) 6.2 Other Options
| Editor | Platform | Features | Rating |
|---|---|---|---|
| VS Code | Win/Mac/Linux | Lightweight, free, rich plugins | ⭐⭐⭐⭐⭐ |
| CLion | Win/Mac/Linux | By JetBrains, powerful but paid | ⭐⭐⭐⭐ |
| Dev-C++ | Windows | Classic, lightweight, but no longer updated | ⭐⭐⭐ |
| Xcode | macOS | Apple's official IDE, for macOS/iOS apps | ⭐⭐⭐⭐ |
Beginner recommendation: VS Code + C/C++ extension — free and powerful enough.
❓ FAQ
g++ --version in the terminal to see if there's a version number; ② No version number → reconfigure PATH; ③ Still not working → reinstall the compiler..exe causes a flash and close (normal — the program finishes and the terminal closes automatically). Run it from CMD/PowerShell instead; ② The program is in an infinite loop — check if the loop condition is always true.📖 Summary
- C++ is a compiled language and requires a compiler (g++ or clang)
- Windows: Recommended to install MinGW-w64 via MSYS2
- macOS: Install Xcode Command Line Tools (
xcode-select --install) - Linux: Install g++ via the package manager (e.g.,
apt install g++) - Compile command:
g++ source.cpp -o output_name - Recommended editor: VS Code + C/C++ extension
📝 Exercises
-
Basic (Difficulty ⭐): Install the g++ compiler on your computer and verify the installation by entering
g++ --versionin the terminal. Take a screenshot and show me! -
Intermediate (Difficulty ⭐⭐): Use VS Code to create a
hello.cppfile, write a "Hello World" program, and successfully compile and run it. Try modifying the output, for example changing it to "Hello, C++ World!", then recompile and run. -
Challenge (Difficulty ⭐⭐⭐): Write a program that:
-
Outputs your name and age
-
Outputs the current date (hint: look up how to get the date in C++, or just use a fixed string for now)
-
Compile and run it, and take a screenshot of the result
7. 🚀 Next Steps
With the environment set up, next we'll learn about variables and basic data types in C++ (lesson 03), starting the real programming journey!