title: How to build a gcc cross compiler for arm64
body: <p>On X86, I need to build a gcc cross compiler for arm64 with things as below:</p>
  
  <pre><code>binutils-2.26.1.tar.gz  gcc-5.4.0.tar.gz  glibc-2.23.tar.gz  gmp-6.1.2.tar.bz2 mpc-1.1.0.tar.gz  mpfr-4.0.1.tar.gz
  </code></pre>
  
  <p>I've put all of them into a directory named <code>download</code>.</p>
  
  <p>After reading this link: <a href="http://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/" rel="nofollow noreferrer">http://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/</a>, I wrote a bash script as below to build the gcc cross compiler:</p>
  
  <pre><code>#!/bin/bash
  
  sudo rm -rf build sources /opt/aarch64-linux-gnu-5.4.0
  
  mkdir build
  cp -r download sources
  cd sources
  for f in *.tar*; do tar xf $f; done
  
  cd gcc-5.4.0/
  ln -s ../mpfr-4.0.1 mpfr
  ln -s ../mpc-1.1.0 mpc
  ln -s ../gmp-6.1.2 gmp
  cd ../..
  
  export  PREFIX=/opt/aarch64-linux-gnu-5.4.0
  #export  PATH=$PREFIX/bin:$PATH
  export  SYSROOT=$PREFIX/sysroot
  export  TARGET=aarch64-linux-gnu
  
  cd build
  mkdir binutils &amp;&amp; cd binutils
  ../../sources/binutils-2.26.1/configure --prefix=/$PREFIX --target=$TARGET
  make -j32
  make install
  cd ..
  
  cd /home/me/projet/kernel/linux-4.1
  make ARCH=arm64 INSTALL_HDR_PATH=$PREFIX/aarch64-linux-gnu/include headers_install
  cd -
  
  mkdir gcc &amp;&amp; cd gcc
  ../../sources/gcc-5.4.0/configure --prefix=$PREFIX --target=$TARGET --enable-languages=c,c++ --without-headers
  make -j32 all-gcc
  make install-gcc
  cd ..
  
  mkdir -p glibc &amp;&amp; cd glibc
  ../../sources/glibc-2.23/configure --prefix= --build=$MACHTYPE --host=$TARGET --target=$TARGET --with-headers=$PREFIX/aarch64-linux-gnu/include libc_cv_forced_unwind=yes
  make prefix=$PREFIX/aarch64-linux-gnu install-bootstrap-headers=yes install-headers
  make -j32 csu/subdir_lib
  install csu/crt1.o csu/crti.o csu/crtn.o $PREFIX/aarch64-linux-gnu/lib
  /opt/aarch64-linux-gnu-5.4.0/bin/aarch64-linux-gnu-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o $PREFIX/aarch64-linux-gnu/lib/libc.so
  touch $PREFIX/aarch64-linux-gnu/include/gnu/stubs.h
  cd ..
  
  cd gcc
  make -j32 all-target-libgcc
  make install-target-libgcc
  cd ..
  
  cd glibc
  make -j32
  make prefix=$PREFIX/aarch64-linux-gnu install
  cd ..
  
  cd gcc
  make -j32
  make install
  cd ..
  </code></pre>
  
  <p>However, when I executed this bash script, I got two errors:</p>
  
  <pre><code>cc1: error: no include path in which to search for stdc-predef.h
  
  In file included from ../../../../sources/gcc-5.4.0/libgcc/gthr.h:148:0,
                   from ../../../../sources/gcc-5.4.0/libgcc/libgcov-interface.c:27:
  ./gthr-default.h:35:21: fatal error: pthread.h: No such file or directory
  </code></pre>