linux 从零开始编译嵌入式Linux镜像文件 zImage
前言笔者的环境在 WIN10上安装的Ubuntu虚拟机系统: X86_64root@zzz# uname -aLinux zzz 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linuxroot@zzz#所用的 linux内核源码和gcc编译器详情如下
前言
笔者的环境
在 WIN10上安装的Ubuntu虚拟机系统: X86_64
root@zzz# uname -a
Linux zzz 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
root@zzz#
所用的 linux内核源码和gcc编译器详情如下:
root@zzz:linux_ker# ll
drwxr-xr-x 5 root root 4096 4月 30 11:38 ./
drwxrwxr-x 12 backvm backvm 4096 4月 25 23:55 ../
dr-xr-xr-x 8 backvm backvm 4096 5月 26 2013 gcc-4.6.4/
-rwxrwxrwx 1 root root 28511300 1月 8 11:39 gcc-4.6.4.tar.xz*
drwxrwxr-x 24 root root 4096 5月 1 03:32 linux-3.5.2/
-rw-r--r-- 1 root root 102302352 4月 17 20:32 linux-3.5.2.tar.gz
root@zzz:linux_ker#
root@zzz:linux_ker#
root@zzz:linux_ker# arm-linux-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gcc
COLLECT_LTO_WRAPPER=/home/backvm/work0/linux_ker/gcc-4.6.4/bin/../libexec/gcc/arm-arm1176jzfssf-linux-gnueabi/4.6.4/lto-wrapper
Target: arm-arm1176jzfssf-linux-gnueabi
Configured with: /work/builddir/src/gcc-4.6.4/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-arm1176jzfssf-linux-gnueabi --prefix=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4 --with-sysroot=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4/arm-arm1176jzfssf-linux-gnueabi/sysroot --enable-languages=c,c++ --with-arch=armv6zk --with-cpu=arm1176jzf-s --with-tune=arm1176jzf-s --with-fpu=vfp --with-float=softfp --with-pkgversion='crosstool-NG hg+default-2685dfa9de14 - tc0002' --disable-sjlj-exceptions --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --with-gmp=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-mpfr=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-mpc=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-ppl=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-cloog=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-libelf=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --enable-threads=posix --enable-target-optspace --without-long-double-128 --disable-nls --disable-multilib --with-local-prefix=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4/arm-arm1176jzfssf-linux-gnueabi/sysroot --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.6.4 (crosstool-NG hg+default-2685dfa9de14 - tc0002)
root@zzz:linux_ker#
root@zzz:linux_ker#
一、gcc目录和linux内核源码目录
解压 gcc-4.6.4.tar.xz 压缩包
root@zzz:linux_ker# cd gcc-4.6.4 # 进入 gcc目录
root@zzz:gcc-4.6.4# ll
总用量 32
dr-xr-xr-x 8 backvm backvm 4096 5月 26 2013 ./
drwxr-xr-x 5 root root 4096 4月 30 11:38 ../
dr-xr-xr-x 6 backvm backvm 4096 5月 26 2013 arm-arm1176jzfssf-linux-gnueabi/
dr-xr-xr-x 2 backvm backvm 4096 8月 13 2014 bin/
dr-xr-xr-x 2 backvm backvm 4096 5月 26 2013 include/
dr-xr-xr-x 3 backvm backvm 4096 5月 26 2013 lib/
dr-xr-xr-x 3 backvm backvm 4096 5月 26 2013 libexec/
dr-xr-xr-x 3 backvm backvm 4096 5月 26 2013 share/
root@zzz:gcc-4.6.4# ls bin # bin 目录有我们所需要的交叉编译工具 arm-linux-*
总用量 15992
arm-arm1176jzfssf-linux-gnueabi-addr2line arm-linux-ar arm-none-linux-gnueabi-as
arm-arm1176jzfssf-linux-gnueabi-ar arm-linux-as arm-none-linux-gnueabi-c++
arm-arm1176jzfssf-linux-gnueabi-as arm-linux-c++ arm-none-linux-gnueabi-cc
arm-arm1176jzfssf-linux-gnueabi-c++ arm-linux-cc arm-none-linux-gnueabi-c++filt
arm-arm1176jzfssf-linux-gnueabi-cc arm-linux-c++filt arm-none-linux-gnueabi-cpp
arm-arm1176jzfssf-linux-gnueabi-c++filt arm-linux-cpp arm-none-linux-gnueabi-ct-ng.config
arm-arm1176jzfssf-linux-gnueabi-cpp arm-linux-ct-ng.config arm-none-linux-gnueabi-elfedit
arm-arm1176jzfssf-linux-gnueabi-ct-ng.config arm-linux-elfedit arm-none-linux-gnueabi-g++
arm-arm1176jzfssf-linux-gnueabi-elfedit arm-linux-g++ arm-none-linux-gnueabi-gcc
arm-arm1176jzfssf-linux-gnueabi-g++ arm-linux-gcc arm-none-linux-gnueabi-gcc-4.6.4
arm-arm1176jzfssf-linux-gnueabi-gcc arm-linux-gcc-4.6.4 arm-none-linux-gnueabi-gcov
....
arm-arm1176jzfssf-linux-gnueabi-strip arm-none-linux-gnueabi-addr2line
arm-linux-addr2line arm-none-linux-gnueabi-ar
....
root@zzz:gcc-4.6.4# ll lib
总用量 980
dr-xr-xr-x 3 backvm backvm 4096 5月 26 2013 ./
dr-xr-xr-x 8 backvm backvm 4096 5月 26 2013 ../
dr-xr-xr-x 3 backvm backvm 4096 5月 26 2013 gcc/
-r--r--r-- 1 backvm backvm 989580 5月 26 2013 libiberty.a
root@zzz:gcc-4.6.4#
解压缩 linux-3.5.2.tar.gz 压缩包
root@zzz:linux-3.5.2# ls
arch crypto fs Kbuild MAINTAINERS modules.order REPORTING-BUGS sound virt
block Documentation include Kconfig Makefile Module.symvers samples System.map vmlinux
COPYING drivers init kernel mm net scripts tools vmlinux.o
CREDITS firmware ipc lib modules.builtin README security usr
root@zzz:linux-3.5.2#
二、添加交叉编译工具链到环境变量
root@zzz:gcc-4.6.4# pwd
/home/backvm/work0/linux_ker/gcc-4.6.4
root@zzz:gcc-4.6.4# ll
总用量 32
dr-xr-xr-x 8 backvm backvm 4096 5月 26 2013 ./
drwxr-xr-x 5 root root 4096 4月 30 11:38 ../
dr-xr-xr-x 6 backvm backvm 4096 5月 26 2013 arm-arm1176jzfssf-linux-gnueabi/
dr-xr-xr-x 2 backvm backvm 4096 8月 13 2014 bin/ # 添加 bin 目录到环境变量
dr-xr-xr-x 2 backvm backvm 4096 5月 26 2013 include/
dr-xr-xr-x 3 backvm backvm 4096 5月 26 2013 lib/ # 添加 lib 目录到环境变量
dr-xr-xr-x 3 backvm backvm 4096 5月 26 2013 libexec/
dr-xr-xr-x 3 backvm backvm 4096 5月 26 2013 share/
root@zzz:gcc-4.6.4#
环境变量生效可以仅在当前用户或者所有用户生效,两种设置选其一即可。
1)设置当前用户的环境变量
在文件最后添加两行,第一行是添加执行程序的环境变量,第二行是库文件的路径。
root@zzz:gcc-4.6.4# pwd
/home/backvm/work0/linux_ker/gcc-4.6.4
root@zzz:gcc-4.6.4# vim ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
#fi
export PATH=$PATH:/home/backvm/work0/linux_ker/gcc-4.6.4/bin # 添加 bin 目录到环境变量
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/backvm/work0/linux_ker/gcc-4.6.4/lib # 添加 lib 目录到环境变量
root@zzz:gcc-4.6.4#
root@zzz:gcc-4.6.4#
保存设置,查看版本:
root@zzz:gcc-4.6.4# source ~/.bashrc
root@zzz:gcc-4.6.4#
root@zzz:gcc-4.6.4#
root@zzz:gcc-4.6.4# arm-linux-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gcc
COLLECT_LTO_WRAPPER=/home/backvm/work0/linux_ker/gcc-4.6.4/bin/../libexec/gcc/arm-arm1176jzfssf-linux-gnueabi/4.6.4/lto-wrapper
Target: arm-arm1176jzfssf-linux-gnueabi
Configured with: /work/builddir/src/gcc-4.6.4/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-arm1176jzfssf-linux-gnueabi --prefix=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4 --with-sysroot=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4/arm-arm1176jzfssf-linux-gnueabi/sysroot --enable-languages=c,c++ --with-arch=armv6zk --with-cpu=arm1176jzf-s --with-tune=arm1176jzf-s --with-fpu=vfp --with-float=softfp --with-pkgversion='crosstool-NG hg+default-2685dfa9de14 - tc0002' --disable-sjlj-exceptions --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --with-gmp=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-mpfr=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-mpc=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-ppl=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-cloog=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-libelf=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --enable-threads=posix --enable-target-optspace --without-long-double-128 --disable-nls --disable-multilib --with-local-prefix=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4/arm-arm1176jzfssf-linux-gnueabi/sysroot --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.6.4 (crosstool-NG hg+default-2685dfa9de14 - tc0002)
root@zzz:gcc-4.6.4#
root@zzz:gcc-4.6.4#
2)设置全局用户的环境变量
步骤与设置当前用户环境变量相同,只是保存的文件不再是 ./bashrc,而是 /etc/profile。
root@zzz:gcc-4.6.4# sudo vi /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
# 依旧是添加下面的两条命令
export PATH=$PATH:/home/backvm/work0/linux_ker/gcc-4.6.4/bin # 添加 bin 目录到环境变量
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/backvm/work0/linux_ker/gcc-4.6.4/lib # 添加 lib 目录到环境
root@zzz:gcc-4.6.4#
root@zzz:gcc-4.6.4# source /etc/profile # 使环境变量生效
root@zzz:gcc-4.6.4# arm-linux-gcc -v #查看版本
Using built-in specs.
COLLECT_GCC=arm-linux-gcc
COLLECT_LTO_WRAPPER=/home/backvm/work0/linux_ker/gcc-4.6.4/bin/../libexec/gcc/arm-arm1176jzfssf-linux-gnueabi/4.6.4/lto-wrapper
Target: arm-arm1176jzfssf-linux-gnueabi
Configured with: /work/builddir/src/gcc-4.6.4/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-arm1176jzfssf-linux-gnueabi --prefix=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4 --with-sysroot=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4/arm-arm1176jzfssf-linux-gnueabi/sysroot --enable-languages=c,c++ --with-arch=armv6zk --with-cpu=arm1176jzf-s --with-tune=arm1176jzf-s --with-fpu=vfp --with-float=softfp --with-pkgversion='crosstool-NG hg+default-2685dfa9de14 - tc0002' --disable-sjlj-exceptions --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --with-gmp=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-mpfr=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-mpc=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-ppl=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-cloog=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-libelf=/work/builddir/arm-arm1176jzfssf-linux-gnueabi/buildtools --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --enable-threads=posix --enable-target-optspace --without-long-double-128 --disable-nls --disable-multilib --with-local-prefix=/opt/TuxamitoSoftToolchains/arm-arm1176jzfssf-linux-gnueabi/gcc-4.6.4/arm-arm1176jzfssf-linux-gnueabi/sysroot --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.6.4 (crosstool-NG hg+default-2685dfa9de14 - tc0002)
root@zzz:gcc-4.6.4#
三、设置交叉编译器前缀和架构的环境变量
由于笔者 ubuntu 属于 X86_64 架构,而嵌入式Linux 属于ARM 架构,所以必须设置为嵌入式 Linux开发板的 ARM架构。
root@zzz:linux-3.5.2# pwd
/home/backvm/work0/linux_ker/linux-3.5.2
root@zzz:linux-3.5.2# ls
arch crypto fs Kbuild MAINTAINERS modules.order REPORTING-BUGS sound virt
block Documentation include Kconfig Makefile Module.symvers samples System.map vmlinux
COPYING drivers init kernel mm net scripts tools vmlinux.o
CREDITS firmware ipc lib modules.builtin README security usr
root@zzz:linux-3.5.2#
root@zzz:linux-3.5.2# export CROSS_COMPILE=arm-linux-
root@zzz:linux-3.5.2# export ARCH=arm
root@zzz:linux# make s3c2410_defconfig # 此处使用 s3c2410作为基本配置
#
# configuration written to .config
#
四、开始编译,执行 make 命令
root@zzz:linux-3.5.2# make
....
/home/backvm/work0/linux_ker/gcc-4.6.4/bin/../lib/gcc/arm-arm1176jzfssf-linux-gnueabi/4.6.4/../../../../
arm-arm1176jzfssf-linux-gnueabi/bin/as: error while loading shared libraries: libz.so.1:
cannot open shared object file: No such file or directory
....
make: *** [scripts] Error 2
root@zzz:linux-3.5.2#
如果编译过程中出现以上错误" error while loading shared libraries: libz.so.1: ",其实这个问题就是你64位的操作系统没有32位的类库,如果没装就只能是报上面的错。
解决方案如下:
root@zzz:linux-3.5.2# sudo apt-get update
root@zzz:linux-3.5.2# sudo apt-get install lib32z1
运行第二个命令时有可能会说找不到或者被其它的所替代,然后再把终端列出的安装就好了。
root@zzz:linux-3.5.2# make
....
TIMEC kernel/timeconst.h
Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at kernel/timeconst.pl line 373.
....
make: *** [kernel] Error 2
root@zzz:linux-3.5.2#
如果编译过程中出现以上错误" Can’t use ‘defined(@array)’ (Maybe you should just omit the defined()?) at kernel/timeconst.pl line 373.",提示的错误信息已经明确告诉你了,你应该省略defined()。
root@zzz:linux-3.5.2# vim kernel/timeconst.pl
@val = @{$canned_values{$hz}};
if (!defined( @val )) {
@val = compute_values($hz);
}
output($hz, @val );
# 将if (!defined( @val )) 改为if (! @val ),再次编译就可以通过了。
root@zzz:linux-3.5.2#
root@zzz:linux-3.5.2# make
CHK include/generated/utsrelease.h
make[1]: 'include/generated/mach-types.h' is up to date.
CALL scripts/checksyscalls.sh
CHK include/generated/compile.h
TIMEC kernel/timeconst.h
CC kernel/time.o
CC kernel/softirq.o
CC kernel/resource.o
....
GZIP arch/arm/boot/compressed/piggy.gzip
AS arch/arm/boot/compressed/piggy.gzip.o
CC arch/arm/boot/compressed/misc.o
CC arch/arm/boot/compressed/decompress.o
CC arch/arm/boot/compressed/string.o
SHIPPED arch/arm/boot/compressed/lib1funcs.S
AS arch/arm/boot/compressed/lib1funcs.o
SHIPPED arch/arm/boot/compressed/ashldi3.S
AS arch/arm/boot/compressed/ashldi3.o
LD arch/arm/boot/compressed/vmlinux
OBJCOPY arch/arm/boot/zImage
Kernel: arch/arm/boot/zImage is ready
Building modules, stage 2.
MODPOST 361 modules
root@zzz:linux-3.5.2#
root@zzz:linux-3.5.2# ll arch/arm/boot/zImage
-rwxr-xr-x 1 root root 2372880 5月 1 03:32 arch/arm/boot/zImage* # zImage 镜像文件已生成
root@zzz:linux-3.5.2#
更多推荐
所有评论(0)