Setting Up Your C++ Environment
The three major compilers
| Compiler | Command | Platform |
|---|---|---|
| GCC (GNU) | g++ | Linux, Windows (MinGW/MSYS2), macOS |
| Clang (LLVM) | clang++ | macOS (default with Xcode), Linux, Windows |
| MSVC (Microsoft) | cl | Windows (Visual Studio) |
All three support C++20 well. Check your version: g++ --version.
Installing
- Windows: install Visual Studio Community (choose "Desktop development with C++"), or MSYS2 for g++
- macOS: run
xcode-select --install(gives clang++) - Linux:
sudo apt install g++(Ubuntu/Debian) - Android phone: apps like Cxxdroid let you practise on the go
Editors and IDEs
- Visual Studio (Windows): full IDE with a great debugger
- VS Code + the C/C++ extension: light and popular
- CLion (JetBrains): powerful paid IDE, free for students
- Code::Blocks / Dev-C++: common in schools and colleges
- Online: Compiler Explorer (godbolt.org), OnlineGDB, Programiz — no install needed
Compiling from the terminal
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
./hello (Windows: hello.exe)
| Flag | Meaning |
|---|---|
-std=c++20 | use the C++20 standard |
-Wall -Wextra | turn on useful warnings ✅ always use them |
-o hello | name of the output program |
-g | include debug information |
-O2 | optimize for speed |
File extensions
Source: .cpp (also .cc, .cxx) · Headers: .h or .hpp
Tip
Treat warnings as errors you haven't found yet. Fix every warning.
Example
#include <iostream>
// Check which C++ standard your compiler is using.
// __cplusplus is a number set by the compiler.
int main() {
std::cout << "__cplusplus = " << __cplusplus << '\n';
if (__cplusplus >= 202002L)
std::cout << "You are using C++20 or newer\n";
else if (__cplusplus >= 201703L)
std::cout << "You are using C++17\n";
else
std::cout << "Older standard: add -std=c++20 when compiling\n";
return 0;
}
/* Output:
__cplusplus = 202002
You are using C++20 or newer
*/