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

本文共 1506 字,大约阅读时间需要 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 HELLOtristate "hello module test"default mhelp    这是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.oCC [M] drivers/hello/hello.oCC drivers/hello/hello.mod.oLD [M] drivers/hello/hello.ko
      • 编译进内核
        :编译过程会输出以下内容:

        CC drivers/hello/hello.oLD 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/

    你可能感兴趣的文章
    Nvidia 系列显卡大解析 B100、A40、A100、A800、H100、H800、V100 该如何选择,各自的配置详细与架构详细介绍,分别运用于哪些项目场景
    查看>>
    NVIDIA-cuda-cudnn下载地址
    查看>>
    nvidia-htop 使用教程
    查看>>
    nvidia-smi 参数详解
    查看>>
    Nvidia驱动失效,采用官方的方法重装更快
    查看>>
    nvmw安装node-v4.0.0之后版本的临时解决办法
    查看>>
    nvm切换node版本
    查看>>
    nvm安装 出现 Error retrieving “http://xxxx/SHASUMS256.txt“: HTTP Status 404 解决方法
    查看>>
    nvm安装以后,node -v npm 等命令提示不是内部或外部命令 node多版本控制管理 node多版本随意切换
    查看>>
    NXLog采集windows日志配置conf文件
    查看>>
    ny540 奇怪的排序 简单题
    查看>>
    NYOJ -216 A problem is easy
    查看>>
    NYOJ 1066 CO-PRIME(数论)
    查看>>
    NYOJ 737:石子合并(一)(区间dp)
    查看>>
    nyoj 91 阶乘之和(贪心)
    查看>>
    nyoj------203三国志
    查看>>
    NYOJ-525 一道水题
    查看>>
    NYOJ127星际之门(一)
    查看>>
    nyoj58 最少步数
    查看>>
    N皇后问题
    查看>>