Skip to content

Instantly share code, notes, and snippets.

@GabrielL
Last active November 11, 2023 16:04
Show Gist options
  • Save GabrielL/4753066 to your computer and use it in GitHub Desktop.
Save GabrielL/4753066 to your computer and use it in GitHub Desktop.
build cross-compiling gcc
#!/bin/sh
binutils_version=2.23
gcc_version=4.7.2
gdb_version=7.5.1
newlib_version=2.0.0
export TARGET=i386-none-elf
export PREFIX=/opt/cross
# compile binutils
wget http://ftp.gnu.org/gnu/binutils/binutils-${binutils_version}.tar.gz
tar -xf binutils-${binutils_version}.tar.gz
cd binutils-${binutils_version}
./configure --target=$TARGET --prefix=$PREFIX --disable-nls --disable-multilib
make
sudo make install
cd ..
# compile gcc
wget ftp://gcc.gnu.org/pub/gcc/releases/gcc-${gcc_version}/gcc-${gcc_version}.tar.bz2
tar -xf gcc-${gcc_version}.tar.bz2
cd gcc-${gcc_version}
mkdir build
cd build
../configure --target=$TARGET --prefix=$PREFIX --disable-nls --disable-multilib --enable-languages=c,c++ \
--with-as=$PREFIX/bin/$TARGET-as --with-ld=$PREFIX/bin/$TARGET-ld --with-local-prefix=$PREFIX/$TARGET/lib \
--disable-shared --with-newlib --without-headers
make all-gcc all-target-libgcc
sudo make install-gcc install-target-libgcc
cd ../..
# compile newlib
wget ftp://sourceware.org/pub/newlib/newlib-${newlib_version}.tar.gz
tar -xf newlib-${newlib_version}.tar.gz
cd newlib-${newlib_version}
PATH=/opt/cross/bin/:$PATH ./configure --target=$TARGET --prefix=$PREFIX
cd ..
# compile libstdc++ and gcc against newlib
cd gcc-${gcc_version}/build
../configure --target=$TARGET --prefix=$PREFIX --disable-nls --disable-multilib --enable-languages=c,c++ \
--with-as=$PREFIX/bin/$TARGET-as --with-ld=$PREFIX/bin/$TARGET-ld --with-local-prefix=$PREFIX/$TARGET/lib \
--disable-shared --with-newlib
make all-gcc all-target-libgcc all-target-libstdc++-v3
sudo make install-gcc install-target-libgcc install-target-libstdc++-v3
cd ../..
# compile gdb
wget ftp://sourceware.org/pub/gdb/releases/gdb-${gdb_version}.tar.bz2
tar -xf gdb-${gdb_version}.tar.bz2
cd gdb-${gdb_version}
PATH=/opt/cross/bin/:$PATH ./configure --target=$TARGET --prefix=$PREFIX
PATH=/opt/cross/bin/:$PATH make
sudo make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment