Solarian Programmer

My programming ramblings

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

Posted on December 13, 2017 by Paul

Updated 24 March 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:

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 xz-utils curl

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

1 curl -SL http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz | tar -xJC .
2 mv clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz clang_8.0.0
3 sudo mv clang_8.0.0 /usr/local

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

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

Finally, let’s test if we can use C++17 std::optional:

 1 // C++17 optional test.
 2 // You can compile the code with:
 3 // clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_optional.cpp -o test_optional
 4 #include <iostream>
 5 #include <optional>
 6 
 7 void show_optional_value(const std::optional<int> &o) {
 8     // Print the optional has a value if it is not empty
 9     if(o) {
10         std::cout << "Optional value is: " << *o << '\n';
11     } else {
12         std::cout << "Optional is empty\n";
13     }
14 }
15 int main() {
16     // Create an empty optional
17     std::optional<int> o1;
18 
19     // Show the optional value
20     show_optional_value(o1);
21 
22     // Store a value in the above optional variable
23     o1 = -33;
24 
25     // Show the optional value
26     show_optional_value(o1);
27 }

This is what I see if I build and run the above code:

1 ~ $ clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_optional.cpp -o test_optional
2 ~ $ ./test_optional
3 Optional is empty
4 Optional value is: -33
5 ~ $

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