Installing a C++ Compiler — g++, Clang, MSVC, and CMake
In this tutorial, you will learn about Installing a C++ Compiler. We cover key concepts, practical examples, and best practices to help you master this topic.
C++ source code must be compiled into machine code before execution, and this lesson shows exactly how to install and verify the compiler toolchain on every major operating system.
What You'll Learn
You will install the GNU C++ Compiler (g++), Clang, or Microsoft Visual C++ (MSVC) on your machine, set up CMake as a build system orchestrator, write and compile your first C++ program to verify everything works, and understand the differences between compiler flags and standards versions.
Why It Matters
Theory is useless without practice. Every C++ lesson from this point forward requires a working compiler. Installing a compiler is the first practical skill you need, and doing it correctly now prevents frustration later. Many beginners get stuck on linking errors, missing headers, or wrong standard versions — and all of these trace back to an incorrectly configured environment.
Learning Path
graph LR
A["01: What is C++"] --> B["02: Installing a Compiler"]
B --> C["03: Hello World"]
C --> D["04: Variables & Types"]
style A fill:#4a90d9,stroke:#2c5f8a,color:#fff
style B fill:#4a90d9,stroke:#2c5f8a,color:#fff
style C fill:#4a90d9,stroke:#2c5f8a,color:#fff
style D fill:#4a90d9,stroke:#2c5f8a,color:#fff
GCC (g++) Installation
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install g++ gdb make
g++ --version
Expected output (version may vary):
g++ (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Linux (Fedora/RHEL)
sudo dnf install gcc-c++ gdb make
g++ --version
macOS
Install Xcode Command Line Tools which include Clang (not GCC, but compatible):
xcode-select --install
g++ --version
Note: On macOS, g++ is actually Clang with a GCC-compatible frontend.
Windows
Option A: MinGW-w64 (Minimalist GNU for Windows)
- Download from https://www.mingw-w64.org
- Add
C:\mingw64\binto your PATH - Open Command Prompt and run
g++ --version
Option B: Visual Studio Community (includes MSVC)
- Download from https://visualstudio.microsoft.com
- Select "Desktop development with C++" workload
- Open "Developer Command Prompt for VS" from Start Menu
- Run
clto verify MSVC compiler is available
Clang Installation
Clang is the default compiler on macOS and is available on all platforms.
Ubuntu/Debian
sudo apt install clang lldb
clang++ --version
Windows (with Visual Studio)
Clang is included with Visual Studio 2019 and later. Select the "C++ Clang tools for Windows" component during installation.
CMake Installation
CMake is a build system generator that creates Makefiles or Visual Studio project files from a CMakeLists.txt file. You will use it extensively in later lessons.
All Platforms
# Linux/macOS
sudo apt install cmake # Ubuntu
brew install cmake # macOS
# Windows: download from https://cmake.org/download/
cmake --version
Expected output (version may vary):
cmake version 3.28.3
Understanding Compiler Flags
When you compile C++ programs, you will pass flags that control the language standard, warnings, and optimization level:
g++ -std=c++20 -Wall -Wextra -Wpedantic -O2 main.cpp -o main
| Flag | Meaning |
|---|---|
-std=c++20 |
Use the C++20 standard |
-Wall |
Enable most common warnings |
-Wextra |
Enable extra warnings |
-Wpedantic |
Reject non-standard extensions |
-O2 |
Optimize for speed (level 2) |
-g |
Include debug symbols |
-o output |
Name of the output executable |
Always compile with warnings enabled. Warnings catch real bugs.
Compiler Differences
GCC, Clang, and MSVC implement the C++ standard with slight variations:
| Feature | GCC | Clang | MSVC |
|---|---|---|---|
| Default standard | gnu++17 | gnu++17 | /std:c++14 |
| C++20 modules | Partial | Full | Partial |
| Conformance | Excellent | Excellent | Good (improving) |
| Error messages | Verbose | Excellent | Good |
| Compilation speed | Moderate | Fast | Moderate |
For learning, any of these compilers works. This series uses GCC with -std=c++17 as the baseline for examples.
Writing Your First Program
Create a file called test.cpp:
#include <iostream>
int main() {
std::cout << "C++ compiler works!\n";
return 0;
}
Compile and run:
g++ -std=c++17 -Wall test.cpp -o test
./test
Expected output:
C++ compiler works!
Using an Integrated Development Environment
You do not need an IDE to learn C++, but it can help. Popular options:
- VS Code with the C++ extension pack (Microsoft, C/C++, CMake Tools)
- CLion by JetBrains (commercial, excellent CMake support)
- Visual Studio Community Edition (Windows-only, excellent debugger)
- Qt Creator (cross-platform, good for GUI applications)
For this series, VS Code with the C/C++ extension is the recommended choice because it is free, cross-platform, and lightweight.
Common Mistakes
Mistake 1: Command Not Found
If your terminal says g++: command not found, you have not installed the compiler or it is not in your PATH. On Windows with MinGW, ensure C:\mingw64\bin is in your PATH environment variable.
Mistake 2: Wrong File Extension
C++ source files use .cpp (recommended), .cc, .cxx, or .C. Never use .c for C++ code — some compilers treat .c files as C code and reject C++ features.
Mistake 3: Mixing Compilers on One System
If you have both GCC and Clang installed, g++ and clang++ are separate commands. Running make may pick the wrong one. Use export CXX=g++ to set the C++ compiler explicitly.
Mistake 4: Forgetting the Standard Flag
Without -std=c++17 or -std=c++20, your compiler defaults to an older standard (gnu++17 on GCC 13, which is close to C++17 but with GNU extensions). Always specify the standard explicitly in learning projects.
Mistake 5: Ignoring Warnings
If your program compiles with warnings, treat each warning as an error. Add -Werror to your compiler flags to treat all warnings as errors. This catches subtle bugs early.
Mistake 6: Compiling C Code with a C++ Compiler
C code usually compiles under C++, but the reverse is not true. If you have a C library, compile it with gcc and link it with your C++ code using g++. Mixing gcc and g++ linking can cause undefined reference errors for C++ standard library functions.
Practice Questions
- Install g++ on your system and verify the version. What output do you get?
- Compile the test program above with
-std=c++98. What happens? Why? - What is the difference between
-Walland-Wextra? Try compiling with both flags versus just-Wall. - Create a program that uses
std::coutto print your name, compile it with-std=c++20, and run it. - Research: What does the
-pedanticflag do? Why would you use it?
Challenge
Set up a CMake project with a CMakeLists.txt file that builds a program printing "CMake works!". Use cmake -B build and cmake --build build to compile it.
FAQ
Mini Project
Create a simple project structure with a build script:
cpp-playground/
main.cpp
build.sh
build.sh:
#!/bin/bash
g++ -std=c++20 -Wall -Wextra -Wpedantic -Werror main.cpp -o playground
./playground
Make it executable with chmod +x build.sh, then run ./build.sh. This script pattern will be the foundation for every lesson in this series.
What's Next
Your compiler is installed and verified. The next lesson writes the classic "Hello, World!" program and examines every piece of it in detail. You will learn about iostream, the std namespace, and what happens during each compilation stage.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro