Codectionary / Developer documentation / C++

C++ Compilers: GCC, Clang and MSVC

Learn what the major C++ toolchains do, how to build a first program with each one, and how to check versions and language-standard support.

A compiler is one part of a toolchain

You write C++ source in a .cpp file. A compiler checks and translates it; a linker combines the resulting code and required libraries into an executable. The collection of compiler, linker, standard-library implementation and related tools is called a toolchain. GCC, Clang and MSVC are three major choices. They accept similar C++ source, but their command-line options, diagnostics, libraries and supported features can differ.

An editor is not a compiler. In particular, installing Visual Studio Code or its C/C++ extension does not install a C++ compiler or debugger. You must install and configure a suitable toolchain separately, or use one already on your computer. Visual Studio is a different product that can install MSVC through its C++ workload.

Use the same program with each toolchain

Save the complete program below as hello.cpp in a folder you can access from a terminal. It prints Lessons ready: 3. The source uses basic facilities available in C++17, so the commands below explicitly request C++17 rather than relying on a compiler’s default mode. After editing the file, build again before running the executable; running an older executable will still show its older behaviour.

The commands are examples for an already installed toolchain. Their output filenames and run commands are platform-specific: ./hello is a POSIX-style shell command, while hello.exe runs in a Windows Developer Command Prompt. If a command is not found, check the installation and terminal setup before changing the C++ source.

hello.cpp

#include <iostream>

int main() {
  int lessons = 3;
  std::cout << "Lessons ready: " << lessons << '\n';
  return 0;
}

GCC and g++

GCC stands for GNU Compiler Collection and includes compilers for C++ and other languages. Its g++ command is the usual driver for compiling and linking a C++ program: it selects C++ treatment for source and arranges linking with the C++ standard library. The gcc driver can also compile a .cpp file as C++, but it does not automatically add the C++ library when linking, so use g++ for this beginner workflow.

GCC is common on Linux. A distribution normally supplies compiler packages through its package manager; package names and available releases depend on the distribution. In a terminal with g++ installed, g++ --version shows the installed driver version. The command below builds hello.cpp into an executable named hello; -std=c++17 chooses the language mode, -Wall and -Wextra request useful warnings, and -o names the output. Run it from the same directory with ./hello.

Check the installed GCC C++ driver

g++ --version

Build and run on Linux or another POSIX-style system

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

Clang and clang++

Clang is a compiler front end in the LLVM project. Like GCC, it processes C++ source and works with other toolchain pieces to make an executable. Use clang++ as the C++ driver for the beginner command below. LLVM and Clang are related, but LLVM is not another name for the entire C++ language or its standard library; the library selected for a Clang build depends on the platform and configuration.

Clang is commonly encountered on macOS because Apple provides its Clang-based toolchain through Xcode or the Command Line Tools for Xcode. Apple’s packaged compiler can differ from an upstream LLVM release, so inspect the tool actually installed on your Mac. Apple documents xcode-select --install for installing its command-line tools when needed. In a terminal with clang++ available, check clang++ --version, then build and run the program as shown.

Check the installed Clang C++ driver

clang++ --version

Build and run on macOS or another POSIX-style system

clang++ -std=c++17 -Wall -Wextra hello.cpp -o hello
./hello

MSVC and the Visual Studio developer prompt

MSVC is Microsoft’s C++ compiler toolchain for Windows. Visual Studio can install it with the Desktop development with C++ workload; Microsoft also offers command-line Build Tools. The compiler executable is cl.exe, and Visual Studio can configure the compiler and linker through its project settings. This setup is different from simply installing VS Code, which can edit C++ but does not provide MSVC itself.

Open the Developer Command Prompt or an appropriate Native Tools Command Prompt installed with your Visual Studio or Build Tools version. That shortcut sets paths to the compiler, headers, libraries and Windows SDK. A plain terminal often lacks those settings. Type cl to display its version and usage; it may return a nonzero status because no source file was supplied, which is normal for this check. Move to your project folder, then build hello.cpp with the command below. /EHsc configures standard C++ exception handling and /std:c++17 selects C++17. The output is hello.exe; run it with hello.exe in that prompt.

In the Visual Studio Developer Command Prompt

cl

Build and run in that prompt

cl /EHsc /std:c++17 hello.cpp
hello.exe

Check versions and standard support separately

A compiler version identifies the installed implementation; a language-standard mode controls which standard’s rules and facilities the compiler attempts to use for this build. g++ --version and clang++ --version report their drivers. In an MSVC Developer Command Prompt, cl prints its compiler version. These version checks do not by themselves prove that every feature in a C++ standard is implemented.

Use -std=c++17 with GCC or Clang and /std:c++17 with MSVC for this guide’s program. Other modes, such as C++20, may be available, but the supported set depends on the installed toolchain and its standard library. Try the requested mode on a small program and consult that release’s official feature-support documentation when a project needs a particular feature. A successful “Hello” build proves the selected option is accepted and this program works; it does not test every C++17 facility. Avoid assuming the default mode is identical across compilers or versions.

ToolchainCheck installed versionSelect C++17
GCC / g++g++ --version-std=c++17
Clang / clang++clang++ --version-std=c++17
MSVC / clcl in a Developer Command Prompt/std:c++17

Choose the toolchain your project needs

For a first local program on Linux, the distribution’s GCC toolchain is a common starting point. On macOS, Apple’s Clang-based tools are a natural default. On Windows, MSVC is well integrated with Visual Studio; other supported toolchains exist, but their installation and shell commands differ. An established project may specify its compiler, minimum version, language mode and libraries. Follow those requirements before switching toolchains.

If g++ or clang++ is “not found”, install a compatible toolchain and check that its command is on your PATH. If cl is not recognised, launch the Visual Studio Developer Command Prompt and verify the C++ workload or Build Tools are installed. If a header cannot be found, check the compiler installation and configured include paths rather than assuming the header belongs in the source folder. If a standard-mode option is rejected or a feature is missing, check the installed compiler version and that release’s official support table. If compilation succeeds but linking fails, confirm that every required .cpp file and library is included in the build.

Syntax

#include <iostream>

int main() {
  int lessons = 3;
  std::cout << "Lessons ready: " << lessons << '\n';
  return 0;
}

Examples

Build one complete C++17 console program

Save this code as hello.cpp and use the command for your installed GCC, Clang or MSVC toolchain above.

#include <iostream>

int main() {
  int lessons = 3;
  std::cout << "Lessons ready: " << lessons << '\n';
  return 0;
}
  1. The program prints Lessons ready: 3 to the terminal. Its source is the same for all three compiler commands; the build command and executable name vary by toolchain and platform.
  2. #include <iostream> supplies the declaration of std::cout. main is the entry function, lessons holds an integer and the stream insertion operators print the text, number and newline.
  3. The compiler checks the source, and the driver arranges the link step to create an executable. Running that executable produces the output; merely saving hello.cpp does not run the program.
  4. Change 3 to 4, build again and rerun to confirm which compiler and output file your terminal uses. If the old output remains, check that you rebuilt and launched the same executable.

Best practices

  • Install a real C++ toolchain separately from an editor and verify its command in the terminal you will use.
  • Choose an explicit language-standard mode and check feature support for the installed release when a project depends on newer C++ facilities.
  • Keep compiler, linker and runtime problems distinct when troubleshooting a build.

Runtime & versions

Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ISO C++ resources

Specifications & further reading