Chinaunix首页 | 论坛 | 博客
  • 博客访问: 531253
  • 博文数量: 104
  • 博客积分: 2089
  • 博客等级: 大尉
  • 技术积分: 1691
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-29 08:48
文章分类

全部博文(104)

文章存档

2015年(1)

2013年(13)

2012年(31)

2011年(59)

分类: C/C++

2012-10-13 12:25:20

在上一篇《 linux内核编译环境配置 》中我给出了个单文件helloword的测试文件,如果多个文件怎么做。
先把前一篇贴出来。 

经典的hello word测试

点击(此处)折叠或打开

  1. ////# cat hello.c
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. static int __init hl_init( void )
  6. {
  7.     printk("Hello,World! init\n");
  8.     return 0;
  9. }
  10. static void __exit hl_cleanup( void )
  11. {
  12.     printk("Goodbye, World! cleanup\n");

  13. }
  14. module_init(hl_init);
  15. module_exit(hl_cleanup);
  16. MODULE_LICENSE("GPL");
经典的由单个c文件产生模块的Makefile。

点击(此处)折叠或打开

  1. # cat Makefile
  2. obj-+= hello.o
  3. CURRENT_PATH := $(shell pwd) #模块所在的当前路径
  4. LINUX_KERNEL := $(shell uname -r) #Linux内核源代码的当前版本
  5. LINUX_KERNEL_PATH := /usr/src/kernels/$(LINUX_KERNEL) #Linux内核源代码的绝对路径
  6. all:
  7.         make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules #编译模块了
  8. clean:
  9.         make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean #清理
编译
Make
就产生了hello.ko。
安装 
insmod hello.ko
卸载 
 rmmod hello
查看log 
 dmesg

点击(此处)折叠或打开

    1. ................
    2. [12238.051159] Hello,World! init
    3. [12242.458122] Goodbye, World! cleanup
[]中的是时间戳。

多文件
我升级下,两个文件,hello.c和timer.c ,就是每隔一秒输出点东西来,开始输出hello init, 退出时输出exit。
hello.c

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>

  4. extern void timer_exit(void);
  5. extern int timer_init(void);

  6. static int __init hl_init( void )
  7. {
  8.     printk("Hello,World! init\n");
  9.     timer_init();
  10.     return 0;
  11. }
  12. static void __exit hl_cleanup( void )
  13. {
  14.     timer_exit();
  15.     printk("Goodbye, World! cleanup\n");

  16. }
  17. module_init(hl_init);
  18. module_exit(hl_cleanup);
  19. MODULE_LICENSE("GPL");
timer.c

点击(此处)折叠或打开

  1. #include <linux/timer.h>

  2. static struct timer_list my_timer;

  3. //定时函数
  4. void tm_say(unsigned long arg){
  5.     printk( "timer do >>>>>>\n");
  6.     mod_timer(&my_timer,jiffies+HZ);
  7. }

  8. //初始化模块和定时器
  9. int timer_init(void)
  10. {
  11.     init_timer(&my_timer);
  12.     my_timer.data=0;
  13.     my_timer.function =tm_say;
  14.     my_timer.expires = jiffies+HZ;
  15.     //定时一秒钟
  16.     add_timer(&my_timer);
  17.     printk(KERN_EMERG "timer_k module inserted\n");
  18.     return 0;
  19. }

  20. void timer_exit(void)
  21. {
  22.     del_timer(&my_timer);
  23.     printk("timer_k module exited\n");
  24. }
Makefile

点击(此处)折叠或打开

  1. obj-m := hhh.o
  2. hhh-objs := hello.o timer.o

  3. KERNELBUILD := /lib/modules/`uname -r`/build
  4. default:
  5.         echo " BUILD kmod"
  6.         make -C $(KERNELBUILD) M=$(shell pwd) modules

  7. clean:
  8.         make -C $(KERNELBUILD) M=$(shell pwd) clean
关键就是,target_name后面的"-objs"的指引。
编吧,make , insmod hhh.ko 等下 再 rmmod hhh 看看 dmes 

点击(此处)折叠或打开

  1. [16324.230095] Hello,World! init
  2. [16324.230095] timer_k module inserted
  3. [16325.232644] timer do >>>>>>
  4. [16326.237437] timer do >>>>>>
  5. [16327.244518] timer do >>>>>>
  6. [16328.247633] timer do >>>>>>
  7. [16329.248125] timer do >>>>>>
  8. [16329.864092] timer_k module exited
  9. [16329.864092] Goodbye, World! cleanup

OK了。
 


阅读(1122) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~