读书人

Linux内核模块编译总结

发布时间: 2012-08-19 21:09:48 作者: rapoo

Linux内核模块编译小结
这两天在看《The Linux Kernel Module Programming Guide》,自己尝试从书中的样例入手,编写简单的内核模块。在编译的过程中遇到了一些问题,总结如下。
1. 编写模块代码hello.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

static int hello_init(void)
{
printk(KERN_ALERT"hello, world./n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT"hello,world./n");
return;
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

2. 编写Makefile文件

obj-m := hello.o

default:
......make -C /usr/src/kernels/2.6.18-128.el5-i686/ M=/home/dhshuai/ modules

注意点:上面红点处必须是一个TAB空格,否则编译出错
-C后面是内核目录树,就是内核源码所在的位置,Redhat系统内核和书中的不同,
M=后面是编译后的目标所在位置,一般和hello.c置于相同的目录下

3.编译,cd到hello.c所在的目录,直接执行make命令,得到的信息为:
make -C /usr/src/kernels/2.6.18-128.el5-i686/ M=/home/dhshuai/ modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-128.el5-i686'
Building modules, stage 2.
MODPOST
make[1]: Leaving directory `/usr/src/kernels/2.6.18-128.el5-i686'

在编译目录下可看到有hello.ko hello.o和其它中间编译文件,表示编译已经成功了。






读书人网 >UNIXLINUX

热点推荐