↓Skip to main content
Alemasone

How to Install C and C++ Compilers on Windows, Linux & macOS

Learn how to install GCC using MSYS2 on Windows, build-essential on Linux, and Xcode tools on macOS, including VS Code setup and compilation commands.

7 min read Updated on
Terminal showing a successful C++ program compilation message
On this page
On Linux and macOS, the compiler is installed with a single command. On Windows, the right way to do it today is MSYS2, which provides GCC via MinGW-w64 and handles updates automatically. The old SourceForge MinGW—the one with the Installation Manager you’ll see in many guides (including the first version of this one)—has been stagnant for years, only produces 32-bit programs, and should no longer be used.

A compiler transforms source code into an executable program. For C and C++, there are two main ones: GCC, around since 1987 and available everywhere, and Clang, which is younger, provides clearer error messages, and is the default on macOS. The latest version of GCC is 16, released in April 2026, which uses the C++20 standard by default for C++.

Below you will find installation instructions for all three systems, the commands to compile, and how to configure Visual Studio Code.

Windows: MSYS2 and MinGW-w64 #

MinGW-w64 is the version of GCC that produces native executables for Windows. MSYS2 is the environment that distributes it and keeps it up to date.

  1. Download the installer from the official MSYS2 website and install it using the default path C:\msys64.

  2. Once finished, an MSYS2 terminal will open. Update the base packages:

    pacman -Syu
    

    If the window closes during the update, don’t worry: reopen it from the Start menu (MSYS2 UCRT64) and repeat the command until there is nothing left to update.

  3. Install the 64-bit toolchain group:

    pacman -S mingw-w64-ucrt-x86_64-toolchain
    

    When prompted which packages to install, press Enter to select all of them, then confirm with Y. If you also need a debugger, it is already included: gdbit is part of the group.

  4. Add the compiler folder to your Windows PATH so you can use them from any terminal. Search for Edit the system environment variables in the Start menu (or go to Settings → System → About → Advanced system settings), click Environment Variables, select Path from the user variables, then click Edit → New and type:

    C:\msys64\ucrt64\bin
    
  5. Confirm with OK on all windows, open a new Command Prompt, and check:

    gcc --version
    g++ --version
    

Windows Environment Variables window with the Path entry under user variables
The Path entry to modify is located in the User variables box

There are also shorter ways. w64devkit is a single archive you just unzip, without a package manager: convenient if you only use it occasionally. And if you just want to learn C on Windows, Windows Subsystem for Linux works perfectly fine: install Ubuntu with wsl --install and follow the Linux section below.

Linux #

On Debian, Ubuntu, and derivatives, everything you need is in a single package:

sudo apt update
sudo apt install build-essential gdb

This includes GCC, G++, make, and development libraries. On Fedora:

sudo dnf install gcc gcc-c++ make gdb

On Arch Linux:

sudo pacman -S base-devel gdb

Verify with gcc --version. You don’t need anything else: the paths are already configured.

macOS #

On Mac, the default compiler is Clang, which comes with Xcode Command Line Tools. You can install them using this command, which will open a confirmation window:

xcode-select --install

From that point on, the commands gcc and g++ exist, but they actually launch Clang. It makes no difference for learning purposes.

If you specifically need GCC—for example, for a project that requires it—you can install it with Homebrew:

brew install gcc

The commands will be named gcc-16 and g++-16 along with the version number, so they don’t conflict with system tools.

Compiling from the terminal #

The commands are the same everywhere; only the name of the resulting file changes.

Compiling a single C file:

gcc programma.c -o programma

Compiling C++, requesting a recent standard and all warnings:

g++ -std=c++20 -Wall -Wextra programma.cpp -o programma

Running the result:

./programma

On Windows, from the Command Prompt, the executable is named programma.exe and is launched by typing programma.

Terminal compiling a C++ file with g++ and immediately running the result
Successful compilation: no messages, and the program starts

Essential options to know from the start:

OptionWhat it does
-Wall -WextraShows warnings: these catch errors that the compiler would otherwise accept silently
-gIncludes debug information, which is necessary for using a debugger
-O2Optimizes the code; use this for final versions, not while developing
-std=c++20Sets the language standard, preventing surprises between different computers
If you have more than three files, stop compiling manually. Write a Makefile or use CMake. Recompiling everything after every change is a waste of time, and sooner or later you’ll forget a file and spend half an hour troubleshooting an error that doesn’t exist.

Configuring Visual Studio Code #

Visual Studio Code does not have its own compiler: it uses whatever you have installed. It only takes two steps.

  1. Install the Microsoft C/C++ extension from the extensions panel. Alternatively, you can use clangd, which is faster for autocompletion and diagnostics.

  2. Open your project folder and create a source file. Press F5: on the first run, the editor will ask which compiler to use and will automatically generate the tasks.json and launch.json files in the .vscode subfolder.

If the compiler doesn’t appear in the list, the culprit is usually the PATH on Windows. Check that gcc --version works in a new terminal, then close and restart the editor entirely: it only reads environment variables at startup.

Visual Studio Code asking which compiler to use during the first debug session
On the first run, the editor lists the compilers it found on your system

To compile without the debugger, the built-in terminal (Ctrl + ò) accepts the same commands as shown above.

Common errors #

gcc non è riconosciuto come comando on Windows: the bin folder is not in your PATH variable, or the terminal was already open when you added it.

fatal error: iostream: No such file or directory: you are compiling a C++ file with gcc instead of g++.

You have two different GCC versions: if the old C:\MinGW\bin is still in your PATH, remove that line; otherwise, Windows might find the older version first. where gcc will tell you which one is being used.

undefined reference to ...: the compilation was successful, but there is a missing link to a library or another source file. When there is more than one file, they all need to be specified, or you should use a build system.

The executable won’t run on another Windows computer: runtime libraries are missing. You can fix this by compiling with -static, which includes them in the executable.

FAQ #

Are MinGW and MSYS2 the same thing? #

No. MinGW-w64 is the toolset that allows GCC to produce programs for Windows; MSYS2 is the environment used to distribute and update them, providing a Unix-like terminal. Today, people install MSYS2 specifically to get an up-to-date version of MinGW-w64.

GCC or Clang: which one should you choose? #

Both are fine for learning. Clang is the default on macOS and provides clearer error messages, which is very helpful when you’re starting out. GCC is the standard on Linux and is more commonly found in existing projects. Many developers use both: compiling with two different compilers can reveal errors that a single one might overlook.

Can I still use the old MinGW from SourceForge? #

It works, but it hasn’t been updated for years; it only creates 32-bit programs and lags behind recent C++ standards. If you already have it installed, uninstall it, remove C:\MinGW\bin from your PATH, and switch to MSYS2.

Can I compile Windows programs from Linux? #

Yes, by installing the MinGW-w64 cross-compiler, which is available as a package in most major distributions. You compile with x86_64-w64-mingw32-gcc to get a working .exe, which is useful for distributing software without needing a Windows computer.

Do I need Visual Studio instead of Visual Studio Code? #

They are two different things. Visual Studio is a full Integrated Development Environment (IDE) with its own Microsoft compiler (MSVC), while Visual Studio Code is an editor that uses whichever compiler you provide it. To learn C and C++, the editor with GCC is sufficient and much lighter.

How do I install a debugger? #

On Linux and with MSYS2, gdb is installed along with the compiler, as shown above. On macOS, use lldb, which is already included in the Xcode tools. In both cases, Visual Studio Code recognizes it automatically when it generates your launch configuration.

Read next