第一个HelloWorld模块成功编译执行!

1235阅读 0评论2011-04-01 t_bruce_yu
分类:LINUX

  1. 昨天试着学习写一个内核驱动模块,安装 kernel-package后,照着《Linux设备驱动程序第三版》的helloWorld程序写了如下源码:
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. MODULE_LICENSE("Dual BSD/GPL");
  6. static int __init hello_init(void){
  7.     printk(KERN_ALERT "hello from hello world\n");
  8.     return 0;
  9. }
  10. static void __exit hello_exit(void){
  11.     printk(KERN_ALERT "goodbye from hello world\n");
  12. }
  13. module_init(hello_init);
  14. module_exit(hello_exit);
  15. 但是上面没有给出Makefile,头疼的我只有在网上找了下,后来经过我整合后,发现如下Makefile能够通过编译
  16. 必须要注意的是,Makefile的大小写问题,如果你Makefile写成了makefile ,make一样会报错的。(所有操作在root下进行)
  17. 我的测试环境是Ubuntu10.10。然后insmod hello.ko;dmesg | tail就能看到效果啦!
    1. obj-m := hello.o
    2. all:
    3.     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
    4. clean:
    5.     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
上一篇:没有了
下一篇:使用indent进行源代码格式优化