Solarian Programmer

My programming ramblings

Raspberry Pi - Install Clang 9 and compile C++17 and C++20 programs

Posted on April 22, 2018 by Paul

Updated 24 September 2019

In this article I will show you how to install Clang 9 on your Raspberry Pi system and how to compile C++17 programs. At the time of this writing Raspbian is based on Debian Buster, which comes with the stable but slightly outdated GCC 8.3 as the default C and C++ compiler. If you prefer to use GCC 9 I wrote an article about installing GCC 9 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 9 for Raspberry Pi (works only for RPi2 and up, doesn’t work for RPi Zero):

1 cd ~
2 wget http://releases.llvm.org/9.0.0/clang+llvm-9.0.0-armv7a-linux-gnueabihf.tar.xz

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

1 tar -xvf clang+llvm-9.0.0-armv7a-linux-gnueabihf.tar.xz
2 rm clang+llvm-9.0.0-armv7a-linux-gnueabihf.tar.xz
3 mv clang+llvm-9.0.0-armv7a-linux-gnueabihf clang_9.0.0
4 sudo mv clang_9.0.0 /usr/local

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

1 export PATH=/usr/local/clang_9.0.0/bin:$PATH
2 export LD_LIBRARY_PATH=/usr/local/clang_9.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_9.0.0/bin:$PATH' >> .bashrc
2 echo 'export LD_LIBRARY_PATH=/usr/local/clang_9.0.0/lib:$LD_LIBRARY_PATH' >> .bashrc
3 . .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 9.0.0 (http://git-us.linaro.org/toolchain/jenkins-scripts.git d13bb6bafb2139131749cebad3ed0862013bb7b6)
3 Target: armv7l-unknown-linux-gnueabihf
4 Thread model: posix
5 InstalledDir: /usr/local/clang_9.0.0/bin
6 pi@raspberrypi:~ $

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

1 sudo rm -rf /usr/local/clang_9.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>
 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 -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>
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, you can compile and run the code with:

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

Clang 9 has experimental support for the new C++20 standard, at this time you’ll need to use std=c++2a in order to enforce the C++20 standard. Here is an example of using the new C++20 std::span:

 1 // C++20 span test
 2 // you can build the code with:
 3 // clang++ -std=c++2a -stdlib=libc++ -Wall -Wextra -pedantic span_test.cpp -o span_test
 4 
 5 #include <iostream>
 6 #include <vector>
 7 #include <span>
 8 
 9 void print_content(std::span<int> container) {
10     for(const auto &e : container) {
11         std::cout << e << ' ';
12     }
13     std::cout << '\n';
14 }
15 
16 int main() {
17     int a[]{23, 45, 67, 89};
18     print_content(a);
19 
20     std::vector<int> v{1, 2, 3, 4, 5};
21     print_content(v);
22 }

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

1 pi@raspberrypi:~ $ clang++ -std=c++2a -stdlib=libc++ -Wall -Wextra -pedantic span_test.cpp -o span_test
2 pi@raspberrypi:~ $ ./span_test
3 23 45 67 89
4 1 2 3 4 5
5 pi@raspberrypi:~ $

For an overview of C++20 support in Clang see https://clang.llvm.org/cxx_status.html. You can also check the status of the C++20 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