Learn Computing from the Experts | The Rheinwerk Computing Blog

What Are the Different C++ Compilers?

Written by Rheinwerk Computing | Dec 20, 2024 2:00:00 PM

Curious about C++ compilers? Find out how GCC, Clang++, and MSVC stack up for modern development and what makes each one unique!

 

The term compiler in C++ does not cover a single translation program, but the entire tool chain from the preprocessor to the linker. In addition, the standard library is an integral part of C++. So if you install a compiler on your system, you will always receive a standard library with it.

 

With well-meaning intentions, this post largely refers to the current standard known as C++23. For C++20, the majority is implemented in the current compilers. We’ll emphasize things that belong to C++23 because the language support is still incomplete, but it is improving. If you use the possibilities that have been added since C++11, you will be able to learn the language faster and use it better than was the case with the previous versions up to C++98, for example.

 

With a modern compiler, C++ is definitely more enjoyable. Fortunately, most compilers are more or less up to date.

 

Gnu C++

The C++ compiler g++ from the Gnu Compiler Collection (GCC) is the compiler available on most platforms. It is also the testing ground for trying out new things, so you will almost always find new features implemented here first. Today, C++20 is very well implemented. Unfortunately, C++23 still lacks some features as of the middle of 2024, especially for ranges. On Linux, GCC is usually the first choice. Although GCC is widely used, it has a reputation for having a very complex code base. The compiled programs fall behind somewhat in terms of speed compared to paid compilers.

 

Clang++ from LLVM

As far as the code base is concerned, LLVM with the C++ compiler called Clang++ has a better reputation. The implementation of the C++20 features is exemplary; little is missing from C++23. Some new features are implemented here first. Clang++ is the standard compiler for macOS development. It is available free of charge for Linux but must be installed in addition to an existing standard library, so it is best to install g++ beforehand.

 

Microsoft Visual C++

Microsoft's product world for C++ consists (together with other tools) of Microsoft Visual C++ (MSVC), the standard library MSVC STL, and the IDEs that integrate them. These are the specialized Microsoft Visual Studio, which is only available for Windows, and the cross-platform Visual Studio Code. MSVC and MSVC STL differ only slightly from other implementations in terms of the implementation of the C++20 standard. Most of the most important features are included. In some C++23 features, this compiler is even ahead of the others.

 

Compiler in the Container

With a suitable Docker container, you can try out other compilers with your code without installing them on your system and potentially messing it up. GCC offers a whole range of ready-to-use containers. For example, you can compile a piece of source code with g++ in version 14.1, which already supports some of C++23 (https://hub.docker.com/_/gcc):

 

docker run --rm -i -t --volume $PWD:/workdir --workdir /workdir gcc:14.1 \

   g++ -std=c++23 -Wall -Wextra -pedantic myFile.cpp -o myFile.x

 

The result can usually be executed directly with ./myfile.x. Sometimes there are dynamic libraries in the container, in which case you must also execute the file in it:

 

docker run --rm -i -t --volume $PWD:/workdir --workdir /workdir gcc:14.1 \

   ./myfile.x

 

With Clang++, it works the same way, except that you use silkeh/clang:18 as the image for this compiler.

 

Editor’s note: This post has been adapted from a section of the book C++: The Comprehensive Guide by Torsten T. Will.