1. 程式人生 > >Ubuntu 18.04安裝i686-elf交叉編譯工具鏈的方法

Ubuntu 18.04安裝i686-elf交叉編譯工具鏈的方法

致謝:wby大佬
一、準備

系統:Ubuntu 18.04 LTS 64位 Ubuntu releases
gcc 7.3.0、g++ 7.3.0:

# 切換到超級使用者模式,可以Ctrl+D切回普通使用者
sudo -s
add-apt-repository ppa:ubuntu-toolchain-r/test 
apt-get update
apt-get install gcc-7
apt-get install g++-7
ln -s /usr/bin/gcc-7 /usr/bin/gcc
ln -s /usr/bin/g++-7 /usr/bin/g++

gnu make 4.1:

sudo
apt-get install make

二、工具鏈編譯過程

1.下載原始碼

# 也可以直接在瀏覽器下載
wget 'https://ftp.gnu.org/gnu/binutils/binutils-2.30.tar.xz'
wget 'https://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.xz'
tar -xvJf binutils-2.30.tar.xz
tar -xvJf gcc-7.3.0.tar.xz

cd gcc-7.3.0
# 該命令會從國外網站下載相關依賴,若無法下載請自行解決網路問題
./contrib/download_prerequisites
cd
..

2.新增環境變數

export PREFIX="/usr/local"
export TARGET=i686-elf
mkdir build-binutils
cd build-binutils

# 選項說明
# --disable-nls tells binutils not to include native language support. 
  This is basically optional, but reduces dependencies and compile time. 
  It will also result in English language diagnostics.
# --with
-sysroot tells binutils to enable sysroot support in the cross-compiler by pointing it to a default empty directory. By default, the linker refuses to use sysroots for no good technical reason, while gcc is able to handle both cases at runtime. This will be useful later on. ../binutils-2.30/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror make sudo make install-strip cd ..

4.編譯gcc

mkdir build-gcc
cd build-gcc

# 選項說明
# --disable-nls is the same as for binutils above.
# --without-headers tells GCC not to rely on any C library (standard or runtime) being present for the target.
# --enable-languages tells GCC not to compile all the other language frontends it supports, but only C (and optionally C++).

../gcc-7.3.0/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
# 注意!下面兩條命令會耗費較長時間
make all-gcc
make all-target-libgcc
sudo make install-strip-gcc
sudo make install-strip-target-libgcc
cd ..

若以上過程順利完成,可以在/usr/local/bin資料夾中看到以i686-elf開頭的工具鏈,下面是我編譯好的工具鏈下載地址,需要請自取:
i686-elf工具鏈