Beginner6 min readLesson 2 of 50
Step 1 — Install a C++ Compiler
A compiler converts your C++ source code into an executable program. The most common compilers are:
| Compiler | Platform | Notes |
| GCC (g++) | Linux / macOS / Windows | Free, open-source, most widely used |
| Clang | macOS / Linux / Windows | Fast, excellent error messages |
| MSVC | Windows | Built into Visual Studio |
Install on Windows (MinGW/GCC)
- Download MSYS2 from msys2.org.
- Open the MSYS2 terminal and run:
pacman -S mingw-w64-x86_64-gcc - Add
C:\msys64\mingw64\bin to your PATH environment variable. - Verify: open Command Prompt and type
g++ --version
Install on macOS
xcode-select --install
This installs Clang (Apple's C++ compiler). Verify with clang++ --version.
Install on Linux (Ubuntu/Debian)
sudo apt update
sudo apt install g++
Step 2 — Choose an IDE or Editor
| IDE / Editor | Best For |
| Visual Studio Code | Lightweight, cross-platform, great extensions |
| Visual Studio | Windows, full-featured, built-in MSVC |
| CLion | Professional C++ development (paid) |
| Code::Blocks | Beginners, lightweight, free |
| Online: cpp.sh | No install needed, browser-based |
Step 3 — Compile and Run Your First Program
Create a file named hello.cpp with the following code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Open your terminal, navigate to the file location, then compile and run:
g++ hello.cpp -o hello
./hello
Output:
Hello, World!
Note: The -o hello flag names the output executable hello. On Windows the output will be hello.exe.
Exercise
Create a file myprogram.cpp that prints C++ is awesome! and compile it using g++.
Show Solution
#include <iostream>
using namespace std;
int main() {
cout << "C++ is awesome!" << endl;
return 0;
}
g++ myprogram.cpp -o myprogram
./myprogram
Quiz
What does a compiler do?
- A) Edits code
- B) Converts source code to an executable
- C) Runs code line by line
- D) Manages memory
Answer
B) A compiler translates your C++ source code into machine code (an executable).
Which command compiles a file named main.cpp using g++?
- A)
run main.cpp - B)
cpp main.cpp - C)
g++ main.cpp -o main - D)
compile main.cpp
Answer
C) g++ main.cpp -o main compiles the file and names the output main.
Summary
- You need a compiler (GCC/g++, Clang, or MSVC) to run C++ programs.
- C++ source files use the .cpp extension.
- Compile with
g++ filename.cpp -o output, run with ./output.
- Online compilers like cpp.sh let you practice without installing anything.