# Raspberry Pi Raspbian - Compiling GCC 8.1

## Posted on December 7, 2017 by Sol

Updated 5 May 2018

This is a short article about compiling, building, GCC 8 from sources and how to get started with C++14 and C++17 on Raspberry Pi with Raspbian. At this time Raspbian comes with the stable but slightly outdated GCC 6.3 as the default C and C++ compiler.

I’ve tested the next steps on a Raspberry Pi 3, but it should work on all current models. Fair warning, compiling GCC from source is a fairly long and intensive process and Raspberry Pi 3 tends to overheat, make sure that you have heat sinks installed or proceed at your own risk. Alternatively, you can use the binary I’ve made, you can can find it on Bitbucket.

I assume that you have a functional Raspbian installation on your Raspberry Pi. Open a Terminal and start by upgrading all your packages from the default Raspbian repository (depending on the speed of your Internet connection this could take some time):

1 sudo apt update


Building GCC 8 from sources could take some time, in my case it took about 5 hours on a Raspberry Pi 3 with a 16GB class 10 SD card and 1GB swap. If you want to avoid the wait time or if you don’t have enough available space, you can download my version from https://bitbucket.org/sol_prog/raspberry-pi-gcc-binary.

Next, let’s download the GCC 8 source and the required prerequisites. Check one of the GCC mirror sites and pick the one closest to you. In my case, I’ve used:

1 cd ~
2 wget https://ftpmirror.gnu.org/gcc/gcc-8.1.0/gcc-8.1.0.tar.gz
3 tar xf gcc-8.1.0.tar.gz
4 cd gcc-8.1.0


At this point, we can configure the build. In order to keep the system clean, we will use /usr/local/gcc-8.1.0 as the destination for the installed GCC 8. Because we want to be able to use GCC 8 while keeping GCC 6.3, we will append -8.1.0 to the resulting executables.

1 cd ~
2 mkdir build && cd build
3 ../gcc-8.1.0/configure -v --enable-languages=c,c++ --prefix=/usr/local/gcc-8.1.0 --program-suffix=-8.1.0 --with-arch=armv6 --with-fpu=vfp --with-float=hard --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf


The above will enable the C and C++ compilers, if you need other compilers like Fortran, for example, add fortran to the configure command:

1 ../gcc-8.1.0/configure -v --enable-languages=c,c++,fortran --prefix=/usr/local/gcc-8.1.0 --program-suffix=-8.1.0 --with-arch=armv6 --with-fpu=vfp --with-float=hard --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf


Now, we are ready to start the build process, as I’ve mentioned earlier this could take a few hours and will use all resources of your RPi. My advice is to restart your Pi in text mode in order to have more RAM disponible for the build process. If you didn’t increased the default swap size for your Raspberry Pi, you can do it by editing the /etc/dphys-swapfile configuration file. Be sure that CONF_SWAPSIZE is at least 1024:

1 CONF_SWAPSIZE=1024


In order to enable the above execute:

1 sudo /etc/init.d/dphys-swapfile stop
2 sudo /etc/init.d/dphys-swapfile start


Let’s build GCC now:

1 cd ~
2 cd build
3 make -j 4


The above command will try to run 4 jobs in parallel, speeding up the build. If you have any error you can try to lower the number of parallel jobs, use make -j 2 for example.

Once the build process is finished you can install the compilers with:

1 sudo make install-strip


1 export PATH=/usr/local/gcc-8.1.0/bin:$PATH  At this point, you should be able to compile any C11 or C++17 code using gcc-8.1.0 or g++-8.1.0. If you want to permanently add GCC 8 to your path simply append the above export line to the end of your .bashrc file: 1 cd ~ 2 echo 'export PATH=/usr/local/gcc-8.1.0/bin:$PATH' >> .bashrc
3 source .bashrc


In GCC 8 the default C++ standard is now -std=gnu++14, which means that by default any C++ program will be compiled for C++14. For C, the default is std=gnu11 starting from GCC 5. If you want to use the new C++17 standard use:

1 g++-8.1.0 -std=c++17 -Wall -pedantic -O2 your_program_name.cpp -o your_program_name


Let’s try to compile and run a C++14 code that uses a generalized lambda expression:

 1 #include<iostream>
2 #include<complex>
3
4 int main() {
5     // Store a generalized lambda, that squares a number, in a variable
6     auto func = [](auto input) { return input * input; };
7
8     // Usage examples:
9     // square of an int
10     std::cout << func(10) << std::endl;
11
12     // square of a double
13     std::cout << func(2.345) << std::endl;
14
15     // square of a complex number
16     std::cout << func(std::complex<double>(3, -2)) << std::endl;
17
18     return 0;
19 }


Assuming that you’ve saved the above code as lambda_test.cpp, you can compile it with:

1 g++-8.1.0 -std=c++17 -Wall -pedantic lambda_test.cpp -o lambda_test


If you run the code, this is what you should see:

1 pi@raspberrypi ~ $g++-8.1.0 -std=c++17 -Wall -pedantic lambda_test.cpp -o lambda_test 2 pi@raspberrypi ~$ ./lambda_test
3 100
4 5.49903
5 (5,-12)
6 pi@raspberrypi ~ \$


If the above worked with no error, you can do some clean up in order to save space on your Pi:

1 cd ~
2 cd rm gcc-8.1.0.tar.gz
3 cd rm -rf gcc-8.1.0
4 cd rm -rf build


If, at some point in the future, you’ll want to get rid of GCC 8 from your system, all you have to do is to remove the gcc-8.1.0 folder from /usr/local, example:

1 sudo rm -rf /usr/local/gcc-8.1.0


For an overview of C++17 support in GCC see https://gcc.gnu.org/projects/cxx-status.html.