Solarian Programmer

My programming ramblings

Raspberry Pi - Install Clang 8 and compile C++17 programs

Posted on April 22, 2018 by Paul

Updated 24 March 2019

In this article I will show you how to install Clang 8 on your Raspberry Pi system and how to compile C++17 programs. At the time of this writing Raspbian is based on Debian Stretch, which comes with the stable but slightly outdated GCC 6.3 as the default C and C++ compiler. If you prefer to use GCC 8 I wrote an article about installing GCC 8 on Raspberry Pi.

There is also a video version of this tutorial:

Let’s start the installation process. Open a Terminal and download the official binary of Clang 8 for Raspberry Pi:

1 cd ~
2 wget

Next, extract the archive and move the extracted compilers to /usr/local:

1 tar -xf clang+llvm-8.0.0-armv7a-linux-gnueabihf.tar.xz
2 rm clang+llvm-8.0.0-armv7a-linux-gnueabihf.tar.xz
3 mv clang+llvm-8.0.0-armv7a-linux-gnueabihf clang_8.0.0
4 sudo mv clang_8.0.0 /usr/local

At this point, all you need to do is to add Clang 8 and his libraries to your system search paths:

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

the above will, temporarily, modify your paths. If you want to make the change permanent, you need to add the above line at the end of your .bashrc file:

1 echo 'export PATH=/usr/local/clang_8.0.0/bin:$PATH' >> .bashrc
2 echo 'export LD_LIBRARY_PATH=/usr/local/clang_8.0.0/lib:$LD_LIBRARY_PATH' >> .bashrc
3 source .bashrc

You can check if everything is properly setup by printing the version of the installed compiler:

1 clang++ --version

This is what I see on my Pi:

1 pi@raspberrypi:~ $ clang++ --version
2 clang version 8.0.0 (tags/RELEASE_800/final)
3 Target: armv7l-unknown-linux-gnueabihf
4 Thread model: posix
5 InstalledDir: /usr/local/clang_8.0.0/bin
6 pi@raspberrypi:~ $

If, at some point in the future, you’ll want to get rid of Clang 8 from your system, all you have to do is to remove the clang_8.0.0 folder from /usr/local, example:

1 sudo rm -rf /usr/local/clang_8.0.0

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 -Wall -pedantic if_test.cpp -o if_test

This is what I see on my Pi:

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

Next, let’s try to compile a program that uses the C++17 Filesystem:

1 #include <iostream>
2 #include <filesystem>
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, you can compile and run the code with:

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

For an overview of C++17 support in Clang see You can also check the status of the C++17 support in libc++.

If you are interested to learn more about modern C++, I recommend A Tour of C++ by Bjarne Stroustroup:

or Effective Modern C++ by Scott Meyers.

Show Comments