Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26343
  • 博文数量: 16
  • 博客积分: 672
  • 博客等级: 上士
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 22:48
文章分类

全部博文(16)

文章存档

2011年(2)

2010年(14)

我的朋友

分类: LINUX

2010-08-03 13:36:47

源码: 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后整理的,仅供参考!
阅读(321) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~