jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Script to download and install new GCC

Thu May 03, 2018 7:42 am

If anyone is interested in using the latest compiler on their Pi, here is a script that can download, build and install it.

Before use:-
You must check there is enough disk space, 10GB or more - a 32GB micro SD card realistically.
The script here is set up to install GCC 8.2 on a Pi3B+, Pi3, or new Pi2. For other choices
edit VERSION and uncomment the ../configure section for the correct target (and obviously comment out the Pi3 configure).
Note the Pi Zero takes a very long time to build (see below), the best is the new Pi3B+ at around 4.5 hours.
At the end, if make succeeds, you will be asked to confirm the install in /usr/local/bin. The original compiler remains in /usr/bin - further down the path, so it needs to be referred to by version (as gcc-6) or giving the full path.
You can add languages by altering --enable-languages=c,c++
Documentation at: https://gcc.gnu.org/install/configure.html

Note for Pi Zero users: if you are lucky enough to have a Pi3+ as well you can run this script on the Pi3, transfer the build tree across to the Pi Zero with tar, "cd gcc-8.2.0/obj" and run "sudo make install" - its much faster. Obviously the .configure needs to be set for the Zero.

Code: Select all

#!/bin/bash

#
#  This is the new GCC version to install.
#
VERSION=8.3.0

#
#  For the Pi or any computer with less than 2GB of memory.
#
if [ -f /etc/dphys-swapfile ]; then
  sudo sed -i 's/^CONF_SWAPSIZE=[0-9]*$/CONF_SWAPSIZE=1024/' /etc/dphys-swapfile
  sudo /etc/init.d/dphys-swapfile restart
fi

if [ -d gcc-$VERSION ]; then
  cd gcc-$VERSION
  rm -rf obj
else
  wget ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-$VERSION/gcc-$VERSION.tar.xz
  tar xf gcc-$VERSION.tar.xz
  rm -f gcc-$VERSION.tar.xz
  cd gcc-$VERSION
  contrib/download_prerequisites
fi
mkdir -p obj
cd obj

#
#  Now run the ./configure which must be checked/edited beforehand.
#  Uncomment the sections below depending on your platform.  You may build
#  on a Pi3 for a target Pi Zero by uncommenting the Pi Zero section.
#  To alter the target directory set --prefix=<dir>
#

# Pi3+, Pi3, and new Pi2
../configure --enable-languages=c,c++,fortran --with-cpu=cortex-a53 \
  --with-fpu=neon-fp-armv8 --with-float=hard --build=arm-linux-gnueabihf \
  --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --enable-checking=no

# Pi Zero's
#../configure --enable-languages=c,c++,fortran --with-cpu=arm1176jzf-s \
#  --with-fpu=vfp --with-float=hard --build=arm-linux-gnueabihf \
#  --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --enable-checking=no

# x86_64
#../configure --disable-multilib --enable-languages=c,c++,fortran --enable-checking=no

# Odroid-C2 AArch64
#../configure --enable-languages=c,c++,fortran --with-cpu=cortex-a53 --enable-checking=no

# Old Pi2
#../configure --enable-languages=c,c++,fortran --with-cpu=cortex-a7 \
#  --with-fpu=neon-vfpv4 --with-float=hard --build=arm-linux-gnueabihf \
#  --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --enable-checking=no

#
#  Now build GCC which will take a long time.  This could range from
#  4.5 hours on a Pi3B+ up to about 50 hours on a Pi Zero.  It can be
#  left to complete overnight (or over the weekend for a Pi Zero :-)
#  The most likely causes of failure are lack of disk space, lack of
#  swap space or memory, or the wrong configure section uncommented.
#  The new compiler is placed in /usr/local/bin, the existing compiler remains
#  in /usr/bin and may be used by giving its version gcc-6 (say).
#
if make -j `nproc`
then
  echo
  read -p "Do you wish to install the new GCC (y/n)? " yn
  case $yn in
   [Yy]* ) sudo make install ;;
	 * ) exit ;;
  esac
fi

#
# An alternative way of adding swap
#
#sudo dd if=/dev/zero of=/swapfile1GB bs=1G count=1
#sudo chmod 0600 /swapfile1GB
#sudo mkswap /swapfile1GB
#sudo swapon /swapfile1GB
Last edited by jahboater on Fri Feb 22, 2019 9:59 pm, edited 16 times in total.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Script for installing GCC

Thu May 03, 2018 7:51 am

Where did this script come from?
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script for installing GCC

Thu May 03, 2018 7:54 am

I wrote it.
A couple of bits (some of the config options and the wget line) came from a script I found on the net some time ago.
Comments welcome!

echmain
Posts: 252
Joined: Fri Mar 04, 2016 8:26 pm

Re: Script to download and install new GCC

Fri May 04, 2018 11:52 am

Thanks for the script!

On my 3B+ the build took a little over 9 hours using make -j 2 (and only languages c,c++ enabled).

I used only 2 cores as I didn't want to risk overheating. Mine is installed in a FLIRC case but I have no other cooling.

During the build the temp seemed to max out at 65C (it's around 47C at idle). Maybe next time I'll try -j 3.

Also, I did the build on a USB thumb drive instead of the sd card.

One question I have for you...can you do the build on a 3B+ (using Pi Zero config options) and then copy the resulting install tree to the Pi Zero?

User avatar
RaTTuS
Posts: 10504
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Script to download and install new GCC

Fri May 04, 2018 12:12 pm

if you have more than one Pi then install distcc on all machines and compile via
distcc
and don't restrict the cores
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script to download and install new GCC

Fri May 04, 2018 1:29 pm

echmain wrote:
Fri May 04, 2018 11:52 am
On my 3B+ the build took a little over 9 hours using make -j 2 (and only languages c,c++ enabled).

I used only 2 cores as I didn't want to risk overheating. Mine is installed in a FLIRC case but I have no other cooling.

During the build the temp seemed to max out at 65C (it's around 47C at idle). Maybe next time I'll try -j 3.
It will just throttle back and reduce the clock speed. But it takes quite a lot to make a 3B+ throttle.
My build stays at 1400Mhz the entire time with make -j4 (make -j `nproc`). The Pi is outside of a case with a 15mm cube heatsink on the SoC. I would have expected the FLIRC case to do better.
echmain wrote:
Fri May 04, 2018 11:52 am
One question I have for you...can you do the build on a 3B+ (using Pi Zero config options) and then copy the resulting install tree to the Pi Zero?
That is a good question, I am going to try it now. The Pi Zero takes ages :(

echmain
Posts: 252
Joined: Fri Mar 04, 2016 8:26 pm

Re: Script to download and install new GCC

Fri May 04, 2018 11:15 pm

Compiling gcc for the Pi Zero on the Pi 3B+ seemed to work fine.

I used the configure settings for the Zero as you have in your script and I used make -j 4 this time. It took a little over 6 hours and the temperature on the 3B+ maxed out at 71C. So I’m not too worried about overheating.

I’ve only compiled a few small programs on the Zero but no problem so far.

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script to download and install new GCC

Sat May 05, 2018 6:03 am

echmain wrote:
Fri May 04, 2018 11:15 pm
Compiling gcc for the Pi Zero on the Pi 3B+ seemed to work fine.
Thanks for trying that, I'll edit the original post to that effect.
echmain wrote:
Fri May 04, 2018 11:15 pm
I used the configure settings for the Zero as you have in your script and I used make -j 4 this time. It took a little over 6 hours and the temperature on the 3B+ maxed out at 71C. So I’m not too worried about overheating.
No thats fine, but it has throttled. Throttling starts at 70C where the speed drops from 1400MHz down to 1200Mhz (the speed of the old Pi3). At 80C the speed will drop again. Mine (with -j4) stays in the mid 60's and remains at 1400MHz all the time, and the build completes in around 4.5 hours. I really like the new 3B+!
I would have thought the FLIRC case would have been better than my simple heat sinks - interesting.

I used to use make -j5 to keep all four CPU's at 100% utilization (if one core is waiting for I/O another job can start), but I suspect more time is spent context switching. So make -j `nproc` in the script seems good.

Apart from a short period near the start when it swaps, the Pi is still usable for other things while the build is running.

echmain
Posts: 252
Joined: Fri Mar 04, 2016 8:26 pm

Re: Script to download and install new GCC

Sat May 05, 2018 10:18 am

Hmmm, I’m surprised at the time difference. The temp on mine maxed out at 70 but most of the time was in the upper 60s. So it was throttled to 1200 only a fraction of the time.

I put my build directory on a USB thumb drive. It seemed like a good idea to separate the disk activity but I don’t know how well USB handles parallel processes or if there’s a big speed difference between USB and the SD card.

Also, your script has —enable-checking=no. I left that out when I did the configure. The built in Raspbian package has that set to ‘release’ so I just went with the default.

About the cooling, I’m starting to think that having the 3B+ in a case at all might be a problem. Even though the FLIRC is a big heat sink, the board is still in an enclosed space. I might try one of those open cases with a small fan.
Last edited by echmain on Sat May 05, 2018 10:38 am, edited 1 time in total.

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script to download and install new GCC

Sat May 05, 2018 10:38 am

echmain wrote:
Sat May 05, 2018 10:18 am
Hmmm, I’m surprised at the time difference.
The old Pi3 at 1200Mhz used to take around 6.5 hours or so.

I hate fans!

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Script to download and install new GCC

Sat May 05, 2018 7:11 pm

While this is all well & good that we are comparing notes on how long this process takes running natively on the Pi, wouldn't it be more useful to develop and document a procedure for building the latest GCC for Pi on x86? I would imagine that these multi-hour (and, seemingly, almost multi-day) build times would convert to mere minutes if run via cross-compilation on a modern x86 (x86-64) Linux (Debian/Ubuntu) machine. Not to mention avoiding the risk of burning up one's Pi...

I've done some Google searching on it, and it seems doable - it seems most of the heavy lifting has already been done by other people. I may take a shot at it at some point, but of course, if someone has already done it, it'd be nice to be able to just use their work.

To be clear:
1) It would be really nice if there was a procedure for building (I.e., cross-compiling) gcc-for-Pi , so that you could take the result and install it on the Pi and now have the latest compiler there.

2) Or, failing that, to just be able to cross-compile whatever it is you want to compile - that is, instead of installing the new GCC on your Pi, just build whatever software you want to build on your x86 box (with the latest-and-greatest GCC) and then copy the results over to the Pi.
Last edited by n67 on Sun May 06, 2018 5:52 am, edited 1 time in total.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

ejolson
Posts: 3828
Joined: Tue Mar 18, 2014 11:47 am

Re: Script to download and install new GCC

Sun May 06, 2018 4:25 am

echmain wrote:
Sat May 05, 2018 10:18 am
Hmmm, I’m surprised at the time difference.
The time differences may also be due, in part, to the performance characteristics and ages of the respective SD cards.

Also, I would recommend against overwriting the default compiler that came with Raspbian.

User avatar
PeterO
Posts: 5152
Joined: Sun Jul 22, 2012 4:14 pm

Re: Script to download and install new GCC

Sun May 06, 2018 7:05 am

When I get time (probably not until Tuesday now) I'm going to install distcc on 2x3B and 2x3P+ and see how it performs building gcc.

I've not used distcc since I was using Gentoo on a collection of systems built from mini-itx board with VIA CPUs (which ISTR are no longer supported by the latest kernels :-( )

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script to download and install new GCC

Sun May 06, 2018 7:49 am

ejolson wrote:
Sun May 06, 2018 4:25 am
The time differences may also be due, in part, to the performance characteristics and ages of the respective SD cards.
Yes. I use these new A1 app performance class cards (sandisk), and /tmp is tmpfs (virtual memory).
Also, I would recommend against overwriting the default compiler that came with Raspbian.
Why? You can always re-install it with apt-get.
It is uncommon to have such a weird or badly written program that it specifically requires an early version of the compiler.
Its also very rare for features to be deprecated and removed from GCC.

If the build succeeds, then the compiler is always fine.

Using new features from a recent compiler is a more common problem - which I am guilty of!

You can use --prefix=<dir>

But you have to keep altering all your makefiles - its not worth it.

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script to download and install new GCC

Mon May 07, 2018 11:32 am

PeterO wrote:
Sun May 06, 2018 7:05 am
When I get time (probably not until Tuesday now) I'm going to install distcc on 2x3B and 2x3P+ and see how it performs building gcc.

I've not used distcc since I was using Gentoo on a collection of systems built from mini-itx board with VIA CPUs (which ISTR are no longer supported by the latest kernels :-( )

PeterO
I will be impressed if it works - its a pretty big project ...

Code: Select all

[email protected]:~/gcc-8.1.0 $ find . | wc -l
129893
[email protected]:~/gcc-8.1.0 $ cd obj
[email protected]:~/gcc-8.1.0/obj $ wc -l Makefile
29418 Makefile

User avatar
PeterO
Posts: 5152
Joined: Sun Jul 22, 2012 4:14 pm

Re: Script to download and install new GCC

Mon May 07, 2018 7:21 pm

Hmmmm distcc installed on 4 PIs, but when building gcc it refuses to compile on anything but the local host :-(
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
RaTTuS
Posts: 10504
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Script to download and install new GCC

Tue May 08, 2018 8:10 am

PeterO wrote:
Mon May 07, 2018 7:21 pm
Hmmmm distcc installed on 4 PIs, but when building gcc it refuses to compile on anything but the local host :-(
PeterO
are they all installed happy?
any error logs
are you using distcc as the compiler and if so how are you setting it

[I've not used distcc on the RPI's I have since jessie so not got it up an drunnig ATM
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

ejolson
Posts: 3828
Joined: Tue Mar 18, 2014 11:47 am

Re: Script to download and install new GCC

Wed May 09, 2018 7:32 am

jahboater wrote:
Sun May 06, 2018 7:49 am
ejolson wrote:
Sun May 06, 2018 4:25 am
Also, I would recommend against overwriting the default compiler that came with Raspbian.
It is uncommon to have such a weird or badly written program that it specifically requires an early version of the compiler.
The Linux kernel is such a program. One of the main purposes of the system compiler is to correctly compile the kernel.

The system compiler is also used to build all the system commands. As the startup code may change in incompatible ways from version to version, you need to be certain that the startup libraries are not overwritten. Otherwise, the entire system may cease to function. Fortunately, these libraries are now stored in subdirectories of the form

/usr/lib/gcc/arm-linux-gnueabihf/X.Y.Z

where X.Y.Z is the version of the compiler. Thus, they would not be overwritten even if you did overwrite the rest of the system compiler--unless you installed a new compiler which happened to use the same version number.

Finally, when the system compiler package is updated, apt-get upgrade will reinstall the system compiler over the version you installed from source. The package manager still thinks it owns the files even through you overwrote them with something different. Thus, it becomes dangerous to update the system because of the risk of losing the new compiler.

None of these things would happen if you installed the locally compiled compiler in /usr/local instead.

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script to download and install new GCC

Wed May 09, 2018 8:07 am

ejolson wrote:
Wed May 09, 2018 7:32 am
None of these things would happen if you installed the locally compiled compiler in /usr/local instead.
My comment in the script is wrong - thats where it gets installed by default.

Because /usr/local/bin is before /usr/bin in $PATH, the new compiler is used when you type "gcc". Which is handy for 99.9% of users as they don't need to alter makefiles.

The system compiler itself is untouched and still available - as "gcc-6".
Or, interestingly if you just type "cc" so the alternatives have not been updated.
Not sure about /usr/lib/gcc/arm-linux-gnueabihf/X.Y.Z though. I cannot see an entry for 8.1.0

User avatar
PeterO
Posts: 5152
Joined: Sun Jul 22, 2012 4:14 pm

Re: Script to download and install new GCC

Wed May 09, 2018 8:17 am

jahboater wrote:
Wed May 09, 2018 8:07 am
Not sure about /usr/lib/gcc/arm-linux-gnueabihf/X.Y.Z though. I cannot see an entry for 8.1.0
When trying to buld gcc with distcc it seemed that the makefile was going directly to /usr/lib/gcc/arm-linux-gnueabihf/* which probably bypasses the distcc mechanisms :(

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ejolson
Posts: 3828
Joined: Tue Mar 18, 2014 11:47 am

Re: Script to download and install new GCC

Wed May 09, 2018 4:35 pm

jahboater wrote:
Wed May 09, 2018 8:07 am
ejolson wrote:
Wed May 09, 2018 7:32 am
None of these things would happen if you installed the locally compiled compiler in /usr/local instead.
My comment in the script is wrong - thats where it gets installed by default.
That sounds fine then. Maybe it would be helpful to update the original post and script to indicate that the compiler will not overwrite the system compiler.


AbhiTronix
Posts: 6
Joined: Sun Sep 02, 2018 3:04 pm

Re: Script to download and install new GCC

Sun Sep 02, 2018 3:09 pm

I have created Latest Set of Precompiled Raspberry Pi GCC Cross-Compilers (Easiest Method): :P
https://github.com/abhiTronix/raspberry ... -compilers

No more Compiling/Error Handing !!! Goodluck :)

jahboater
Posts: 4843
Joined: Wed Feb 04, 2015 6:38 pm

Re: Script to download and install new GCC

Sun Sep 02, 2018 6:41 pm

Thanks!
Building GCC 8.2.0 with the given cross compiler took about 45 minutes on my Ubuntu machine on all cores. Compare this with the straight 15~20 hours needed to build GCC 8.2.0 directly on Pi 3B+(+24 hours on Rpi2) at full CPU Load plus memory swapping needed and you will see the advantage of having a cross compiler on your main machine.
It doesn't take 15~20 hours on my Pi 3B+.
Actually around 4.5 hours.
You need a good heat sink to do that of course (maintain 1.4GHz throughout the build).

AbhiTronix
Posts: 6
Joined: Sun Sep 02, 2018 3:04 pm

Re: Script to download and install new GCC

Mon Sep 03, 2018 2:35 am

It doesn't take 15~20 hours on my Pi 3B+.
Actually around 4.5 hours.
You need a good heat sink to do that of course (maintain 1.4GHz throughout the build).
Yeah, I corrected that, But actually I was using -j2 flag compilation as -j3/j4 was rather causing CPU throttling in my case and resulting in failed build :cry: , Also I compiled my GCC for C, C++, Fortran, objC, objC++, go languages all at once that in return about 15~20 hrs compilation time. That was the most frustrating thing I ever encountered in my RPI development. :evil:

That's why I created my repo, No more frustrations, 5min build/installation time only :P

Return to “C/C++”