LFS第6.18章gcc-pass2编译失败:x86_64-lfs-linux-gnu-gcc未找到
LFS stable-systemd版第6.18章gcc-pass2编译失败问题排查
问题描述
我在进行LFS(Linux From Scratch)stable-systemd版本第6.18章的gcc-pass2编译时,执行x86_64-lfs-linux-gnu-gcc -dumpspecs > tmp-specs命令时提示x86_64-lfs-linux-gnu-gcc: command not found,最终导致make执行失败。
错误日志
g++ -fno-PIE -c -g -O2 -DIN_GCC -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -DHAVE_CONFIG_H -I. -I. -I../.././gcc -I../.././gcc/. -I../.././gcc/../include -I../.././gcc/../libcpp/include -I../.././gcc/../libcody -I/mnt/lfs/sources/gcc-12.2.0/host-x86_64-lfs-linux-gnu/gmp -I/mnt/lfs/sources/gcc-12.2.0/gmp -I/mnt/lfs/sources/gcc-12.2.0/host-x86_64-lfs-linux-gnu/mpfr/src -I/mnt/lfs/sources/gcc-12.2.0/mpfr/src -I/mnt/lfs/sources/gcc-12.2.0/mpc/src -I../.././gcc/../libdecnumber -I../.././gcc/../libdecnumber/dpd -I../libdecnumber -I../.././gcc/../libbacktrace -o insn-recog.o -MT insn-recog.o -MMD -MP -MF ./.deps/insn-recog.TPo insn-recog.cc x86_64-lfs-linux-gnu-gcc -dumpspecs > tmp-specs /bin/sh: line 1: x86_64-lfs-linux-gnu-gcc: command not found make[2]: *** [Makefile:2185: specs] Error 127 make[2]: *** Waiting for unfinished jobs.... rm gcc.pod make[2]: Leaving directory '/mnt/lfs/sources/gcc-12.2.0/host-x86_64-lfs-linux-gnu/gcc' make[1]: *** [Makefile:4621: all-gcc] Error 2 make[1]: Leaving directory '/mnt/lfs/sources/gcc-12.2.0' make: *** [Makefile:1036: all] Error 2
编译函数
function gcc2() { export BUILD_DIR=/mnt/lfs/sources ## This si usualy at top of file but added it for this examples sake. #https://linuxfromscratch.org/lfs/view/stable-systemd/chapter06/gzip.html cd $BUILD_DIR && tar -vxkf gcc-12.2.0.tar.xz && cd gcc-12.2.0 $BUILD_DIR/gcc-12.2.0/contrib/download_prerequisites rm -rf $BUILD_DIR/gcc-12.2.0/isl* case $(uname -m) in x86_64) sed -e '/m64=/s/lib64/lib/' -i.orig $BUILD_DIR/gcc-12.2.0/gcc/config/i386/t-linux64 ;; esac sed '/thread_header =/s/@.*@/gthr-posix.h/' -i $BUILD_DIR/gcc-12.2.0/libgcc/Makefile.in libstdc++-v3/include/Makefile.in if ! [[ -d "$BUILD_DIR/gcc-12.2.0/build" ]]; then mkdir $BUILD_DIR/gcc-12.2.0/build && cd $BUILD_DIR/gcc-12.2.0/build fi $BUILD_DIR/gcc-12.2.0/build/../configure --build=$(../config.guess) --host=$LFS_TGT --target=$LFS_TGT LDFLAGS_FOR_TARGET=-L$PWD/$LFS_TGT/libgcc --prefix=/usr --with-build-sysroot=$LFS --enable-initfini-array --disable-nls --disable-multilib --disable-decimal-float --disable-libatomic --disable-libgomp --disable-libquadmath --disable-libssp --disable-libvtv --enable-languages=c,c++ make && make DESTDIR=$LFS install && ln -sv gcc $LFS/usr/bin/cc }
环境变量
lfs:/mnt/lfs/sources/gcc-12.2.0$ env PWD=/mnt/lfs/sources/gcc-12.2.0 HOME=/home/lfs MAKEFLAGS=-j8 TERM=screen SHLVL=1 PS1=\u:\w\$ LFS_TGT=x86_64-lfs-linux-gnu LC_ALL=POSIX LFS=/mnt/lfs CONFIG_SITE=/mnt/lfs/usr/share/config.site PATH=/mnt/lfs/tools/bin:/usr/bin _=/usr/bin/env OLDPWD=/mnt/lfs/sources/gcc-12.2.0/build lfs:/mnt/lfs/sources/gcc-12.2.0$
问题原因及解决方法
核心原因
交叉编译器x86_64-lfs-linux-gnu-gcc未被系统找到,要么是gcc-pass1未正确安装到/mnt/lfs/tools/bin目录,要么是编译过程中路径逻辑存在问题。
排查与修复步骤
检查交叉编译器是否存在
执行以下命令确认/mnt/lfs/tools/bin下是否有目标编译器:ls -l /mnt/lfs/tools/bin/x86_64-lfs-linux-gnu-gcc如果输出不存在,说明gcc-pass1编译安装失败,需回到LFS第5章重新完成gcc-pass1的编译与安装。
修正编译函数的路径逻辑
原函数中,若build目录已存在,不会执行cd命令,导致后续configure在错误路径执行。修改为强制进入build目录:mkdir -p $BUILD_DIR/gcc-12.2.0/build && cd $BUILD_DIR/gcc-12.2.0/build(
mkdir -p确保目录存在,无论是否已创建)清理后重新编译
清理现有build目录,重新执行configure和make:rm -rf $BUILD_DIR/gcc-12.2.0/build mkdir -p $BUILD_DIR/gcc-12.2.0/build && cd $_ ../configure --build=$(../config.guess) --host=$LFS_TGT --target=$LFS_TGT LDFLAGS_FOR_TARGET=-L$PWD/$LFS_TGT/libgcc --prefix=/usr --with-build-sysroot=$LFS --enable-initfini-array --disable-nls --disable-multilib --disable-decimal-float --disable-libatomic --disable-libgomp --disable-libquadmath --disable-libssp --disable-libvtv --enable-languages=c,c++ make
内容的提问来源于stack exchange,提问作者Definity
相关产品推荐
相关产品推荐

