Setting Up Your Environment
Before you can run C++ programs, you need three things: a compiler that turns your source code into machine code, a build system that manages compilation for projects with multiple files, and a text editor where you write the code. This guide sets up the most common beginner stack: GCC or Clang as the compiler, CMake as the build system, and Visual Studio Code as the editor. The resulting workflow scales from single-file exercises all the way to large professional projects.
Step 1 — Install a compiler
The compiler is the core tool. It reads your .cpp files and produces native machine code. The three major C++ compilers are GCC (the GNU Compiler Collection), Clang (part of the LLVM project), and MSVC (Microsoft Visual C++). GCC and Clang are interchangeable for most purposes; MSVC is the native choice on Windows. Install whichever is standard for your platform:
sudo apt update sudo apt install build-essential cmake # Verify g++ --version # should print GCC version cmake --version # should print CMake version
# Install Xcode Command Line Tools (installs clang++) xcode-select --install # Then install CMake via Homebrew brew install cmake # Verify clang++ --version cmake --version
# Option A: Visual Studio (full IDE, includes MSVC + CMake) # Download from https://visualstudio.microsoft.com/ # Install "Desktop development with C++" workload # Option B: MSYS2 (gives you GCC + CMake in a Unix-like shell) # 1. Download MSYS2 from https://www.msys2.org/ # 2. Open "MSYS2 MINGW64" terminal and run: pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-cmake # Option C: WSL2 (Windows Subsystem for Linux) — run Ubuntu inside Windows # Then follow the Linux instructions above
Step 2 — Install VS Code and the C++ extension
Visual Studio Code is a lightweight, free editor that runs on all platforms. Its C++ extension (from Microsoft) adds IntelliSense code completion, inline error highlighting, debugging support, and integration with CMake. Download VS Code from code.visualstudio.com, then install the following extensions from the Extensions panel (Ctrl+Shift+X):
C/C++ — Microsoft
IntelliSense, debugging, code navigation. Required.
CMake Tools — Microsoft
Configure, build, and debug CMake projects directly from VS Code. Required for CMake projects.
C/C++ Extension Pack — Microsoft
Bundles the above two plus themes and extra tools. Install this if you want everything at once.
Step 3 — Create your first CMake project
CMake is a build system generator — it reads a configuration file called CMakeLists.txt and generates the native build files for your platform (Makefiles on Linux/macOS, Visual Studio project files on Windows, etc.). You write one CMakeLists.txt and CMake handles platform differences for you. For the Hello World program, the configuration is tiny:
Create this directory structure:
hello-project/
├── CMakeLists.txt
└── src/
└── main.cppCMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(Hello CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable(hello src/main.cpp)
src/main.cpp:
#include <iostream>
int main()
{
std::cout << "Hello, CMake!" << std::endl;
return 0;
}Each line of the CMake file does something specific:
cmake_minimum_required(VERSION 3.16)Ensures CMake is new enough to support the features you use.project(Hello CXX)Names the project 'Hello' and declares it uses C++ (CXX).set(CMAKE_CXX_STANDARD 17)Compiles all targets with the C++17 standard.set(CMAKE_CXX_STANDARD_REQUIRED ON)Makes CMake error out rather than silently falling back if C++17 is unavailable.add_executable(hello src/main.cpp)Tells CMake to compile main.cpp and link it into an executable named 'hello'.Building and running
CMake uses an out-of-source build pattern: you create a separate build/ directory so generated files never mix with your source. From the project root:
# Configure — creates the build directory and generates build files cmake -S . -B build # Build — compiles and links cmake --build build # Run ./build/hello # Linux / macOS build\hello.exe # Windows
With the CMake Tools extension in VS Code, you can do all of this through the UI: a status bar at the bottom shows the current kit (compiler), build type, and target. Click Build or press F7. To run with the debugger, press F5.
For development builds, add -DCMAKE_BUILD_TYPE=Debug to the configure step. Debug builds include debugging symbols and disable optimizations, making the debugger more useful. Release builds (-DCMAKE_BUILD_TYPE=Release) enable full optimizations for the final shipping binary.
Verify your setup
Run through this checklist to confirm everything is working before moving on:
g++ --version or clang++ --version prints a version number
If not: Compiler is not installed or not on PATH. Re-run the install step for your platform.
cmake --version prints 3.16 or higher
If not: CMake is missing. Install via apt / brew / winget or download from cmake.org.
cmake -S . -B build completes without errors
If not: Check that CMakeLists.txt is in the current directory and has no syntax errors.
cmake --build build produces a hello executable
If not: Check main.cpp for syntax errors — the compiler output will tell you exactly what is wrong.
./build/hello prints 'Hello, CMake!'
If not: If it runs but prints nothing, check that cout and endl are spelled correctly.