ldd学习准备
来源:互联网 发布:js模块化编程 阮一峰 编辑:程序博客网 时间:2024/06/05 13:40
内核树构建过程
安装编译内核所需要的软件(也可不装,除非你要用 make menuconfig,用make oldconfig不要)
sudo apt-get install build-essential kernel-package libncurses5-dev fakeroot
下载内核源码
先查看linux内核版本:$uname -r
网上说用apt-cache search linux-source命令, 会列出一些可选源码包,对准你的内核版本号,选择“with Ubuntu patche”的那个
最后用apt-get install linux-source-2.6.35下载之。解压缩源码包,进入解压后的源码目录。
可是我试了,搜不到,但是还是可以直接用上面的apt-get 命令下载的,但是我下载,也可以直接到这个网址下源代码,这里面有各个版本的内核,从1.0到2.6的,都有。
http://www.at.kernel.org/pub/linux/kernel/
在编译之前我们需要Ubuntu原来内核的一个配置文件,这是我/usr/src目录下的文件预览:ls -al
drwxr-xr-x 4 root root 4096 2010-09-04 21:31 fglrx-8.723.1
drwxr-xr-x 24 root root 4096 2010-09-04 20:35 linux-headers-2.6.35-22
drwxr-xr-x 7 root root 4096 2010-09-04 20:35 linux-headers-2.6.35-22-generic
drwxr-xr-x 25 root root 4096 2010-09-16 21:39 linux-source-2.6.35
-rw-r--r-- 1 root root 65846876 2010-09-01 22:41 linux-source-2.6.35.tar.bz2
现在我们需要/boot目录下的config-2.6.35-22-generic文件,我们把它拷贝到我们刚下好解压的目录,也就是linux-source-2.6.35
sudo cp /boot/config-2.6.35-22-generic /usr/src/linux-source-2.6.35/.config
接下来切换到root用户
sudo -i
cd /usr/src/linux-source-2.6.35
make menuconfig或者直接make oldconfig(无需拷贝.config)
终端会弹出一个配置界面
最后有两项:load a alternative kernel configuration...
save a alternative configuration...
选择load a kernel configuration保存,然后在选择save akernel configuration再保存退出,并退出配置环境。
接下来我们就要开始编译了。
#cd /usr/src/linux-source-2.6.35
#make
记住一定要是管理员帐号运行,这个过程很久,如果你的cpu是双核的可以在make后面加个参数,make -j4.
#make bzImage 执行结束后,可以看到在当前目录下生成了一个新的文件: vmlinux, 其属性为-rwxr-xr-x。
2、构建完内核树,就可以开始驱动程序的编写及运行了。
以下通过实现打印Hello,world。来编写第一个驱动程序
- hello.c
- #include
- #include
- MODULE_LICENSE("Dual BSD/GPL");
- static int hello_init(void)
- {
- printk(KERN_ALERT "Hello, world\n");
- return 0;
- }
- static void hello_exit(void)
- {
- printk(KERN_ALERT "Goodbye, cruel world\n");
- }
- module_init(hello_init);
- module_exit(hello_exit);
- Makefile
- ifeq ($(KERNELRELEASE),)
- # Assume the source tree is where the running kernel was built
- # You should set KERNELDIR in the environment if it's elsewhere
- KERNELDIR ?= /lib/modules/$(shell uname -r)/build
- # The current directory is passed to sub-makes as argument
- PWD := $(shell pwd)
- modules:
- $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
- modules_install:
- $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
- clean:
- rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
- .PHONY: modules modules_install clean
- else
- # called from kernel build system: just declare what our modules are
- obj-m := hello.o
- endif
以上为hello.c及Makefile文件,通过make即可得到hello.ko文件,该文件即为将要加载如内核的模块。
hello_init(void)函数将在模块被装载入内核时调用。
hello_exit(void)函数将在模块被移除时调用。
printk()函数类似于printf函数,用于信息打印。
Makefile看不懂的话,不急,先把程序Make下,接下来会专门把makefile的知识进行一次解读的,务必做到简单,每个人都会用,现在先跳过。
- printk(KERN_ALERT "Hello, world\n");
用printk,内核会根据日志级别,可能把消息打印到当前控制台上,这个控制台通常是一个字符模式的终端、一个串口打印机或是一个并口打印机。这些消息正常输出的前提是──日志输出级别小于console_loglevel(在内核中数字越小优先级越高)。
没有指定日志级别的printk语句默认采用的级别是 DEFAULT_ MESSAGE_LOGLEVEL(这个默认级别一般为<4>,即与KERN_WARNING在一个级别上),其定义在linux26/kernel/printk.c中可以找到
日志级别一共有8个级别,printk的日志级别定义如下(在include/linux/kernel.h中):
#define KERN_EMERG 0
#define KERN_ALERT 1
#define KERN_CRIT 2
#define KERN_ERR 3
#define KERN_WARNING 4
#define KERN_NOTICE 5
#define KERN_INFO 6
#define KERN_DEBUG 7
上一篇:关于数组名和字符串
下一篇:u-boot2010.06的移植(6)——支持yaffs下载
- SHTML是什么_SSI有什么用...
- shell中字符串操作
- shell中的特殊字符
- 关于java中的“错误:找不到或...
- linux设备驱动归纳总结...
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
- ldd学习准备
- 学习ldd的过程:)
- LDD学习课程之一
- LDD字符驱动学习
- ldd
- LDD
- ldd
- ldd
- ldd
- LDD学习课程之二
- LDD学习笔记(1)
- [ldd] p21讲到的如何准备一个内核源代码树
- LDD 第三章学习要点 和作业
- LDD 第四章 学习要点和作业
- LDD 第五章学习要点 和作业
- LDD 第六章学习要点 和作业
- LDD学习课程之二(2)
- LDD 第七章 学习要点和作业
- Server Error in '/' Application.错误
- 标准I/O库函数
- 【转】C语言字节对齐
- 字节对齐的位运算
- 关于数组名和字符串
- ldd学习准备
- u-boot2010.06的移植(6)——支持yaffs下载
- linux-2.6.37.4的移植(1)——移植前准备及设置分区
- works[0]——对 i.mx6 的认识
- 计算字符串数组有多少个字符串
- isdigit()函数——判断某一字符是否为数字
- GSM短消息的pdu和text模式的区别是什么?
- 短信模块开发 tpdu格式(一)
- VirtualBox虚拟vdi磁盘扩容