Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

C++ compilers: GCC, Clang and MSVC

Compare the three main C++ compilers, GCC, Clang and MSVC: how to invoke them, detect them in code, check language support and write portable programs.

A C++ compiler turns source files into object files, and a linker joins those into a program. C++ is defined by an ISO standard, not by one implementation, so several compilers exist, and each one supports a slightly different subset of the newest features at any moment. The three you will meet most often are GCC (g++), Clang (clang++) and MSVC (cl.exe).

Good C++ code is written against the standard and tested on at least two compilers. Different compilers print different warnings, so a second one finds bugs the first one stays quiet about. This page shows how the compilers differ, how a program can recognize which one is building it, and how to ask what a compiler supports instead of guessing.

Terminal
g++     -std=c++23 -Wall -Wextra main.cpp -o app      # GCC
clang++ -std=c++23 -Wall -Wextra main.cpp -o app      # Clang
cl /std:c++latest /W4 /EHsc main.cpp                  # MSVC

Try it

C++ Which compiler and which standard?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Ask the compiler, do not guess

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The examples above are compiled on our server by GCC 15.2 in -std=c++23 mode. Clang and MSVC are not installed there, so their command lines in this page are shown read-only.

The three compilers

Compiler Driver Platforms Typical strengths
GCC g++ Linux first, also Windows (MinGW) and macOS Very fast generated code, broad target support, usually early with new library features.
Clang (LLVM) clang++ Linux, macOS, Windows Clear diagnostics, a rich tool ecosystem (clang-tidy, clang-format, sanitizers), a standard library of its own (libc++).
MSVC cl.exe Windows Native Visual Studio integration, the default for Windows desktop code and the Windows SDK.

Others exist: Intel's icpx is Clang-based, Apple's clang++ ships with Xcode, and zig c++ bundles Clang. Embedded vendors often supply patched GCC or Clang builds.

Compiler, library and runtime

The compiler is only one part of the toolchain. Three things are distinct and can be mixed:

  • the compiler front end that understands the language (the -std= flag controls it),
  • the standard library (libstdc++ with GCC, libc++ with Clang on macOS, Microsoft's STL with MSVC), which decides when <print>, std::flat_map or <mdspan> arrive,
  • the linker and C runtime that produce the final executable.

That is why "my compiler supports C++23" does not guarantee that every C++23 header works. Check the compiler support tables for the language features and the library's feature-test macros (see feature-test macros) for the library ones.

Usage notes

  • Pick the standard explicitly. Compilers default to an older or "GNU flavored" mode. -std=c++23 selects strict ISO C++23; -std=gnu++23 also enables GNU extensions. MSVC uses /std:c++20 or /std:c++latest.
  • __cplusplus is unreliable on MSVC. MSVC reports 199711L unless you add /Zc:__cplusplus. Use _MSVC_LANG there, or better, feature-test macros.
  • Clang defines __GNUC__ too. Test __clang__ before __GNUC__, as the first example does, or you will take the GCC branch on Clang.
  • Prefer standard attributes ([[nodiscard]], [[maybe_unused]]) over __attribute__((...)) or __declspec(...). Reach for the vendor spelling only for features that have no standard equivalent, and hide it behind a macro.
  • Build with two compilers in CI. Each reports different bugs, and code that compiles cleanly on both is far more likely to be portable and standard-conforming.
  • Do not rely on extensions such as variable-length arrays, #pragma once (widely supported but not standard) or __builtin_* functions unless a wrapper keeps them in one place.
  • Version numbers matter. A compiler release adds C++ features gradually: always read the release notes and the language support table of the exact compiler version you ship with.
  • Compile times and diagnostics differ. Clang often prints friendlier errors for templates; GCC sometimes catches more flow-related warnings with -Wall -Wextra.