为 ARM 编译 Linux 内核 3.2 时出错

Error when compiling Linux kernel 3.2 for ARM

我正在按照本教程为 ARM 编译 Linux 内核 3.2 并使用 QEMU 进行仿真:

https://balau82.wordpress.com/2012/03/31/compile-linux-kernel-3-2-for-arm-and-emulate-with-qemu/

我正在执行以下步骤:

  1. wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.2.tar.bz2
  2. tar xjf linux-3.2.tar.bz2
  3. export ARCH=arm
  4. export CROSS_COMPILE=arm-linux-gnueabi-
  5. cd linux-3.2
  6. make vexpress_defconfig
  7. make all
  8. cd ..
  9. arm-linux-gnueabi-gcc -static init.c -o init
  10. echo init|cpio -o --format=newc > initramfs
  11. qemu-system-arm -M vexpress-a9 -kernel linux-3.2/arch/arm/boot/zImage -initrd initramfs -serial stdio -append "console=tty1"

我的问题是当我尝试第 7 步(make all)时,出现了这个问题:

include/linux/compiler-gcc.h:94:30: fatal error: linux/compiler-gcc5.h: No such file or directory
compilation terminated.
/home/ramy/QEMU_Learn/kernel/linux-3.2/./Kbuild:35: recipe for target 'kernel/bounds.s' failed
make[1]: *** [kernel/bounds.s] Error 1
Makefile:985: recipe for target 'prepare0' failed

我正在使用Ubuntu操作系统,我正在使用Linaro工具链编译内核。

我还检查了 PATH 变量,结果如下:

$ printenv | grep PATH

XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ramy/gcc-arm-none-eabi-5_3-2016q1/bin
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path

您的内核版本 (3.2.0) 太旧,与使用的 gcc (gcc-5) 不兼容。您可以使用 gcc-4 编译内核,或者使用更新的内核版本。

你的内核在 linux/compiler-gcc.h 头文件中有特殊的包含 http://lxr.free-electrons.com/source/include/linux/compiler-gcc.h?v=3.2#L91

 91 #define __gcc_header(x) #x
 92 #define _gcc_header(x) __gcc_header(linux/compiler-gcc##x.h)
 93 #define gcc_header(x) _gcc_header(x)
 94 #include gcc_header(__GNUC__)

它将为不同的 gcc 版本包含不同的文件(GNUC 是 gcc 的主要版本)。它支持 gcc-3 和 gcc-4,但不支持 gcc-5:

http://lxr.free-electrons.com/source/include/linux/?v=3.2

C file  compiler-gcc.h  3705 bytes
C file  compiler-gcc3.h 631 bytes
C file  compiler-gcc4.h 2073 bytes
C file  compiler-intel.h    746 bytes
C file  compiler.h  8628 bytes

您可以尝试将 compiler-gcc4.h 重写为 compiler-gcc5.h,但您应该了解如何执行此操作。你可能不会直接把gcc4复制到gcc5,会出现不正确的宏。

compiler-gcc5.h 仅添加到 linux 内核版本 3.18:http://lxr.free-electrons.com/source/include/linux/compiler-gcc5.h?v=3.18 (not in 3.17 http://lxr.free-electrons.com/source/include/linux/compiler-gcc5.h?v=3.17)

较旧的 linaro 编译器(在 gcc-5 之前)是 https://releases.linaro.org/components/toolchain/binaries/4.9-2016.02/

根据 JJ Hakala 的评论,在 3.2.* 内核的最后一个版本中有 compiler-gcc5 头文件,即 3.2.81:https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.2.81.tar.gz - 只需更改 wget 命令和 tar xjf到此版本并重试。