Getting Started

Setting Up the C++ Environment

Install GCC/G++, configure VS Code, write your first program, and understand the compile-and-run workflow on Windows, macOS, and Linux.

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

CompilerPlatformNotes
GCC / G++Linux, macOS, Windows (MinGW)Open-source, standard reference
Clang / Clang++macOS, Linux, WindowsExcellent error messages, used by Apple
MSVCWindowsMicrosoft 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)

bash
sudo apt update
sudo apt install build-essential gdb cmake
g++ --version
# g++ (Ubuntu 13.2.0) 13.2.0

build-essential installs GCC, G++, make, and standard headers in one command.

macOS

bash
# Install Xcode Command Line Tools (includes Clang aliased as g++)
xcode-select --install

# Or install GCC via Homebrew
brew install gcc
g++ --version

Windows (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++ --version

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 integration

Configure IntelliSense

Create .vscode/c_cpp_properties.json in your project folder:

json
{
    "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:

json
{
    "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:

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

bash
# Compile
g++ -std=c++17 hello.cpp -o hello

# Run
./hello          # Linux / macOS
hello.exe        # Windows

# Output:
# Hello, World!

What Each Flag Does

FlagMeaning
-std=c++17Use the C++17 standard
-WallEnable all common warnings
-WextraEnable extra warnings
-gInclude debug symbols (for gdb / debugger)
-O2Optimise for speed (use in release builds)
-o helloName the output file hello

my_project/
├── src/
│   ├── main.cpp
│   └── utils.cpp
├── include/
│   └── utils.h
├── build/          ← compiled output goes here
├── CMakeLists.txt  ← build system definition
└── README.md

Simple CMakeLists.txt

cmake
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)
bash
# Build with CMake
mkdir build && cd build
cmake ..
cmake --build .
./myapp

Using the Debugger (gdb)

bash
# 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 gdb

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

ToolURLFeatures
Compiler Explorergodbolt.orgSee assembly output
Wandboxwandbox.orgMultiple compilers
replit.comreplit.comFull IDE in browser
cpp.shcpp.shQuick snippets

Summary

  • Install GCC/G++ via build-essential (Linux), xcode-select (macOS), or MSYS2 (Windows)
  • VS Code with the C/C++ extension and tasks.json gives 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 -Wextra to catch common mistakes early
  • Use CMake for projects with multiple source files
  • The -g flag adds debug information; use gdb or 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.