Setting Up the C++ Environment
Before writing C++ programs you need two things: a compiler (turns your code into an executable) and a code editor (where you write the code). This topic walks through the complete setup on all major platforms and gives you the compile-run workflow you'll use every day.
Choosing a Compiler
| Compiler | Platform | Notes |
|---|---|---|
| GCC / G++ | Linux, macOS, Windows (MinGW) | Open-source, standard reference |
| Clang / Clang++ | macOS, Linux, Windows | Excellent error messages, used by Apple |
| MSVC | Windows | Microsoft Visual C++ — used with Visual Studio IDE |
Note: This course uses GCC/G++ because it is available on all platforms and is the most common compiler in educational and Linux server environments.
Installation
Linux (Ubuntu / Debian)
sudo apt update
sudo apt install build-essential gdb cmake
g++ --version
# g++ (Ubuntu 13.2.0) 13.2.0build-essential installs GCC, G++, make, and standard headers in one command.
macOS
# Install Xcode Command Line Tools (includes Clang aliased as g++)
xcode-select --install
# Or install GCC via Homebrew
brew install gcc
g++ --versionWindows (MinGW-w64 via MSYS2)
1. Download MSYS2 from https://www.msys2.org
2. Run the installer — default path: C:\msys64
3. Open "MSYS2 UCRT64" terminal and run:
pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-gdb cmake
4. Add to PATH: C:\msys64\ucrt64\bin
5. Open a new Command Prompt and verify:
g++ --versionVS Code Setup (Recommended Editor)
VS Code with the C/C++ extension is the most popular lightweight setup:
1. Install VS Code from https://code.visualstudio.com
2. Install the "C/C++" extension by Microsoft (ms-vscode.cpptools)
3. Optional: Install "C/C++ Extension Pack" for CMake integrationConfigure IntelliSense
Create .vscode/c_cpp_properties.json in your project folder:
{
"configurations": [
{
"name": "Linux",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}Build Task
Create .vscode/tasks.json for one-key compilation:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build C++",
"type": "shell",
"command": "g++",
"args": [
"-std=c++17",
"-Wall",
"-Wextra",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}Press Ctrl+Shift+B to build. Press F5 to run with the debugger.
Your First Program — Step by Step
Create a file called hello.cpp:
#include <iostream> // Include the input/output library
int main() { // Every C++ program starts at main()
std::cout << "Hello, World!" << std::endl;
return 0; // 0 means success
}Compile and Run
# Compile
g++ -std=c++17 hello.cpp -o hello
# Run
./hello # Linux / macOS
hello.exe # Windows
# Output:
# Hello, World!What Each Flag Does
| Flag | Meaning |
|---|---|
-std=c++17 | Use the C++17 standard |
-Wall | Enable all common warnings |
-Wextra | Enable extra warnings |
-g | Include debug symbols (for gdb / debugger) |
-O2 | Optimise for speed (use in release builds) |
-o hello | Name the output file hello |
Recommended Project Structure
my_project/
├── src/
│ ├── main.cpp
│ └── utils.cpp
├── include/
│ └── utils.h
├── build/ ← compiled output goes here
├── CMakeLists.txt ← build system definition
└── README.mdSimple CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(MyProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# Enable warnings
add_compile_options(-Wall -Wextra)
add_executable(myapp src/main.cpp src/utils.cpp)
target_include_directories(myapp PRIVATE include)# Build with CMake
mkdir build && cd build
cmake ..
cmake --build .
./myappUsing the Debugger (gdb)
# Compile with debug info
g++ -std=c++17 -g hello.cpp -o hello
# Start gdb
gdb ./hello
# Inside gdb:
(gdb) break main # Set breakpoint at main
(gdb) run # Start the program
(gdb) next # Step over one line
(gdb) print someVar # Inspect a variable
(gdb) continue # Continue until next breakpoint
(gdb) quit # Exit gdbIn VS Code, just press F5 — the GUI debugger uses gdb under the hood.
Online Compilers (No Installation)
If you want to experiment without a local setup:
| Tool | URL | Features |
|---|---|---|
| Compiler Explorer | godbolt.org | See assembly output |
| Wandbox | wandbox.org | Multiple compilers |
| replit.com | replit.com | Full IDE in browser |
| cpp.sh | cpp.sh | Quick snippets |
Summary
- Install GCC/G++ via
build-essential(Linux),xcode-select(macOS), or MSYS2 (Windows) - VS Code with the C/C++ extension and
tasks.jsongives you one-key compile from the editor - Compile with
g++ -std=c++17 -Wall file.cpp -o output, run with./output - Always compile with
-Wall -Wextrato catch common mistakes early - Use CMake for projects with multiple source files
- The
-gflag adds debug information; usegdbor VS Code's debugger to step through code
Next, we'll explore the basic syntax of C++ — how programs are structured, what #include means, and the rules that govern every line of code.