helloworld.ko

helloworld模块:

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$vim hello.c

1 /*======================================================================
  2 A simple kernel module: "hello world"
  3 
  4 The initial developer of the original code is Baohua Song
  5 <author@linuxdriver.cn>. All Rights Reserved.
  6 ======================================================================*/
  7 #include <linux/init.h>
  8 #include <linux/module.h>
  9 MODULE_LICENSE("Dual BSD/GPL");
 10 static int hello_init(void)
 11 {
 12 printk(KERN_INFO " Hello World enter\n");
 13 return 0;
 14 }
 15 
 16 static void hello_exit(void)
 17 {
 18 printk(KERN_INFO " Hello World exit\n ");
 19 }
 20 
 21 module_init(hello_init);
 22 module_exit(hello_exit);
 23 
 24 MODULE_AUTHOR("Song Baohua");
 25 MODULE_DESCRIPTION("A simple Hello World Module");
 26 MODULE_ALIAS("a simplest module");


xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$vim Makefile

1 obj-m := hello.o
  2 
  3 clean: 
  4 rm -rf *.ko *.mod.c *.o *.order *.symvers


编译模块:
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$make -C /usr/src/linux-headers-2.6.35-22-generic/ M=$(pwd) modules
make:进入目录'/usr/src/linux-headers-2.6.35-22-generic'
  CC [M]  /home/xxha/share/learning/songbaohua/4_char_device_driver/helloworld/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/xxha/share/learning/songbaohua/4_char_device_driver/helloworld/hello.mod.o
  LD [M]  /home/xxha/share/learning/songbaohua/4_char_device_driver/helloworld/hello.ko
make:离开目录“/usr/src/linux-headers-2.6.35-22-generic”

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$sudo insmod hello.ko

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$tail -n 2 /var/log/messages
Jan 24 11:13:15 xxha-OptiPlex-780 kernel: [ 6909.919001]  book num:5000
Jan 24 13:38:43 xxha-OptiPlex-780 kernel: [15638.098334]  Hello World enter

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$modinfo hello.ko    //查询模块的信息。
filename:       hello.ko
alias:          a simplest module
description:    A simple Hello World Module
author:         Song Baohua
license:        Dual BSD/GPL
srcversion:     AF31C8A811E72E11F852B3E
depends:       
vermagic:       2.6.35.4 SMP mod_unload modversions

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$sudo rmmod hello.ko
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/4_char_device_driver/helloworld$tail -n 2 /var/log/messages
Jan 24 13:38:43 xxha-OptiPlex-780 kernel: [15638.098334]  Hello World enter
Jan 24 13:40:34 xxha-OptiPlex-780 kernel: [15748.893361]  Hello World exit

这就是helloworld.ko, 超简单。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值