Chinaunix

标题: linux 2.6内模模块加载问题 [打印本页]

作者: freewildwolf    时间: 2007-03-30 17:44
标题: linux 2.6内模模块加载问题

1,一个简单的驱动程序

//hello.c
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include
#include
#include
int hello(void)
{
printk("This is a module,hello PeiJun!\n");
return 0;
}
void bye(void)
{
printk("Bye-bye!\n");
}
module_init(hello);
module_exit(bye);

2, 编写Makefile
obj-m := hello.o
KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -f hello.ko hello.mod hello.mod.o hello.o

3,执行make后,生成hello.ko hello.mod hello.mod.o hello.o

4, 执行insmod hello.o
.....
insmod: erro inserting 'hello.o' : -1 Invalid module format

2.4不会生成这些文件,只有2.6才会生成hello.ko文件,用*.ko后缀来区分内核模块与一般的目标文件,在2.4下也是在x window的终端gnome终端口加载这样的模块,printk可以正常打印,但到了2.6就不行了.文本模式下不行,主要是printk优先级的问题.内核的优先级定义可以在linux/kernel.h和kernel/printk.c中看.

5,执行insmod hello.ko
显示 This is a module,hello PeiJun!

6,lsmod查看
Module                     Size            Used by
hello                         1536           0

7,执行rmmod hello
显示: Bye-bye!



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/34739/showart_268379.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2