Linux and WSL - Install Clang 9 with libc++ and compile C++17 and C++20 programs
Posted on December 13, 2017 by Paul
Updated 23 September 2019
In this article, I will show you how to install Clang with libc++ on Ubuntu Linux and Windows System for Linux. Same procedure should work on other Debian based Linux distributions. Latest version of Clang supports the C++17 standard and has experimental support for C++20.
If you want to compile Clang from sources check my previous post.
Alternatively, if you are using Docker, you can install Clang 9 in a Docker container.
Open a Terminal (on Windows 10, you can open a Command Prompt or a PowerShell window and write bash to start WSL) and make sure your system is updated:
Next, we need to install a few prerequisites for running Clang:
Download and extract latest binary of Clang, which is 9.0.0 at the time of this writing:
Next, you will need to add Clang to your system PATH:
Let’s try to compile and run a C++17 code that uses an if block with init-statement (the example is a bit silly, but it will show you how to compile C++17 programs):
Save the above code in a file named if_test.cpp and compile it with:
This is what I see on my machine:
Next, let’s try to compile a program that uses the C++17 Filesystem:
Save the above file as test_fs.cpp and compile it with:
This is what I see on my machine if I run the above code (you should see a list of files that are present in the folder where you have the executable):
Finally, let’s test if we can use C++17 std::optional:
This is what I see if I build and run the above code:
Clang 9 has experimental support for the new C++20 standard, at this time you’ll need to use std=c++2a in order to enforce the C++20 standard. Here is an example of using the new C++20 std::span:
This is what I see if I build and run the above code:
If you are interested to learn more about modern C++ I would recommend reading A tour of C++ by Bjarne Stroustrup.