博客
关于我
在linux内核里面新添加一个驱动模块
阅读量:640 次
发布时间:2019-03-14

本文共 1533 字,大约阅读时间需要 5 分钟。

构建测试模块:hello

1. 在linux-3.4/drivers/下新建目录hello

linux-3.4/drivers/目录中执行以下命令:

cd linux-3.4/drivers/
mkdir hello

1.2 在hello/下新建hello.c Makefile Kconfig三个文件

hello.c
// 所有模块都需要的头文件
#include
#include
#include
#define MODULE_LICENSE "GPL"
#define MODULE_AUTHOR "baoli"
#define MODULE_DESCRIPTION "hello world module"
static int __init hello_init(void) {
printk(KERN_WARNING "hello world.\n");
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_WARNING "hello exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile
# 如果需要在用户空间编译为模块(m)
# 如果需要直接编译成内核模块(y)
# 如果不需要编译(n)
# 例如:make menuconfig ARCH=arm
Kconfig
menu "HELLO TEST Driver"
comment "HELLO TEST Driver Config"
config HELLO
tristate "hello module test"
default m
help
这是hello测试驱动
endmenu

1.3 修改上一级目录的Kconfig和Makefile

进入linux-3.4/drivers/目录,对MakefileKconfig进行修改:

  • 修改Makefile:
  • obj-$(CONFIG_HELLO) += hello/
    1. 修改Kconfig:
    2. source "drivers/hello/Kconfig"

      (注意:某些内核版本可能需要在arch/arm/Kconfig中添加相同内容)


      2. 编译

      完成以上设置后,进入菜单配置并进行编译:

      2.1 选择编译选项

      make menuconfig ARCH=arm

      进入“Device Drivers”选项,您可以看到新增的“HELLO TEST Driver”选项。进入该选项后,您可以选择以下编译选项:

      • 编译成模块
      • 编译进内核
      • 不编译

      2.2 编译结果

      • 编译成模块
        :编译过程会输出以下内容:

        LD drivers/hello/built-in.o
        CC [M] drivers/hello/hello.o
        CC drivers/hello/hello.mod.o
        LD [M] drivers/hello/hello.ko
      • 编译进内核
        :编译过程会输出以下内容:

        CC drivers/hello/hello.o
        LD drivers/hello/built-in.o

      编译完成后,drivers/hello/目录中会生成hello.ohello.ko文件。同时,/linux-3.4/output/lib/modules/3.4.39/目录中也会包含生成的hello.ko文件。


      希望以上内容能为您的内核开发提供帮助!如果需要进一步优化或者有其他问题,请随时联系!

    转载地址:http://mpflz.baihongyu.com/

    你可能感兴趣的文章
    MySQL集群解决方案(4):负载均衡
    查看>>
    mysql颠覆实战笔记(八)--mysql的自定义异常处理怎么破
    查看>>
    MySQL高级-MySQL并发参数调整
    查看>>
    MySQL高级-视图
    查看>>
    MySQL:判断逗号分隔的字符串中是否包含某个字符串
    查看>>
    Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
    查看>>
    Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Nacos配置中心集群原理及源码分析
    查看>>
    nacos配置自动刷新源码解析
    查看>>
    Nacos集群搭建
    查看>>
    nacos集群搭建
    查看>>
    Navicat for MySQL 查看BLOB字段内容
    查看>>
    Neo4j电影关系图Cypher
    查看>>
    Neo4j的安装与使用
    查看>>
    Neo4j(2):环境搭建
    查看>>
    Neo私链
    查看>>
    nessus快速安装使用指南(非常详细)零基础入门到精通,收藏这一篇就够了
    查看>>
    Nessus漏洞扫描教程之配置Nessus
    查看>>
    Nest.js 6.0.0 正式版发布,基于 TypeScript 的 Node.js 框架
    查看>>
    NetApp凭借领先的混合云数据与服务把握数字化转型机遇
    查看>>