Solarian Programmer

My programming ramblings

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

Posted on April 22, 2018 by Sol

In this article I will show you how to install Clang 6 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 7 I wrote an article about installing GCC 7 on Raspberry Pi.

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

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

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

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

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

1 export PATH=/usr/local/clang_6.0.0/bin:$PATH
2 export LD_LIBRARY_PATH=/usr/local/clang_6.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_6.0.0/bin:$PATH' >> .bashrc
2 echo 'export LD_LIBRARY_PATH=/usr/local/clang_6.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 6.0.0 (tags/RELEASE_600/final)
3 Target: armv7l-unknown-linux-gnueabihf
4 Thread model: posix
5 InstalledDir: /usr/local/clang_6.0.0/bin
6 pi@raspberrypi:~ $

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

1 sudo rm -rf /usr/local/clang_6.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. Please note, that at the time of this writing the filesystem header is still marked as experimental by Clang 6.0.0 so, for now, you need to use the experimental namespace:

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

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

For an overview of C++17 support in Clang see https://clang.llvm.org/cxx_status.html. You can also check the status of the C++17 support in libc++.

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