Solarian Programmer

My programming ramblings

Raspberry Pi Raspbian Compiling GCC 6

Posted on June 29, 2016 by Sol

This is a short article about compiling, building, GCC 6 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 outdated GCC 4.9 as the default C and C++ compiler.

I’ve tested the next steps on a Raspberry Pi 2, but it should work on all current models.

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-get update
2 sudo apt-get upgrade

Building GCC 6 from sources could take some time, in my case it took about 12 hours on a Raspberry Pi 2 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

Next, we’ll download the GCC 6 source and prerequisites:

1 cd ~
2 wget
3 tar xf gcc-6.1.0.tar.bz2
4 cd gcc-6.1.0
5 contrib/download_prerequisites

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

1 cd ~
2 mkdir build && cd build
3 ../gcc-6.1.0/configure -v --enable-languages=c,c++ --prefix=/usr/local/gcc-6.1.0 --program-suffix=-6.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-6.1.0/configure -v --enable-languages=c,c++,fortran --prefix=/usr/local/gcc-6.1.0 --program-suffix=-6.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:


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 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

Add the new compilers to your path with:

1 export PATH=/usr/local/gcc-6.1.0/bin:$PATH

At this point, you should be able to compile any C11 or C++14 code using gcc-6.1.0 or g++-6.1.0. If you want to permanently add GCC 6 to your path simply append the above export line to the end of your .profile file.

In GCC 6 the default C++ standard is now -std=gnu++14, which means that by default any C++ program will be treated as a C++14. For C, the default is std=gnu11 starting from GCC 5.

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

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

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

1 g++-6 -Wall -pedantic lambda_test.cpp -o lambda_test

or, if you want to disable any GNU extensions and use the ISO C++14 standard:

1 g++-6 -std=c++14 -Wall -pedantic lambda_test.cpp -o lambda_test

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

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

The C++17 standard is not finalized at the time of this writing, however if you want to compile a C++ program in C++17 mode you can use:

1 g++-6 -std=c++1z -Wall -pedantic lambda_test.cpp -o lambda_test

For an overview of C++17 support in GCC see

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.

comments powered by Disqus