Setting Up C++ Development Environment
Install GCC or Clang, configure CMake, set up VS Code, and compile your first C++ program with modern standards.
Installing a C++ Compiler
Before you can write and run C++, you need a compiler that translates your source code into machine code. The two dominant choices are GCC (GNU Compiler Collection) and Clang (LLVM-based). Both fully support C++17 and C++20, produce fast binaries, and are completely free. Clang tends to produce friendlier error messages, making it particularly good for learning. GCC often edges ahead on raw optimization. In practice, professionals use both.
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install build-essential # installs g++, gcc, make
sudo apt install clang # optional: install Clang too
g++ --version # verify GCC
clang++ --version
build-essential gives you g++, gcc, make, and standard headers. It is the standard starting point on Debian-based distros.
macOS
Apple ships Clang via Xcode Command Line Tools. This is the fastest path to a working compiler on macOS — no full Xcode install required.
# Install Xcode Command Line Tools (includes Apple Clang)
xcode-select --install
# Verify
clang++ --version
# Optionally install GCC via Homebrew
brew install gcc
g++-14 --version # note: Homebrew installs as g++-<version>
Windows (MSYS2 / MinGW-w64)
The cleanest way to get GCC on Windows is through MSYS2. It provides a full Linux-like shell environment and package manager, so you can follow the same workflow as on Linux.
# In MSYS2 UCRT64 terminal
pacman -S mingw-w64-ucrt-x86_64-gcc
pacman -S mingw-w64-ucrt-x86_64-clang # optional
g++ --version
Add C:\msys64\ucrt64\bin to your Windows PATH so you can run g++ from any terminal.
Alternative on Windows: Install Visual Studio Build Tools to get MSVC (cl.exe), which is Microsoft’s own compiler. VS Code works with all three (GCC, Clang, MSVC).
Compiling from the Command Line
Understanding the compiler flags directly is essential before relying on build systems. When something goes wrong in a build, you need to know what command the build system is actually running. These flags are the vocabulary of C++ compilation.
# Basic compile — outputs executable named 'main'
g++ main.cpp -o main
# With modern standard and warnings — use this while learning
g++ -std=c++17 -Wall -Wextra -o main main.cpp
# With optimizations (release build)
g++ -std=c++17 -O2 -DNDEBUG -o main main.cpp
# With debug info (debug build) — required for debuggers like gdb/lldb
g++ -std=c++17 -g -O0 -o main main.cpp
# C++20
g++ -std=c++20 -Wall -Wextra -o main main.cpp
# Using Clang (same flags)
clang++ -std=c++17 -Wall -Wextra -o main main.cpp
Key flags:
-std=c++17/-std=c++20— enable the specified standard (default is usually C++14 or older)-Wall— enable common warnings-Wextra— enable additional warnings beyond-Wall-O2— optimize for speed (release builds)-g— include debug symbols for debuggers like gdb/lldb-o <name>— set output file name
A simple test program to verify your setup:
// verify.cpp
#include <iostream>
#include <format> // C++20 — if this compiles, you have C++20 support
#include <vector>
#include <ranges>
int main() {
std::vector<int> v = {5, 3, 1, 4, 2};
// C++20 ranges sort — cleaner than std::sort(v.begin(), v.end())
std::ranges::sort(v);
for (int x : v) {
std::cout << std::format("{} ", x);
}
std::cout << "\n";
return 0;
}
g++ -std=c++20 -o verify verify.cpp && ./verify
# 1 2 3 4 5
Setting Up VS Code
VS Code is the most popular editor for C++ development on all platforms. It is free, fast, and has excellent C++ tooling through extensions. The right extension combination gives you IntelliSense, debugging, and integrated CMake builds all in one window.
Extensions to install:
- C/C++ by Microsoft (
ms-vscode.cpptools) — IntelliSense, debugging, code navigation - clangd (
llvm-vs-code-extensions.vscode-clangd) — faster, more accurate IntelliSense based on Clang’s toolchain (recommended over the default IntelliSense for large projects) - CMake Tools (
ms-vscode.cmake-tools) — CMake integration with build/run buttons
If you use both cpptools and clangd, disable cpptools IntelliSense to avoid conflicts: add "C_Cpp.intelliSenseEngine": "disabled" to your VS Code settings.
A minimal .vscode/c_cpp_properties.json for GCC on Linux:
{
"configurations": [
{
"name": "Linux GCC",
"compilerPath": "/usr/bin/g++",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
Setting Up CMake
CMake is the de-facto build system for C++ projects. It solves a real problem: you want your project to build on Linux with Makefiles, on macOS with Xcode, and on Windows with Visual Studio — all from the same source. CMake generates the native build files for each platform from a single CMakeLists.txt description.
Install CMake:
# Ubuntu/Debian
sudo apt install cmake
# macOS
brew install cmake
# Windows (MSYS2)
pacman -S mingw-w64-ucrt-x86_64-cmake
A minimal CMakeLists.txt for a single-target project:
cmake_minimum_required(VERSION 3.20)
project(MyApp VERSION 1.0 LANGUAGES CXX)
# Require C++17 — fail the build if the compiler doesn't support it
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) # use -std=c++17, not -std=gnu++17
# Define the executable and its source files
add_executable(myapp
src/main.cpp
src/utils.cpp
)
# Enable warnings for this target
target_compile_options(myapp PRIVATE -Wall -Wextra)
Build and run:
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build .
./myapp
For a debug build:
cmake .. -DCMAKE_BUILD_TYPE=Debug
cmake --build .
CMake separates source and build directories — the build/ folder contains all generated files and compiled objects. Your source tree stays clean and the build directory can be deleted and regenerated at any time.
Recommended Project Layout
A clean, consistent directory layout makes projects easier to navigate and scales naturally as the codebase grows. This layout separates source, headers, and tests, and keeps generated build artifacts out of the source tree.
myproject/
├── CMakeLists.txt
├── src/
│ ├── main.cpp
│ └── engine/
│ ├── engine.hpp
│ └── engine.cpp
├── tests/
│ └── engine_test.cpp
└── build/ # generated, not committed to git
With this structure, your CMakeLists.txt grows naturally as you add source files, and VS Code’s CMake Tools extension can auto-detect the project and provide build buttons in the status bar.