linux静态添加模块

静态模块编译:
本文为实现在内核的./kernel/drivers/char添加 hello模块
wiwa@tech:~/sourcecodes/kernel/drivers/char$ mkdir hello

在hello文件夹下,创建以下文件:hello.c、Kconfig、Makefile

hello.c文件:「摘自linux kernel development」
<span style="font-size:18px;">    /*</span>
     * hello.c - The Hello, World! Kernel Module
     */  
      
    #include <linux/init.h>  
    #include <linux/module.h>  
    #include <linux/kernel.h>  
      
    /*
     * hello-init - the init function, called when the module is loaded.
     * Return zero if successfully loaded, nonzero otherwise.
     */  
     static int hello_init(void)  
     {  
        printk(KERN_ALERT "I bear a charmed life.\n");  
        return 0;  
     }  
       
     /*
      * hello_exit - the exit function, called when the module is removed.
      */  
        
     static void hello_exit (void)  
     {  
        printk(KERN_ALERT "Out, out, brief candle!\n");  
     }  
        
      module_init(hello_init);  
      module_exit(hello_exit);  
        
      MODULE_LICENSE("GPL");  
      MODULE_AUTHOR("Shakespeare");  
      MODULE_DESCRIPTION("A Hello, world Module");  

 

Kconfig文件:

#
# hello configuration
#
config HELLO
    tristate 'Create a hello module'
    help 
    We want to create the files only for testing

Makefile 文件

 #Makefile for Hello device drivers.

obj-$(CONFIG_HELLO) += hello.o

修改./kernel/drivers/char/Kconfig文件,添加以下代码:

source "drivers/char/hello/Kconfig"

修改./kernel/drivers/char/Makefile文件,添加以下代码:

obj-$(CONFIG_HELLO)         += hello/

查看配置效果

wiwa@tech:~/sourcecodes/kernel$ make gconfig

在Device drivers下找到Character Device的”Create a hello module“,选勾,保存文件。

查看配置输出,找到了刚才配置的CONFIG_HELLO参数了

wiwa@tech:~/sourcecodes/kernel$ cat .config | grep HELLO

CONFIG_HELLO=y
编译内核镜像,放在相应启动位置/boot文件夹下,从dmesg找到“hello”被编译进内核启动时的printk信息。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值