博客
关于我
在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/目录,对Makefile和Kconfig进行修改:

  • 修改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.o和hello.ko文件。同时,/linux-3.4/output/lib/modules/3.4.39/目录中也会包含生成的hello.ko文件。


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

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

    你可能感兴趣的文章
    Python——基础知识细节
    查看>>
    Python——正则表达式
    查看>>
    Python—面向对象2
    查看>>
    Python“和“带整数的运算符
    查看>>
    Python“文件存在“制作目录时出错
    查看>>
    Python“文件存在“制作目录时出错
    查看>>
    Python、IMAP 和 GMail.将消息标记为已查看
    查看>>
    python一个一个获取迭代器的元素
    查看>>
    python一维数组 提取_python – 如何从numpy中的1维数组中提取零维切片
    查看>>
    python三大库之--numpy(一)
    查看>>
    python三大库之--numpy(二)
    查看>>
    python上的字无效字符_标识中的无效字符
    查看>>
    python下用decode和encode 中文转码
    查看>>
    Python下调用Linux的Shell命令
    查看>>
    Python接口自动化测试实战系列
    查看>>
    python下载_安装_配置_以及第一行python程序---python工作笔记009
    查看>>
    Python不协调机器人消息.内容
    查看>>
    Python与C#之间内存共享互传信息
    查看>>
    python与c语言混合编程_你试过C语言和Python一起混合编程吗?两者相加不是已经无敌了!...
    查看>>
    python与excel
    查看>>