Linux and WSL - Install Clang 8 with libc++ and compile C++17 programs
Posted on December 13, 2017 by Paul
Updated 24 Mars 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 has partial support for the newest C++ standard, C++17.
If you want to compile Clang from sources check my previous post.
Alternatively, if you are using Docker, you can install Clang 8 in a Docker image.
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 8.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:
If you are interested to learn more about modern C++ I would recommend reading A tour of C++ by Bjarne Stroustrup.
or Effective Modern C++ by Scott Meyers.