Cross Compiler GCC
- the compiler must know the correct target platform that the programmer is targeting
- the default compiler targets the hosts system by default
- building a cross compiler or downloading an existing cross compiler solves this problem
- cross compiler is needed to support builds for different cpu architectures and operating systems
For this project we are going to build our own GCC Cross Compiler
- os dev has guidelines for building your own cross compiler
gcc --version - to build a minimal cross compiler gcc and binutils are needed
ld --version - for detailed guidelines refer: OS DEV CROSS COMPILER GCC
Tools required for the build OS dev
replace dnf with apt for debian/ ubuntu and pacman -S for arch
- GCC - core compiler
sudo dnf install gcc - G++ - required for new gcc versions
sudo dnf install g++ - MAKE - automate the build system
sudo dnf install make - BISON - parser
sudo dnf install bison - FLEX - lexical analyzer generator
sudo dnf install flex - GMP (math library) - handles large arithmetic
sudo dnf install gmp-devel - MPFR(floating point math) - precise floating-point calculations
sudo dnf install mpfr-devel - MPC(complex math) - complex number arithmetic
sudo dnf install libmpc-devel - TEXINFO - generate GCC documentation
sudo dnf install texinfo - ISL (OPTIONAL) - advanced loop optimizations and code analysis
sudo dnf install isl-devel
Steps
# defining the path and the target
#defining the path where everything will be installed
export PREFIX="$HOME/opt/cross"
# the target architecture i686 is for 32 bit mode and the file output -elf is for flat binary output file
export TARGET=i686-elf
# adding the compiler to the path
export PATH="$PREFIX/bin:$PATH"
# make sure that the folder names are the below mentioned
# src is the source folder where the extracted source code is saved
cd $HOME/src
# compiling binutils replace x.y.z with the downloaded version
mkdir build-binutils
cd build-binutils
# target is defining the architecture to target
# prefix install location
# with sysroot enables a virtual root file system
# disable nls disables native language support
# disable werror suppresses warnigs being treated as errors
../binutils-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror
make
make install
cd $HOME/src
# compiling gcc replace x.y.z with the downloaded version
mkdir build-gcc
cd build-gcc
# target is defining the architecture to target
# prefix install location
# with sysroot enables a virtual root file system
# disable nls disables native language support
# enable languages is defining the support for the supported languages
# without headers is dont expect any existing standard libraries
# disable werror suppresses warnings being treated as errors
# disable hosted libstdcxx disables hosted c++ standard library features
../gcc-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers --disable-hosted-libstdcxx
make all-gcc
make all-target-libgcc
make all-target-libstdc++-v3
make install-gcc
make install-target-libgcc
make install-target-libstdc++-v3
## to verify the cross compiler build
$HOME/opt/cross/bin/i686-elf-gcc --version
## to make the cross compile shareable
tar -czf i686-elf-gcc.tar.gz -C $HOME/opt cross