文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>编译自己的内核模块示例+printk

编译自己的内核模块示例+printk

时间:2010-08-03  来源:seans88

源码: hello.c
  

#include <linux/kernel.h>
#include <linux/module.h>
/*
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
 #include<linux/modversions.h>
#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<linux/modversions.h>
  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后整理的,仅供参考!
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载