Skip to main content
C++ Fundamentals for Beginners to Advanced
CHAPTER 02 Beginner

Installing C++ Compiler and Setup

Updated: May 17, 2026
5 min read

# CHAPTER 2

Installing C++ Compiler and Setup

1. Introduction

Unlike JavaScript or Python which can run in a browser or an interpreter, C++ is a compiled language. Your computer cannot understand C++ code natively. You must use a Compiler to translate your C++ source code into binary machine code (an .exe file on Windows).

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the role of the g++ compiler.
  • Install MinGW (GCC/G++) on Windows.
  • Configure Environment Variables.
  • Install and set up Visual Studio Code for C++.
  • Compile and run a basic C++ program using the terminal.

3. What is a Compiler?

A compiler takes your human-readable code and converts it into computer-readable instructions.
1
[Source Code (main.cpp)] ---> [COMPILER (g++)] ---> [Executable (main.exe)] ---> OS runs it

4. Installing the Compiler (MinGW on Windows)

Windows does not have a native C++ compiler installed by default. We will use MinGW-w64, which provides the GNU Compiler Collection (GCC).

Steps:

  1. 1. Download MSYS2 from msys2.org.
  1. 2. Install it and open the MSYS2 terminal.
  1. 3. Run this command to install the GCC toolchain:

bash
123456789101112
   pacman -S mingw-w64-ucrt-x86_64-gcc
   ```
4. Now, you must add the compiler to your Windows **PATH**.
   - Search for "Environment Variables" in the Windows start menu.
   - Click "Edit the system environment variables".
   - Find the `Path` variable, click Edit, and add: `C:\msys64\ucrt64\bin`.
   - Click OK and save.

*(Note: Mac users can simply run `xcode-select --install` in the terminal to get the Clang compiler. Linux users can run `sudo apt install g++`)*.

### 5. Verifying the Installation
Open Command Prompt (cmd) or PowerShell and type:

bash g++ --version

123456789101112131415
If you see a version number (like `g++ (GCC) 13.1.0`), your compiler is ready!

### 6. Setting up the IDE (VS Code)
An IDE (Integrated Development Environment) makes writing code much easier. **Visual Studio Code** is the industry standard.

1. Download and install VS Code from `code.visualstudio.com`.
2. Open VS Code, go to Extensions (Ctrl+Shift+X).
3. Search for and install the **"C/C++"** extension by Microsoft.
4. Optional: Install the **"Code Runner"** extension for a quick "Play" button to run code.

*(Alternatives: Visual Studio Community (heavy but powerful), CLion (paid), Code::Blocks).*

### 7. Compiling and Running Manually
Create a file named `hello.cpp` and type the following:

cpp #include <iostream>

int main() { std::cout << "Compiler is working!" << std::endl; return 0; }

1
Open the terminal in VS Code (Terminal -> New Terminal) and run:

bash # Compile the code (-o names the output file) g++ hello.cpp -o hello.exe

# Run the executable .\hello.exe `` *Output:* Compiler is working!

8. Common Mistakes

  • Using gcc instead of g++: gcc is for C. g++ is for C++. If you use gcc on a C++ file, it won't link the standard C++ libraries correctly.
  • Forgetting to update PATH: If the terminal says 'g++' is not recognized, your PATH environment variable is incorrect.
  • Spaces in file paths: Save your code in folders without spaces (e.g., C:\CppProjects\) to avoid terminal errors.

9. Best Practices

  • Keep your workspace organized. Create a dedicated folder for all your C++ projects.
  • Always read the compiler error messages. They tell you exactly which line failed.

10. Exercises

  1. 1. Verify your g++ installation in the terminal.
  1. 2. Install VS Code and the C/C++ extension.
  1. 3. Write a program that prints your name and age, then compile it manually via the terminal.

11. MCQ Quiz with Answers

Question 1

What is the standard compiler command for C++?

Question 2

Which software package provides GCC for Windows?

Question 3

What does compiling a C++ program do?

Question 4

What is the correct terminal command to compile main.cpp into app.exe?

Question 5

If the terminal says g++ is not recognized, what is the likely issue?

Question 6

What file extension is used for C++ source files?

Q7. Is VS Code a compiler? a) Yes b) No Answer: b) No *Explanation:* VS Code is a text editor. It relies on external compilers like MinGW to actually build the code.
Question 8

What command runs an executable named app.exe in Windows PowerShell?

Question 9

Which extension by Microsoft is essential for writing C++ in VS Code?

Q10. Can you compile C++ without an IDE like VS Code? a) Yes, using just Notepad and a terminal b) No, an IDE is required Answer: a) Yes, using just Notepad and a terminal *Explanation:* IDEs are just helpful tools; the compiler does the actual work.

12. Interview Questions

  • Q: Explain the C++ build process (Preprocessing -> Compiling -> Assembling -> Linking).
  • Q: What is the difference between an IDE and a Compiler?

13. FAQs

Q: Do I need a powerful computer to write C++? A: No! The compiler is very lightweight. Any modern laptop can handle basic C++ programming.

14. Summary

To program in C++, you need a compiler (like
g++ via MinGW) and a text editor (like VS Code). Compiling code translates it from human-readable .cpp files into computer-readable .exe` files.

15. Next Chapter Recommendation

With your environment ready, in Chapter 3: C++ Syntax and First Program, we will break down the structure of a C++ program line-by-line.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·