Skip to content

Instantly share code, notes, and snippets.

Last active June 24, 2024 09:46
Show Gist options
  • Save preshing/41d5c7248dea16238b60 to your computer and use it in GitHub Desktop.
Save preshing/41d5c7248dea16238b60 to your computer and use it in GitHub Desktop.
A shell script to download packages for, configure, build and install a GCC cross-compiler.
#! /bin/bash
set -e
trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
trap 'echo FAILED COMMAND: $previous_command' EXIT
# This script will download packages for, configure, build and install a GCC cross-compiler.
# Customize the variables (INSTALL_PATH, TARGET, etc.) to your liking before running.
# If you get an error and need to resume the script from some point in the middle,
# just delete/comment the preceding lines before running it again.
# See:
CONFIGURATION_OPTIONS="--disable-multilib" # --disable-threads --disable-shared
# Download packages
export http_proxy=$HTTP_PROXY https_proxy=$HTTP_PROXY ftp_proxy=$HTTP_PROXY
wget -nc$BINUTILS_VERSION.tar.gz
wget -nc$GCC_VERSION/$GCC_VERSION.tar.gz
if [ $USE_NEWLIB -ne 0 ]; then
wget -nc -O || true
unzip -qo
wget -nc$LINUX_KERNEL_VERSION.tar.xz
wget -nc$GLIBC_VERSION.tar.xz
wget -nc$MPFR_VERSION.tar.xz
wget -nc$GMP_VERSION.tar.xz
wget -nc$MPC_VERSION.tar.gz
wget -nc$ISL_VERSION.tar.bz2
wget -nc$CLOOG_VERSION.tar.gz
# Extract everything
for f in *.tar*; do tar xfk $f; done
# Make symbolic links
ln -sf `ls -1d ../mpfr-*/` mpfr
ln -sf `ls -1d ../gmp-*/` gmp
ln -sf `ls -1d ../mpc-*/` mpc
ln -sf `ls -1d ../isl-*/` isl
ln -sf `ls -1d ../cloog-*/` cloog
cd ..
# Step 1. Binutils
mkdir -p build-binutils
cd build-binutils
make install
cd ..
# Step 2. Linux Kernel Headers
if [ $USE_NEWLIB -eq 0 ]; then
cd ..
# Step 3. C/C++ Compilers
mkdir -p build-gcc
cd build-gcc
if [ $USE_NEWLIB -ne 0 ]; then
../$GCC_VERSION/configure --prefix=$INSTALL_PATH --target=$TARGET --enable-languages=c,c++ $CONFIGURATION_OPTIONS $NEWLIB_OPTION
make $PARALLEL_MAKE all-gcc
make install-gcc
cd ..
if [ $USE_NEWLIB -ne 0 ]; then
# Steps 4-6: Newlib
mkdir -p build-newlib
cd build-newlib
../newlib-master/configure --prefix=$INSTALL_PATH --target=$TARGET $CONFIGURATION_OPTIONS
make install
cd ..
# Step 4. Standard C Library Headers and Startup Files
mkdir -p build-glibc
cd build-glibc
../$GLIBC_VERSION/configure --prefix=$INSTALL_PATH/$TARGET --build=$MACHTYPE --host=$TARGET --target=$TARGET --with-headers=$INSTALL_PATH/$TARGET/include $CONFIGURATION_OPTIONS libc_cv_forced_unwind=yes
make install-bootstrap-headers=yes install-headers
make $PARALLEL_MAKE csu/subdir_lib
install csu/crt1.o csu/crti.o csu/crtn.o $INSTALL_PATH/$TARGET/lib
$TARGET-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o $INSTALL_PATH/$TARGET/lib/
touch $INSTALL_PATH/$TARGET/include/gnu/stubs.h
cd ..
# Step 5. Compiler Support Library
cd build-gcc
make $PARALLEL_MAKE all-target-libgcc
make install-target-libgcc
cd ..
# Step 6. Standard C Library & the rest of Glibc
cd build-glibc
make install
cd ..
# Step 7. Standard C++ Library & the rest of GCC
cd build-gcc
make install
cd ..
trap - EXIT
echo 'Success!'
Copy link

How to make it works with "arm android"?

Copy link

lcbcFoo commented Mar 27, 2017

Nice script! Just had to change a few things to build a gcc cross compiler for mips.

Copy link

MitaliSinha00 commented Apr 9, 2017

I am facing the following error after make -j4:

cc1: all warnings being treated as errors
Makefile:891: recipe for target 'subsegs.o' failed
make[4]: *** [subsegs.o] Error 1
make[4]: *** Waiting for unfinished jobs....
mv -f .deps/stabs.Tpo .deps/stabs.Po
mv -f .deps/read.Tpo .deps/read.Po
mv -f .deps/symbols.Tpo .deps/symbols.Po
make[4]: Leaving directory '/home/bem/Desktop/crosscompiler/build-binutils/gas'
Makefile:2165: recipe for target 'all-recursive' failed
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory '/home/bem/Desktop/crosscompiler/build-binutils/gas'
Makefile:682: recipe for target 'all' failed
make[2]: *** [all] Error 2
make[2]: Leaving directory '/home/bem/Desktop/crosscompiler/build-binutils/gas'
Makefile:4710: recipe for target 'all-gas' failed
make[1]: *** [all-gas] Error 2
make[1]: Leaving directory '/home/bem/Desktop/crosscompiler/build-binutils'
Makefile:830: recipe for target 'all' failed
make: *** [all] Error 2

Copy link

U007D commented May 6, 2017

I'm building for a RaspberryPi3 running Raspbian, so I want to build my cross-compiler for a 32-bit Linux target. (I'm on Mac OS X Sierra 10.12.4, if that matters.) Should I use arm-linux? armv7-unknown-gnueabihf? Something else?

Fairly new to this, so in addition to knowing the proper 32-bit target identifier for the RPi3, I'd like to know how/where to find the list of valid targets.

Thank you for this and the blog article!

Copy link

U007D commented May 6, 2017

Incidentally I'm here looking at the script, because in following the steps on your blog, I got an error on Step 4.

I'm on Mac OS X Sierra 10.12.4. No errors (modulo having to unpack linux-4.4.38 to a case-sensitive filesystem in order for it to install properly) following Steps 1-3.

At Step 4, though, I got the following:

$ ../glibc-2.19/configure --prefix=/usr/local/bin/gcc-7.1.0-armv7-linux-gnueabihf/armv7-linux-gnueabihf --build=$MACHTYPE --host=armv7-linux-gnueabihf --target=armv7-linux-gnueabihf --with-headers=/usr/local/bin/gcc-7.1.0-armv7-linux-gnueabihf/include --disable-multilib libc_cv_forced_unwind=yes
checking build system type... x86_64-apple-darwin16
checking host system type... armv7-unknown-linux-gnueabihf
checking for armv7-linux-gnueabihf-gcc... /usr/local/bin/gcc-7.1.0-armv7-linux-gnueabihf/bin/armv7-linux-gnueabihf-gcc
checking whether to put _rtld_local into .sdata section... no
checking for .preinit_array/.init_array/.fini_array support... no
configure: error: Need linker with .init_array/.fini_array support.

I've tried several Google searches and a number of variations of the above configure command, but no luck.

Any ideas on how to fix this?


Some background, in case it helps:

  • Followed steps 1-4 exactly, changing only the target (armv7-unknown-gnueabihf for 32-bit ARM) and the install path (/usr/local/bin/gcc-7.1.0-armv7-unknown-gnueabihf).
  • Compiled using gcc v7.1.0 using the following versions of the gcc components to best match the kernel version used for the Raspberry Pi 3's Raspbian Linux or otherwise used the latest version:
    • binutils v2.28
    • glibc v2.19
    • gmp v6.1.2
    • isl v0.16.1
    • linux 4.4.38
    • mpc v1.0.3
    • mpfr v3.1.5

Copy link

ghost commented Dec 30, 2017

Disable libsanitizer by passing --disable-libsanitizer to GCC while configuring. That is, if you're using uClibc-ng.

If you want to compile on macOS do a Canadian Cross Compile with a macOS to Linux ABI handover. I'm not sure how to explain.

Copy link

kanj commented Apr 3, 2018

Amazing script. I was able to create a cross compiler for ARM, and then create a Kernel and BusyBox to boot up in QEMU. I have converted the script to a makefile under kanj/MAKE_CROSS_LINUX for reference.

Copy link

Very interesting script. On this basis, i create conan package

Copy link

Is it possible to build multi lib gcc i.e. with aarch64 and arm?

Copy link

This doesn't work for me trying to build gcc-4.8.5. It fails on configuring glibc (2.12.2) as it is using the newly built cross-compiled-gcc which cannot find stdio.h. I can resolve this by not exporting the PATH to the new binaries but then the csu binaries built will be for the host architecture. Not really sure how to resolve it.

Copy link

stahta01 commented Aug 8, 2020

Anyone trying to use something other than Linux as the build/host platform may be helped by looking at my fork of this script.
My Fork

I used MSys2 64 bit (a Cygwin fork) under Win7 64 bit; I yet to test the cross compiler produced; did it as a learning exercise.
I have no need to use it; but, likely will in a few months.

Edit2: Once, I deleted the installed files my version of the script stopped building with gcc/glibc errors.

Tim S.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment