Solarian Programmer

My programming ramblings

Linux and WSL - Install Clang with libc++ and compile C++17 programs

Posted on December 13, 2017 by Sol

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. In this article, we are going to use the official Clang 5.0.0 binary from

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:

1 sudo apt update
2 sudo apt upgrade

Next, we need to install a few prerequisites for running Clang:

1 sudo apt install xz-utils wget build-essential

Download and extract latest binary of Clang, which is 5.0.0 at the time of this writing:

1 wget
2 tar xf clang+llvm-5.0.0-linux-x86_64-ubuntu16.04.tar.xz
3 sudo mv clang+llvm-5.0.0-linux-x86_64-ubuntu16.04 /usr/local/clang_5.0.0

Next, you will need to add Clang to your system PATH:

1 export PATH=/clang_5.0.0/bin:$PATH
2 export LD_LIBRARY_PATH=/clang_5.0.0/lib:LD_LIBRARY_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):

 1 #include <iostream>
 3 int main() {
 4     // if block with init-statement:
 5     if(int a = 5; a < 8) {
 6         std::cout << "Local variable a is < 8\n";
 7     } else {
 8         std::cout << "Local variable a is >= 8\n";
 9     }
10     return 0;
11 }

Save the above code in a file named if_test.cpp and compile it with:

1 clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic if_test.cpp -o if_test

This is what I see on my machine:

1 ~ $ clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic if_test.cpp -o if_test
2 ~ $ ./if_test
3 Local variable a is < 8
4 ~ $ 

Next, let’s try to compile a program that uses the C++17 Filesystem. Please note, that at the time of this writing the filesystem header is still marked as experimental by Clang 5.0.0 so, for now, you need to use the experimental namespace:

1 #include <iostream>
2 #include <experimental/filesystem>
4 int main() {
5     for(auto &file : std::experimental::filesystem::recursive_directory_iterator("./")) {
6         std::cout << file << '\n';
7     }
8 }

Save the above file as test_fs.cpp and compile it with:

1 clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs -lc++experimental

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):

1 ~ $ clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs -lc++experimental
2 ~ $ ./test_fs
3 "./test_1.cpp"
4 "./test_fs"
5 "./test_0.c"
6 "./if_test"
7 "./if_test.cpp"
8 "./test_fs.cpp"
9 ~ $ 

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.

comments powered by Disqus