源码: hello.c
#include <linux/kernel.h>
#include <linux/module.h>
/*
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#endfi
*/
MODULE_LICENSE("GPL");
int init_module()
{
printk("hello xx! - this is kernel speaking..\n");
return 0;
}
void cleanup_module()
{
printk("shor is the life of a kernel module\n");
}
|
Makefile:
obj-m := hello.o
KERNEL_SRC ?= /xxx/xxx/kernel-2.6.18/linux-2.6.18.i686(kernel src path)
all:
$(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules
clean:
rm -rf *.o *.so *.ko .*.cmd *.mod.c
|
命令:
make
insmod hello.ko
lsmod
rmmod hello
查看结果:
cat /var/log/messages
------------------------------
当然更灵活的程序写法如下:
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 /*
4 #if CONFIG_MODVERSIONS==1
5 #define MODVERSIONS
6 #include
7 #endfi
8 */
9
10 MODULE_LICENSE("GPL");
11 int hello_init()
12 {
13 printk("hello xx! - this is kernel speaking..\n");
14 return 0;
15 }
16
17 void hello_cleanup()
18 {
19 printk("shor is the life of a kernel module\n");
20 }
21 22 module_init(hello_init); 23 module_exit(hello_exit);
|
这些知识都是在学习了其他网友的blog后整理的,仅供参考!
阅读(341) | 评论(0) | 转发(0) |