博客
关于我
在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/

    你可能感兴趣的文章
    opencart出现no such file or dictionary
    查看>>
    OpenCV 3.1 imwrite()函数写入异常问题解决方法
    查看>>
    OpenCV 4.1.0版drawContours
    查看>>
    opencv glob 内存溢出异常
    查看>>
    opencv Hog Demo
    查看>>
    opencv Hog学习总结
    查看>>
    opencv Mat push_back
    查看>>
    opencv putText中文乱码
    查看>>
    OpenCV Python围绕特定点将图像旋转X度
    查看>>
    opencv resize
    查看>>
    opencv SVM分类Demo
    查看>>
    OpenCV VideoCapture.get()参数详解
    查看>>
    opencv videocapture读取视频cap.isOpened 输出总是false
    查看>>
    opencv waitKey() 函数理解及应用
    查看>>
    OpenCV 中的图像转换
    查看>>
    OpenCV 人脸识别 C++实例代码
    查看>>
    OpenCV 在 Linux 上的 python 与 anaconda 无法正常工作.收到未实现 cv2.imshow() 的错误
    查看>>
    Opencv 完美配置攻略 2014 (Win8.1 + Opencv 2.4.8 + VS 2013)上
    查看>>
    opencv 模板匹配, 已解决模板过大程序不工作的bug
    查看>>
    OpenCV 错误:(-215)size.width>0 &&函数imshow中的size.height>0
    查看>>