Skip to content
Laviyx Learn

Try

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.

Beginner4 min readUpdated
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:

PowerShell
winget install BrechtSanders.WinLibs.POSIX.UCRT

This installs a recent GCC (the WinLibs MinGW-w64 build). Close and reopen PowerShell so the new commands are found, then check:

PowerShell
g++ --version

You 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)#

  1. Download Visual Studio Community (free) from Microsoft's website.
  2. In the installer, tick the Desktop development with C++ workload.
  3. 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:

Terminal
xcode-select --install

Then check the installation. On macOS, the g++ command is an alias for clang, so either name works:

Terminal
clang++ --version

Linux#

On Ubuntu or Debian:

Terminal
sudo apt update
sudo apt install build-essential
g++ --version

On 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:

hello.cpp (C++)
#include <iostream>

int main() {
    std::cout << "The compiler works!\n";
}
Output
The compiler works!

Then compile and run it from a terminal in the same folder:

Terminal
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
./hello

On Windows PowerShell, run .\hello.exe instead of ./hello. With clang, replace g++ with clang++.

The extra options are worth using every time:

OptionMeaning
-std=c++20Use the C++20 standard (some older compilers default to an older standard)
-Wall -WextraTurn on helpful warnings about likely mistakes
-o helloName the output program hello (otherwise it is a.out or a.exe)

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?