C++ course menu
Installing a C++ Compiler on Windows, macOS and Linux (g++, clang, MSVC)
Install a C++ compiler step by step: GCC (MinGW-w64) or Visual Studio on Windows, Xcode command line tools (clang) on macOS, and g++ on Linux. Check the installation, compile your first file, and choose an editor.
On this page
To run C++ you need a compiler: a program that turns your .cpp source files into an executable. The three widely used compilers are free:
- GCC (
g++): the standard on Linux, also available on Windows through MinGW-w64. - Clang (
clang++): the default on macOS. - MSVC (
cl): Microsoft's compiler, included with Visual Studio on Windows.
Any of them works for this course. Pick the section for your operating system.
Windows#
Option 1: GCC with winget (quick, command line)#
Windows 10 and 11 include the winget package manager. Open PowerShell and run:
winget install BrechtSanders.WinLibs.POSIX.UCRTThis installs a recent GCC (the WinLibs MinGW-w64 build). Close and reopen PowerShell so the new commands are found, then check:
g++ --versionYou should see a version number such as g++ (MinGW-W64 ...) 14.2.0 or newer. Any version from 11 onwards supports everything in this course.
Option 2: Visual Studio (full IDE)#
- Download Visual Studio Community (free) from Microsoft's website.
- In the installer, tick the Desktop development with C++ workload.
- Create a new Console App project, and press Ctrl + F5 to build and run.
Visual Studio is a complete environment with a debugger, which is great for larger projects. It uses the MSVC compiler instead of g++.
macOS#
Install Apple's command line tools, which include clang:
xcode-select --installThen check the installation. On macOS, the g++ command is an alias for clang, so either name works:
clang++ --versionLinux#
On Ubuntu or Debian:
sudo apt update
sudo apt install build-essential
g++ --versionOn Fedora, use sudo dnf install gcc-c++. Most other distributions have a similar package.
Compile and run a test program#
Create a file called hello.cpp with this content:
#include <iostream>
int main() {
std::cout << "The compiler works!\n";
}The compiler works!Then compile and run it from a terminal in the same folder:
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
./helloOn Windows PowerShell, run .\hello.exe instead of ./hello. With clang, replace g++ with clang++.
The extra options are worth using every time:
Choosing an editor#
You can write C++ in any text editor. Popular free choices:
- Visual Studio Code with Microsoft's C/C++ extension: light and works on every system.
- Visual Studio (Windows): a full IDE with a powerful debugger.
- CLion (paid, with free educational licences) and Code::Blocks (free).
You can also try small programs in an online compiler such as Compiler Explorer, but install a compiler locally for real work.
Common problems#
Key takeaway#
Next: write and understand your First C++ Program.
Finished this lesson?