Things required:
- Linux headers: https://cdn.kernel.org/pub/linux/kernel/
- Binutils: https://www.gnu.org/software/binutils/
- GCC: https://www.gnu.org/software/gcc/
- glibc: https://www.gnu.org/software/libc/
These variables control the target architecture, and installation directory.
export TARGET=x86_64-pc-linux-gnu
export PREFIX=$HOME/gcc/x.x.x
export TARGET_PREFIX=$PREFIX/$TARGET
export PATH=$PREFIX/bin:$PATH
cd linux-x.x.x
make ARCH=$(echo $TARGET | cut -d '-' -f 1) INSTALL_HDR_PATH=$TARGET_PREFIX headers_install
mkdir binutils-build
cd binutils-build
../binutils-x.x.x/configure --target=$TARGET --prefix=$PREFIX --disable-nls -v
make -j$(nproc) all install
May need the additional configure flag --disable-multilib
if there are no 32-bit system libraries installed on the system. Note that --with-newlib
does not refer to the newlib C library.
cd gcc-x.x.x
./contrib/download_prerequisites
cd ..
mkdir gcc-build
cd gcc-build
../gcc-x.x.x/configure --target=$TARGET --prefix=/tmp/gcc-first-stage --without-headers --with-newlib -v
make -j$(nproc) all-gcc all-target-libgcc install-gcc install-target-libgcc
cd glibc-x.x
CC=/tmp/gcc-first-stage/bin/gcc ../glibc-x.x/configure --target=$TARGET --prefix=$PREFIX --with-headers=${TARGET_PREFIX}/include --without-selinux
make -j$(nproc) all install
May need again --disable-multilib
.
cd gcc-build
rm -rf *
../gcc-x.x.x/configure --target=$TARGET --prefix=$PREFIX --enable-languages=c,c++
make -j$(nproc) all install
In order to use the new compiler together with the new glibc, call gcc/g++ with the following linking flags:
-L$PREFIX/lib -L$PREFIX/lib64 -L$TARGET_PREFIX/lib -I$PREFIX/include -I$TARGET_PREFIX/include -Wl,--rpath=$PREFIX/lib -Wl,--rpath=$PREFIX/lib64 -Wl,--rpath=$TARGET_PREFIX/lib -Wl,--dynamic-linker=$PREFIX/lib/ld-linux-x86-64.so.2
Hi Stantonparish,
Unfortunately, building the gnu toolchain from scratch is complicated, and the procedure sometimes differs from version to version. Since I wrote this gist, I've given up on building it manually and I usually resort to crosstool-ng. Although it's intended for building a cross-compiling toolchain, you can also use it to build a toolchain for the host platform.
Best of luck with it!