Chinaunix首页 | 论坛 | 博客
  • 博客访问: 231131
  • 博文数量: 77
  • 博客积分: 80
  • 博客等级: 民兵
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-06 17:15
文章分类
文章存档

2013年(4)

2012年(73)

分类: LINUX

2012-01-08 16:40:36

一.在terminal终端下执行:

#mkdir -r /home/genglaizhi/LDD/0

#cd /home/genglaizhi/LDD/0

#vi hello.c

源代码:

/*
Author:genglaizhi
Time:2012年1月8日15:47:02
ENvironment:ubuntu9
*/
//hello world模块

#include//module_exit()&_init()
#include
MODULE_LICENSE("Dual BSD/GPL");//2.6内核必须

static int hello_init(void)
{
printk(KERN_EMERG "hello\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_EMERG "goodbye\n");
}

module_init(hello_init);
module_exit(hello_exit);

保存并退出:wq

二.编辑Makefile

#vi Makefile

源代码:

ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
endif
clean:
rm -f *.o *.ko *.mod.c .hello*

三.

#make

#insmod ./hello.ko

/*

出现的问题:在终端上看不到输出hello?

*/

解决办法:

#dmesg | tail -8

会显示/var/log/messages文件的最后8行输出,能看到hello了

若再次#insmod ./hello.ko会出现“insmod:error inserting ./hello.ko : -1 File exists"错误,表示模块已经存在,

运行#lsmod会看到hello已经存在

#rmmod hello来卸载模块。

阅读(964) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:深入分析 Linux 内核链表

给主人留下些什么吧!~~