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:
[color=red]......[/color]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和其它中间编译文件,表示编译已经成功了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值