Skip to content

通过知识获得解放,通过技术获得自由

Menu
  • 专题目录
  • 液压相关
    • 液压技术
    • 液压相邻技术
    • 液压应用
  • 计算机相关
    • 计算机和软件
    • 网络和网站技术
  • 哲学
  • 关于本站
Menu

搭建编译交叉工具链[0]

Posted on 2023年11月14日 by

此处在宿主机host以lfs用户运行

本部分对应原书第五章内容

第二次编译耗时6h,以下需要安装四个软件包,其中gcc需要安装两次

1)编译Binutils

该软件包包含链接器、汇编器,以及其他处理目标文件的工具

cd $LFS/sources
tar xvf binutils-2.37.tar.xz
cd binutils-2.37
mkdir -v build
cd       build
../configure --prefix=$LFS/tools \
--with-sysroot=$LFS \
--target=$LFS_TGT \
--disable-nls \
--disable-werror
make -j4
make install -j1

cd ..
rm -rf binutils-2.37

2)GCC安装

这个软件包是 GNU 编译器的集合。它包含 C 和 C++ 的编译器,以及其他一些在 LFS 中不会涉及的编译器。

GCC 依赖于 GMP、MPFR 和 MPC 这三个包。

tar xvf gcc-11.2.0.tar.xz
cd gcc-11.2.0

tar -xf ../mpfr-4.1.0.tar.xz
mv -v mpfr-4.1.0 mpfr
tar -xf ../gmp-6.2.1.tar.xz
mv -v gmp-6.2.1 gmp
tar -xf ../mpc-1.2.1.tar.gz
mv -v mpc-1.2.1 mpc

case $(uname -m) in
  x86_64)
    sed -e '/m64=/s/lib64/lib/' \
        -i.orig gcc/config/i386/t-linux64
 ;;
esac

mkdir -v build
cd       build
../configure                                       \
    --target=$LFS_TGT                              \
    --prefix=$LFS/tools                            \
    --with-glibc-version=2.11                      \
    --with-sysroot=$LFS                            \
    --with-newlib                                  \
    --without-headers                              \
    --enable-initfini-array                        \
    --disable-nls                                  \
    --disable-shared                               \
    --disable-multilib                             \
    --disable-decimal-float                        \
    --disable-threads                              \
    --disable-libatomic                            \
    --disable-libgomp                              \
    --disable-libquadmath                          \
    --disable-libssp                               \
    --disable-libvtv                               \
    --disable-libstdcxx                            \
    --enable-languages=c,c++
    
    
make -j4
make install

cd ..

cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
`dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/install-tools/include/limits.h


cd ..
rm -rf gcc-11.2.0

3)Linux-5.13.12 API 头⽂件

这个软件包就是操作系统.

Linux 内核需要导出一个应用程序编程接口 (API) 供系统的 C 运行库 (例如 LFS 中的 Glibc) 使用。这通过净化内核源码包中提供的若干 C 头文件完成

tar xvf linux-5.13.12.tar.xz
cd linux-5.13.12
make mrproper
make headers
find usr/include -name '.*' -delete
rm usr/include/Makefile
cp -rv usr/include $LFS/usr

cd ..
rm -rf linux-5.13.12

4)安装Glibc

这个软件包包含主要的 C 语言库。Linux 程序没有该软件包的支持根本无法运行

tar xvf glibc-2.34.tar.xz
cd glibc-2.34
case $(uname -m) in
    i?86)   ln -sfv ld-linux.so.2 $LFS/lib/ld-lsb.so.3
    ;;
    x86_64) ln -sfv ../lib/ld-linux-x86-64.so.2 $LFS/lib64
            ln -sfv ../lib/ld-linux-x86-64.so.2 $LFS/lib64/ld-lsb-x86-64.so.3
    ;;
esac

patch -Np1 -i ../glibc-2.34-fhs-1.patch

mkdir -v build
cd       build

echo "rootsbindir=/usr/sbin" > configparms

../configure                             \
      --prefix=/usr                      \
      --host=$LFS_TGT                    \
      --build=$(../scripts/config.guess) \
      --enable-kernel=3.2                \
      --with-headers=$LFS/usr/include    \
      libc_cv_slibdir=/usr/lib
      
make -j4
make DESTDIR=$LFS install

sed '/RTLDLIST=/s@/usr@@g' -i $LFS/usr/bin/ldd

#检查环境
echo 'int main(){}' > dummy.c
$LFS_TGT-gcc dummy.c
readelf -l a.out | grep '/ld-linux'

rm -v dummy.c a.out


cd ..
rm -rf glibc-2.34

5)完成 limits.h 头⽂件的安装

$LFS/tools/libexec/gcc/$LFS_TGT/11.2.0/install-tools/mkheaders

6)安装Libstdc++(属于GCC的一部分,本部分成果直接用于lfs系统中)

tar xvf gcc-11.2.0.tar.xz
cd gcc-11.2.0
rm -rf build
mkdir build
cd build
../libstdc++-v3/configure           \
    --host=$LFS_TGT                 \
    --build=$(../config.guess)      \
    --prefix=/usr                   \
    --disable-multilib              \
    --disable-nls                   \
    --disable-libstdcxx-pch         \
    --with-gxx-include-dir=/tools/$LFS_TGT/include/c++/11.2.0

make -j4
make DESTDIR=$LFS install

cd ..
rm -rf gcc-11.2.0

参考资料:

  1. https://bf.mengyan1223.wang/lfs/zh_CN/11.0/chapter05/introduction.html
  2. https://www.linuxfromscratch.org/lfs/view/stable-systemd/chapter05/introduction.html

欢迎回来

希望本站对你有所帮助!

如有疑问请联系info@fanlog.org
2023 年 11 月
一二三四五六日
 12345
6789101112
13141516171819
20212223242526
27282930 
« 6 月    

AI辅助 (17)

© 2025 | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT