C++ - Installation & Toolchain
Overview
Estimated time: 30–45 minutes
Install a modern C++ compiler and basic tools on macOS, Linux, or Windows. Verify versions and run a quick compile.
Learning Objectives
- Install clang++/g++/MSVC and confirm versions.
- Compile a simple program from the command line.
- Understand the role of build systems (CMake) for larger projects.
Prerequisites
Install
macOS (Homebrew)
brew update && brew install llvm
clang++ --version
Linux (Debian/Ubuntu)
sudo apt update && sudo apt install build-essential -y
g++ --version
Windows (MSVC)
Install "Desktop development with C++" using Visual Studio Installer. Use Developer Command Prompt:
cl /Bv
Compile a program
# hello.cpp
cat > hello.cpp <<'CPP'
#include <iostream>
int main(){ std::cout << "hi\n"; }
CPP
# Compile with clang++ or g++ (C++20)
clang++ -std=c++20 -O2 hello.cpp -o hello
./hello
Expected Output: hi
CMake (optional)
brew install cmake # macOS (or your OS package manager)
cmake --version
Common Pitfalls
- Using an old default compiler that lacks C++20 support—pin -std=c++20 and check versions.
- Mixing debug and release artifacts when compiling manually—clear builds between configurations.
Checks for Understanding
- How do you check your compiler version?
- Why use a build system like CMake?
Show answers
- Run
clang++ --version
,g++ --version
, orcl /Bv
. - To manage multi-file builds, dependencies, and platform differences consistently.
Exercises
- Compile hello.cpp in both debug (-g) and optimized (-O2) modes.
- Create a minimal CMakeLists.txt and build hello using CMake.