Ubuntu 18.04安装i686-elf交叉编译工具链的方法[通俗易懂]

2022-06-26 12:02:34 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

致谢:wby大佬 一、准备

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

代码语言:javascript复制
# 切换到超级用户模式,可以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:

代码语言:javascript复制
sudo apt-get install make

二、工具链编译过程

1.下载源代码

代码语言:javascript复制
# 也可以直接在浏览器下载
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.添加环境变量

代码语言:javascript复制
export PREFIX="/usr/local"
export TARGET=i686-elf

3.编译binutils 什么是binutils?

代码语言:javascript复制
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

代码语言:javascript复制
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工具链

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133900.html原文链接:https://javaforall.cn

0 人点赞