Compilers

This explains some of the issues surrounding compilers, implementations of the C++ Standard Library, and platforms.

The compiler MSVC (Microsoft Visual C++), cl.exe, is only available on Windows. It is not available on Linux, macOS, or in GitHub Codespaces. This is despite the fact that GitHub Codespaces run on Azure (Microsoft).

Two other popular compilers do run on Linux, macOS, and in GitHub Codespaces:

All C++ compilers support the C++ standard library. This is a set of include files, e.g., <iostream>, and the corresponding shared object libraries. There are two implementations:

Implementation Description Supporting Compilers Notes
libstdc++ The GCC implementation of the C++ Standard Library GCC and Clang Older and standard for Linux
libc++ The Clang implementation of the C++ Standard Library Clang Newer and standard for macOS

By "standard" it means that shipped programs and packages typically use the indicated standard library.

Although both library implementations support the same standard, they are not implemented the same way and may differ. This mostly has to do with the include files used to implement it. This means that a program that compiles on Linux with libstdc++ may not compile on macOS with libc++, and vice versa. You will mostly see this when indirect include files are missing.

This means it is important that your program can build with both libstdc++ and libc++. In addition, each compiler, GCC and Clang, may issue warnings that the other does not, leading to improved, less buggy code.

The following will show you which standard library you are using, and how to build with the other standard library.

The instructions assume that you have a build directory and have run cmake at least once. Note that there is nothing magic about building in a subdirectory called "build". The name of the build directory can be anything you want. Consider having multiple build directories, one for GCC and one for Clang.

Linux/WSL

Linux uses the GCC compiler for C++, which is g++ by default, and as explained above, the compiler will use libstdc++. You can see this by building normally with g++ and seeing which standard library implementation is used:

If you want to compile with clang, then you have to install it on Linux and WSL:

This installs the compiler clang++ and libc++.

To configure CMake to use a different compiler, you define the CMake variable CMAKE_CXX_COMPILER:

To switch back

GitHub Codespaces

Just like Linux/WSL, except clang is already installed.

macOS

By default, macOS uses clang. Even the command g++ is a clang compiler. To compile with GCC, you install gcc:

This will give you a version of the GCC C++ compiler. Currently, it is g++-15

To configure CMake to use a different compiler, you define the CMake variable CMAKE_CXX_COMPILER. For GCC with libstdc++:

The default is Clang with libc++. To switch back, delete the build directory.

Note Clang on macOS uses Clang's standard C++ include files, while clang on Linux (and WSL) typically uses GCC's standard C++ include files. So you will want to periodically use GitHub Codespaces to verify that your code has the correct include files for clang on Linux.