Solarian Programmer

My programming ramblings

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

Posted on December 13, 2017 by Sol

Updated 26 September 2018

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. Unfortunately, at the time of this writing, there is no official Clang binary for Ubuntu 18.04 so we’ll use a binary built by me. If you want to built it yourself, check my previous article.

If you are on Ubuntu 16.04, download the official Clang 7.0.0. Alternatively, if you are using Docker, you can install Clang 7 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:

1 sudo apt update
2 sudo apt upgrade

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

1 sudo apt install build-essential subversion cmake python3-dev libncurses5-dev libxml2-dev libedit-dev swig doxygen graphviz xz-utils

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

1 git clone https://bitbucket.org/sol_prog/clang-7-ubuntu-18.04-x86-64.git
2 cd clang-7-ubuntu-18.04-x86-64
3 tar xf clang_7.0.0.tar.xz
4 sudo mv clang_7.0.0 /usr/local

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

1 export PATH=/usr/local/clang_7.0.0/bin:$PATH
2 export LD_LIBRARY_PATH=/usr/local/clang_7.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>
 2 
 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:

1 #include <iostream>
2 #include <filesystem>
3 
4 int main() {
5     for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
6         std::cout << file.path() << '\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++fs

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++fs
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.


Show Comments