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:

  1. Reads your .cpp source code file
  2. Checks for syntax errors
  3. Translates it into machine code that the computer understands
  4. Generates an executable file (.exe on 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.

Method 1: MSYS2 (Recommended)

MSYS2 is a Linux-like environment for Windows that makes it easy to install g++.

Steps:

  1. Download MSYS2 Visit https://www.msys2.org and download the installer (about 50MB)

  2. Install MSYS2 Double-click the installer, keep clicking "Next", and install to the default location C:\msys64

  3. Open the MSYS2 MINGW64 terminal Start Menu → search for "MSYS2 MINGW64" → open it

  4. Install the g++ compiler In the terminal, enter:

BASH
pacman -S mingw-w64-x86_64-gcc

Type Y to confirm and wait for the installation to complete (about 200MB)

  1. Add to system PATH
  1. Verify the installation Open a new CMD or PowerShell and enter:
BASH
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:

  1. Visit https://winlibs.com
  2. Download the "UCRT runtime" version Zip archive (about 50MB)
  3. Extract to C:\mingw64
  4. Add C:\mingw64\bin to the system PATH
  5. Open a new CMD and enter g++ --version to verify

(3) 2.3 Common Issues

Q: Why does g++ --version say "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 be C:\msys64\mingw64\bin or C:\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 use pacman to 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

  1. Open Terminal (Applications → Utilities → Terminal)
  2. Enter the following command:
BASH
xcode-select --install
  1. A dialog will appear — click "Install"
  2. Wait for the installation to complete (about 5-10 minutes)
  3. Verify the installation:
BASH
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

BASH
sudo apt update
sudo apt install g++ -y

(2) 4.2 Fedora / RHEL

BASH
sudo dnf install gcc-c++ -y

(3) 4.3 Arch Linux

BASH
sudo pacman -S gcc

(4) 4.4 Verify the Installation

BASH
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:

CPP
#include <iostream>

int main() {
 std::cout << "Hello, C++ World!" << std::endl;
 return 0;
}
▶ Try it Yourself

Output:

TEXT 📖 Display only
Hello, C++ World!

Save the file.

Step 2: Compile

Open a terminal, navigate to the file's directory, and enter:

BASH
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

BASH
.\hello.exe
BASH
./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:

CPP
#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;
}
▶ Try it Yourself

Output:

TEXT 📖 Display only
Please enter your name: 
Hello, ! Welcome to C++!

Step 2: Compile

BASH
g++ greeting.cpp -o greeting

Step 3: Run

Run result:

TEXT 📖 Display only
Please enter your name: MOTO
Hello,MOTO!Welcome to learn C++!

💡 Tip: This program introduces two new concepts:

  1. #include <string> — includes the string library
  2. std::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:

CPP
#include <iostream>

int main() {
 std::cout << "First compilation" << std::endl
 return 0; // ❌ Missing semicolon at the end of the previous line
}
▶ Try it Yourself

Output:

TEXT 📖 Display only
First compilation

Try compiling:

BASH
g++ bug.cpp -o bug

The compiler will report an error:

TEXT 📖 Display only
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:

  1. Missing semicolons — Every statement must end with ;
  2. Mismatched brackets{ and } must come in pairs
  3. Typos — Writing cout as cout (with an extra space)
💡 Tip: Compiler errors aren't scary! Google the error message — 99% of C++ compilation errors have been encountered by others. Copy the error into a search engine and you'll find the answer right away.



While you can write C++ with Notepad, a good editor can dramatically boost your productivity.

VS Code (Visual Studio Code) is a free editor from Microsoft — lightweight, powerful, and cross-platform.

Installation steps:

  1. Visit https://code.visualstudio.com, download and install
  2. Open VS Code, click the "Extensions" icon on the left (or press Ctrl+Shift+X)
  3. Search for and install the following extensions:

Configuring the C++ environment:

  1. Create a folder (e.g., cpp-learning)
  2. Open the folder in VS Code
  3. Create a new file hello.cpp and enter your code
  4. Press `Ctrl+`` (backtick) to open the terminal
  5. Enter g++ hello.cpp -o hello && ./hello to 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

Q What if compilation says "iostream: No such file or directory"?
A This means the compiler isn't properly installed or the PATH is misconfigured. Check in order: ① Run g++ --version in the terminal to see if there's a version number; ② No version number → reconfigure PATH; ③ Still not working → reinstall the compiler.
Q Why does my program compile successfully but show nothing when run?
A Two common scenarios: ① On Windows, double-clicking the .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.
Q What's the difference between g++ and clang? Which should I use?
A Both are excellent C++ compilers that fully support the C++ standard. g++ is from the GNU project, default on Linux, with detailed error messages; clang is led by Apple, compiles faster, with friendlier error messages. Beginners don't need to worry — on Windows, g++ (MinGW-w64) is recommended; on macOS, just use the built-in clang.
Q Do I need to install Visual Studio (full version)?
A No! The full Visual Studio IDE is about 10GB, suitable for large projects. For learning, VS Code + g++ is more than enough — lightweight and free.

📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Install the g++ compiler on your computer and verify the installation by entering g++ --version in the terminal. Take a screenshot and show me!

  2. Intermediate (Difficulty ⭐⭐): Use VS Code to create a hello.cpp file, 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.

  3. Challenge (Difficulty ⭐⭐⭐): Write a program that:

  4. Outputs your name and age

  5. Outputs the current date (hint: look up how to get the date in C++, or just use a fixed string for now)

  6. 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!

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏