GCC, Clang, and MSVC compilers with C++
Perfect Match
Due to the fast pace of updates of the C++ standards, compiler builders have been busy. How do the most popular compilers fit with the standards and what are the differences?
The quiet times are over for C++. A full 13 years passed between the C++98 and C++11 standards, but since then, new standards have appeared every three years, with C++14, C++17, and preliminary work on C++20. The C++ standardization committee already shows signs of enthusiasm for the next cycle.
With so many versions of C++ out in the world (see the "Blessings of Diversity" box), a developer could easily wonder, how do the compiler makers keep up with it all? This article looks at support for C++ standards in three popular compiler alternatives:
- GCC: The GNU Compiler Collection (GCC) [1] is the quintessential free software compiler. Originally created by Richard Stallman in 1987 as the compiler for the GNU project, GCC is now supported by a large community of developers and is found on almost all Linux distributions.
- Clang: This popular free compiler collection [2] uses the LLVM compiler as a back end. Clang is actually the front-end component. Clang/LLVM is designed for a high degree of compatibility with GCC. The Clang project enjoys the support of several major vendors, including Apple, Google, Microsoft, Intel, and AMD, possibly because they find Clang's permissive free software license easier to integrate with commercial projects than the GPL3 license attached to GCC.
- MSVC: This compiler is for Microsoft's Visual C++ (MSVC) environment [3]. In the past, Linux support in a major Microsoft development tool would have been unthinkable, but today, versions of MSVC run on Linux, and extensions are available for developing Linux applications on MSVC.
Blessings of Diversity
Some may wonder why so many different compilers exist. The fact that many computer architectures exist is only half the answer. The rest of the answer is that different compilers have different distinguishing features.
All three major compilers run on the x86 architecture, and each of the three has a special area in which it impresses. The Clang compiler scores points with understandable error messages and an open architecture that supports many powerful tools. GCC is distinguished by the fact that it supports most hardware architectures and is consequently gaining in importance in the embedded world with the GNU ARM Embedded Toolchain [4]. For MSVC, integration with Visual Studio adds massive value for many C++ developers.
Ultimately, this diversity in compilers is a blessing for all C++ developers. Each of the three compilers sets standards and, at the same time, sets the bar higher for the others. The fact that the compilers continue to compete for developers' favor will ultimately benefit the developers at the end of the day.
In this article, you'll also learn about some online compiler front ends that let you test your code for different compilers and standards, which often leads to valuable insights, optimizations, and troubleshooting tips.
Compilers and Standards
An estimated 95 percent of all C++ developers rely on the GCC, Clang, or MSVC compilers. The timeline in Figure 1 shows the existing C++ standards, with the exception of C++03, which was only released as a bug fix for C++98 in 2003. This article focuses on C++11, C++14, and C++17. I will avoid looking back (C++98) or into the future (C++20).
Table 1 shows which compiler versions support the C++11, C++14, and C++17 standards.
Table 1
Compiler Support
C++ Compiler | C++11 | C++14 | C++17 |
---|---|---|---|
GCC |
4.8.1 |
5.0 |
7 |
Clang |
3.3 |
3.4 |
5 |
MSVC |
19.0 |
19.1 |
19.1(in part) |
The table refers to support for the C++ core language. Sometimes that support is only partially complete. MSVC 19.1, for instance, only partially supports C++17, and this support requires the third update. Tables 2 and 3 show all the details of C++17 support [5].
Table 2
C++17 Support (Part 1)
C++17 Features | Paper | Version | GCC | Clang | MSVC |
---|---|---|---|---|---|
New auto rules for direct-list-initialization |
N3922 |
c++17-lang |
5 |
3.8 |
19.0 |
|
N3928 |
c++17-lang |
6 |
2.5 |
19.1 |
Typename in a template template parameter |
N4051 |
c++17-lang |
5 |
3.5 |
19.0 |
Removing trigraphs |
N4086 |
c++17-lang |
5.1 |
3.5 |
16.0 |
Nested namespace definition |
N4230 |
c++17-lang |
6 |
3.6 |
19.0 |
Attributes for namespaces and enumerators |
N4266 |
c++17-lang |
4.9 (Namespaces), 6 (Enumerators) |
3.6 |
19.0 |
|
N4267 |
c++17-lang |
6 |
3.6 |
19.0 |
Allow constant evaluation for all non-type template arguments |
N4268 |
c++17-lang |
6 |
3.6 |
no |
Fold expressions |
N4295 |
c++17-lang |
6 |
3.6 |
no |
Remove deprecated use of the register keyword |
P0001R1 |
c++17-lang |
7 |
3.8 |
19.1 |
Remove deprecated |
P0002R1 |
c++17-lang |
7 |
3.8 |
19.1 |
Removing deprecated exception specifications from C++17 |
P0003R5 |
c++17-lang |
7 |
4 |
no |
Make exception specifications part of the type system |
P0012R1 |
c++17-lang |
7 |
4 |
no |
Aggregate initialization of classes with base classes |
P0017R1 |
c++17-lang |
7 |
3.9 |
no |
Lambda capture of |
P0018R3 |
c++17-lang |
7 |
3.9 |
19.1 |
Using attribute namespaces without repetition |
P0028R4 |
c++17-lang |
7 |
3.9 |
19.1 |
Dynamic memory allocation for over-aligned data |
P0035R4 |
c++17-lang |
7 |
4 |
no |
Unary fold expressions and empty parameter packs |
P0036R0 |
c++17-lang |
6 |
3.9 |
no |
|
P0061R1 |
c++17-lang |
5 |
yes |
19.1 |
Table 3
C++17-Support (Part 2)
C++17 Features | Paper | Version | GCC | Clang | MSVC |
---|---|---|---|---|---|
Template argument deduction for class templates |
P0091R3 |
c++17-lang |
7 |
5 |
no |
Non-type template parameters with auto type |
P0127R2 |
c++17-lang |
7 |
4 |
no |
Guaranteed copy elision |
P0135R1 |
c++17-lang |
7 |
4 |
no |
New specification for inheriting constructors (DR1941 et al) |
P0136R1 |
c++17-lang |
7 |
3.9 |
no |
Direct-list-initialization of enumerations |
P0138R2 |
c++17-lang |
7 |
3.9 |
19.1 |
Stricter expression evaluation order |
P0145R3 |
c++17-lang |
7 |
4 |
no |
|
P0170R1 |
c++17-lang |
7 |
5 |
19.1 |
Differing begin and end types in range-based for |
P0184R0 |
c++17-lang |
6 |
3.9 |
19.1 |
|
P0188R1 |
c++17-lang |
7 |
3.9 |
19.1 |
|
P0189R1 |
c++17-lang |
7 |
3.9 |
19.1 |
Pack expansions in using-declarations |
P0195R2 |
c++17-lang |
7 |
4 |
no |
|
P0212R1 |
c++17-lang |
7 |
3.9 |
19.1 |
Structured bindings |
P0217R3 |
c++17-lang |
7 |
4 |
19.1 |
Hexadecimal floating-point literals |
P0245R1 |
c++17-lang |
3 |
yes |
no |
Ignore unknown attributes |
P0283R2 |
c++17-lang |
yes |
3.9 |
no |
|
P0292R2 |
c++17-lang |
7 |
3.9 |
19.1 |
Init-statements for |
P0305R1 |
c++17-lang |
7 |
3.9 |
19.1 |
Inline variables |
P0386R2 |
c++17-lang |
7 |
3.9* |
no |
DR: Matching of template template-arguments excludes compatible templates |
P0522R0 |
c++17-lang |
7 |
4 |
no |
Standardization of parallelism TS |
P0024R2 |
c++17 |
|
|
no |
|
N4259 |
c++17 |
6 |
3.7 |
19.0 |
Splicing maps and sets |
P0083R3 |
c++17 |
7 |
|
no |
Improving |
N4387 |
c++17 |
yes |
4 |
19.0 |
Improving |
N4387 |
c++17 |
yes |
4 |
19.0 |
Elementary string conversions |
P0067R5 |
c++17 |
|
|
no |
|
N3921 |
c++17 |
7 |
4 |
19.1 |
If you are not satisfied with the information provided on the big three compilers, or if you are interested in the C++ compilers that other Unix platforms have to offer, you can also find more detailed information on the current C++ standards at the C++ Reference website [5].
Support for the C++ standard library (STL) varies a little. There is currently no implementation of the parallel STL in C++17. You have to do some tinkering work to find a solution: The High Performance Parallex (HPX) library [6], for instance, is a framework for parallel and distributed applications that already implements the parallel STL.
Important Compiler Flags
In order to use the correct C++ standard, you need to specify it for GCC or Clang. Both support the same flags. Thus the
g++ -std=c++11 dataRace.cpp
call compiles the dataRace.cpp
file source code according to the C++11 standard. You can also specify C++14 or C++17. This step is not necessary for newer versions of GCC and Clang, but not all compiler-to-C++ standard combinations can cope without this specification, and it will not do any harm to specify. MSVC does not require you to specify the C++ standard.
There are some other interesting compiler flags for developers. For example, -O3
(for GCC and Clang) and /Ox
(for MSVC), for example, generate a maximum optimized program, and -Wall
(for GCC and Clang) or /Wall
(for MSVC) set the maximum warning level.
Sanitize
Sanitizer [7], a tool that checks whether the code correctly uses addresses, threads, and memory, has been available since GCC 4.8 and Clang 3.2. MSVC users will have to try their luck with a Windows port [8].
A small example will help illustrate the power of the Thread Sanitizer [9]. The program in Listing 1 contains code that can clearly lead to a race condition, since several threads access the globalVal
variable at the same time. More precisely, both threads try to change the variable at the same time.
Listing 1
A Simple Race Condition
Compile Thread Sanitizer into GCC and Clang by using the -fsanitize=thread
flag:
g++ -std=c++11 dataRace.cpp -fsanitize=thread -pthread -g -o dataRace
then run the program compiled here with GCC; it will generate output that identifies the potential race condition (Figure 2). At the end, the output also shows the sequence of the built-in data race.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.