`
jiagou
  • 浏览: 2516506 次
文章分类
社区版块
存档分类
最新评论

嵌入式开发实作(Linux内核编译及安装)

 
阅读更多

KEY:Linux 内核编译 内核配置 嵌入式

内核配置(Kernel configuration)
Makefile版本修改
为了区别基于同一源码构建(bulid)的不内核镜像,可使用变量EXTRAVERSION(定义位于makefile的顶部):

$ head -4 makefile
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 7
EXTRAVERSION = -acme1
$ head -4 makefile
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 7
EXTRAVERSION = -acme1

运行“uname --r”会返回: 2.6.7--acme1

内核配置
先定义内核需要什么特性,并进行配置。内核构建系统(The kernel build system)远不是简单用来构建整个内核和模块,想了解更多的高级内核构建选项,你可以查看 Documentation/kbuild 目录内的内核文档。

可用的配置命令和方式:

make xconfig
make menuconfig
make oldconfig
或者 手动编写
内核配置文件.config与内核编译makefile?

内核是利用make编译并安装的一个C程序。而这个C程序很现代很复杂,仅凭单一个makefile难以完成编译任务。假设内核编译只需要一个makefile,这个makefile具体也会因编译不同功能特性的内核而有所不同,也就是说在编译内核先“编译”编译的所需要的makefile,这个makefile是动态生成的。那么这个动态的makefile从何而来呢?答案是config命令通过读取[内核配置文件 ](kernel configuration file)来生成编译内核所需要所有文件(包括makefile);那[内核配置文件 ]又是哪来的呢?还是make生成的,各种make的config(xconfig/menuconfig)会生成所需要的[内核配置文件 ]。

内核配置文件(kernel configuration file)保存为内核源代码的顶层目录的.config文件。发行版的内核配置文件通常在/boot/内。

命令:make xconfig

qconf: 全新的基于QT的配置接口,2.6版本内核
更易使用(切记阅读 help -> introduction: useful options!)
具有文件浏览功能,更易的加载配置文件
命令:make menuconfig

老式字符界面,依然很管用。你够自信,完全可以手写配置文件!
命令:make oldconfig

用于升级早期发布内核的配置文件
对一些绝对符号(obsolete symbols)发出警告
询问新符号的配置值
何为makefile?

makefile包含用以构建应用程序的一组规则集(set of rules)。并且第一条[规则 ]是特殊的[规则 ],叫[默认规则 ](default rule)。一条[规则]由三部分组成:目标(target)、前提条件(prerequisites)和命令动作(command):
target: prereq1 prereq2
[tab]commands
target: prereq1 prereq2
[tab]commands

[目标 ]是被构建(made)的[文件 ]或其它东西。[前提条件 ]或者叫依赖(dependents)是构建目标的“材料”。而[命令动作 ]是利用[前提条件 ]构建[目标 ]的shell命令。

以下是编译C源码的规则例子:

foo.o: foo.c foo.h
tab]gcc -c foo.c
foo.o: foo.c foo.h
[tab]gcc -c foo.c

注意格式,冒号前是[目标 ],后是[前提条件 ];[命令 ]在第二行,并且开始于一个tab字符。

编译内核
编译和安装内核
编译步骤:

$ cd /usr/src/linux2.6
$ make
$ cd /usr/src/linux2.6
$ make

安装步骤 (logged as root!)

$ make install
$ make modules_install
$ make install
$ make modules_install

以下的步骤在2.6版本不再使用:

$ make depends
$ make modules (done by make)
$ make depends
$ make modules (done by make)

提升编译速度
多花一些时间在内核配置上,并且只编译那些你硬件需要的模块。这样可以把编译时间缩短为原来的1/30,并且节省数百MB的空间。另外,你还可以并行编译多个文件:

$ make -j <number>

make 可以并行执行多个目标(target)(KEMIN:前提是目标规则间没有交叉依赖项,这个怎么做到的?)

$ make -j 4

即便是在单处理器的工作站上也会很快,读写文件的时间被节省下来了。多线程让CPU保持忙碌。
number大于4不见得有效了,因为上下文切换过多反而降低的工作的速度。
make -j <4*number_of_processors>
内核编译tips

查看完整的 (gcc, ld)命令行: $ make V=1
清理所有的生成文件 (to create patches...): $ make mrproper
部分编译:$ make M=drivers/usb/serial
单独模块编译:$ make drivers/usb/serial/visor.ko
别处编译(假设源码在CDROM):
$ cd /mnt/cdrom/linux-2.6.17.11
$ make O=~/linux/linux-2.6.17.11
最终生成的文件
vmlinux 原始内核镜像,非压缩的
arch/<arch>/boot/zImage zlib压缩的内核镜像(Default image on arm)
arch/<arch>/boot/bzImage bzip2压缩的内核镜像。通常很小,足够放入一张软盘(Default image on i386)
安装的文件
/boot/vmlinuz-<version> 内核镜像;
/boot/System.map-<version> 保存有内核的符号地址(symbol addresses);
/boot/initrd-<version>.img Initial RAM disk:保存有你需要在引导时挂接最终根文件系统的模块。安装命令“make install”为替你运行“mkinitrd ”生成initrd;
/etc/grub.conf or /etc/lilo.conf
bootloader的配置文件:“make install”会为你的新内核更新相应的bootloader的配置文件。如果你使用的是LILO,它会在生成配置文件后,执行/sbin/lilo,让LILO的配置生效。
/lib/modules/<version>/ Kernel modules + extras
build/
为本<version>的内核添加模块所需的所有东西: .config file (build/.config), module symbol information (build/module.symVers), kernel headers (build/include/)
kernel/
内核模块文件 .ko (Kernel Object),目录结构与源代码目标一一对应。
modules.alias
模块别名记录(用于insmod和modprobe),例如:
alias sound--service--?-0 snd_mixer_oss
modules.dep
模块依赖记录(用于insmod和modprobe)
modules.symbols
标识某符号是属于哪个模块的。
这个目录的所有文件都是文本文件,可以直接查看。

小结编译及安装步骤:
编辑Makefile版本信息
定义内核特性,生成配置文件.config,用于编译:make xconfig
编译内核:make
安装内核:make install
安装模块:make modules_install
交叉编译内核
Makefile修改
通常通过修改已有的makefile获得

你必须修改目标平台,假设目标平台是ARM,修改以下:

ARCH ?= arm
CROSS_COMPILE ?= arm-linux-
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-

或运行带参数的make:

$ cd /usr/scr/linuxXX
$ make ARCH=arm CROSS_COMPILE=arm-linux-
$ cd /usr/scr/linuxXX
$ make ARCH=arm CROSS_COMPILE=arm-linux-

内核配置文件
配置过程和本地配置一样; 可以把生成的配置文件(.config)分享给其他人,比如像:

$
$ cp .config arch/<arch>/config/acme_defconfig
$
$ cp .config arch/<arch>/config/acme_defconfig

这样其他同样开发ACME系统的开发人员可以通过以下命令编译出同样的内核:

$ make acme_defconfig
$
$ make acme_defconfig
$

建立交叉编译环境(Cross--compiling setup)
假设你有ARM的交叉编译工具(cross--compiling toolchain)在 in /usr/local/arm/3.3.2/, 你得把它输出到PATH:

$ export PATH=/usr/local/arm/3.3.2/bin:$PATH
$ export PATH=/usr/local/arm/3.3.2/bin:$PATH

注意查看内核文档(在Documentation/Changes)有关最低工具版本要求。

编译并安装内核
1. $ make //如果你修改了Makefile

或者

1'. $ make ARCH=arm CROSS_COMPILE=arm-linux-

2. 拷贝 arch/<platform>/boot/zImage 到目标系统

$ make modules_install

3. 拷贝 /lib/modules/<version> 到目标系统

你可以通过 arch/<arch>/boot/install.sh 自定义安装,让”make install“自动代劳。

何为交叉编译工具链(cross--compiling toolchain)?

有如任何其它开发活动一般,嵌入式开发的第一步是建立(setting up)用于构建嵌入式Linux内核(当然包括驱动程序)及应用程序的工具链(toolchains )。不过,嵌入式开发需要是跨平台工具链。跨平台是什么意思呢?一般开发活动是在本地编译,使用是本地的工具链;而由于嵌入式的软硬资源(内存不足、没有本地编译器或操作系统都没有)限制等没法进行本地开发。需要在Linux-x86 主机(HOST)开发,使用主机的编译器生成目标(TARGET)平台代码,这个过程叫交叉编译。

我们常常说的编译器有广义和狭义之分。狭义的编译器只完软件编译(或者叫软件构建)的第一步;广义的编译器包括了软件编译(或者叫软件构建)所需要代码库(比如libc)和其它构建工具(比如汇编器和连接器)。无论是什么编译器都需要支持的代码库和各种构建工具,交叉编译也不例外。一整套广义的编译器称为交叉编译工具链。

何为工具链?

In software, a toolchain is the set of computer programs (tools) that are used to create a product (typically another computer program or system of programs). The tools may be used in a chain, so that the output of each tool becomes the input for the next, but the term is used widely to refer to any set of linked development tools.

A simple software development toolchain consists of a text editor for editing source code, a compiler and linker to transform the source code into an executable program, libraries to provide interfaces to the operating system, and a debugger.

The GNU toolchain is a blanket term for a collection of programming tools produced by the GNU Project. These tools form a toolchain (suite of tools used in a serial manner) used for developing applications and operating systems.

Projects included in the GNU toolchain are:

* GNU make: Automation tool for compilation and build;
* GNU Compiler Collection (GCC): Suite of compilers for several programming languages;
* GNU Binutils: Suite of tools including linker, assembler and other tools;
* GNU Bison: Parser generator
* GNU m4: m4 macro processor
* GNU Debugger (GDB): Code debugging tool;
* GNU build system (autotools):
o Autoconf
o Autoheader
o Automake
o Libtool
参考


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/keminlau/archive/2009/09/15/4556157.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics