Install C++ and Compile Your First Program
Set up a modern C++ toolchain on macOS, Linux, or Windows, then compile and run your first program with g++ or clang++ from the command line.
What you'll learn
- ✓Install a C++17/C++20 compiler on each major OS
- ✓Verify the toolchain with version checks
- ✓Compile and run a hello-world program from the terminal
- ✓Pick reasonable warning and standard flags
- ✓Structure a tiny multi-file project
Prerequisites
- •Comfort with a terminal and a text editor
C++ is a compiled language. Before writing code, you need a compiler that turns source files into machine code, a linker that stitches object files together, and a standard library. The good news: every major platform ships a one-line install path.
Pick a compiler
The two free, production-grade C++ compilers are GCC (g++) and Clang (clang++). Both implement modern C++ standards. Pick whichever your platform installs first. MSVC on Windows is also fine if you prefer Visual Studio.
Install on macOS
Install Xcode Command Line Tools. This gives you clang++ and the system standard library.
xcode-select --install
clang++ --version
If you want GCC too, install it via Homebrew with brew install gcc. The binary will be named g++-14 or similar to avoid colliding with Apple’s g++ alias.
Install on Linux
Use your distribution’s package manager.
# Debian/Ubuntu
sudo apt update && sudo apt install build-essential
g++ --version
# Fedora
sudo dnf install gcc-c++ make
build-essential pulls in g++, make, and the standard library headers in one shot.
Install on Windows
The simplest path is MSYS2, which gives you a real GCC. Download the installer from msys2.org, then run:
pacman -S mingw-w64-ucrt-x86_64-gcc
Add C:\msys64\ucrt64\bin to your PATH and confirm with g++ --version from a new terminal. If you prefer MSVC, install Visual Studio with the Desktop development with C++ workload.
Your first program
Create a file named hello.cpp.
#include <iostream>
int main() {
std::cout << "Hello, C++!" << std::endl;
return 0;
}
Compile it. Always pass a standard flag and turn on warnings; the defaults are too permissive.
g++ -std=c++20 -Wall -Wextra -O2 hello.cpp -o hello
./hello
Breakdown: -std=c++20 selects the language version, -Wall -Wextra enables most useful warnings, -O2 requests optimization, and -o hello names the output binary. On Windows the binary is hello.exe.
Multi-file projects
Real projects span many files. Split declarations into headers and definitions into .cpp files. Create greet.h:
#pragma once
#include <string>
std::string greet(const std::string& name);
Create greet.cpp:
#include "greet.h"
std::string greet(const std::string& name) {
return "Hello, " + name + "!";
}
Update hello.cpp:
#include <iostream>
#include "greet.h"
int main() {
std::cout << greet("world") << "\n";
}
Compile both translation units together. The compiler produces object files and the linker merges them.
g++ -std=c++20 -Wall -Wextra hello.cpp greet.cpp -o hello
#pragma once ensures the header is included only once per translation unit, replacing the older include-guard pattern.
A minimal Makefile
Typing the full command each rebuild gets tedious. A small Makefile saves keystrokes:
CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -O2
hello: hello.o greet.o
$(CXX) $(CXXFLAGS) -o $@ $^
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
rm -f *.o hello
Run make to build and make clean to wipe artifacts. For larger projects, CMake is the standard build system; learn it once your project has more than a handful of files.
Common pitfalls
- Forgetting
-std=c++20(orc++17) leaves you on an old standard. Modern features will not compile. - Skipping
-Wall -Wextrahides bugs the compiler can detect for free. - Mixing GCC and Clang standard libraries on macOS can cause link errors. Stick to one toolchain per project.
- On Windows,
g++not being on PATH is the most common confusing error. Open a fresh terminal after editing PATH.
What to learn next
You now have a working compiler. The next steps are the language fundamentals: read Variables and Fundamental Types for how C++ models data, and Control Flow for branching and loops. Once those click, move on to Functions and References.
Wrap up
A C++ toolchain is one package install away on every platform. Always compile with a recent standard, warnings on, and use a build system once you have more than two files. Everything else in C++ builds on this loop: edit, compile, run, repeat.