博客
关于我
4.【linux驱动】hello_world
阅读量:680 次
发布时间:2019-03-17

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

hello_world模块开发指南

模块开发

我们需要将一个简单的内核模块开发出来,模块名称为hello_world。以下是实现步骤:

  • 创建与模块相关的C文件
  • 包含必要的头文件
  • 定义模块的初始化和卸载函数
  • 使用 printk输出日志信息
  • 代码示例如下:

    #include 
    #include
    #include
    MODULE_LICENSE("GPL");static int __init hello_init(void) { printk("hello init\n"); return 0;}static void __exit hello_exit(void) { printk("hello exit\n");}module_init(hello_init);module_exit(hello_exit);

    Makefile编译

    这部分将介绍如何使用Makefile来编译内核模块。我们将使用Kconfig机制来进行配置。

  • 打开终端,进入内核源码目录
  • 执行编译命令:
  • make -C /home/minicoco/Dev/Dev/nanopi/kernel/linux-3.4.y M=`pwd`
    1. Makefile中添加以下配置:
    2. hello: make -C ${KERNEL_DIR} M=`pwd` hello_world.oclean: make -C ${KERNEL_DIR} M=`pwd` cleanobj-m += hello_world.o

      这样模块将会在当前目录输出。

      加载与卸载测试

      在完成编译后,我们需要测试模块的加载与卸载过程。

    3. 使用以下命令加载模块:
    4. insmod hello_world.ko
      1. 模块加载成功后,查看日志以确认是否成功加载:
      2. dmesg | tail
        1. 卸载模块使用以下命令:
        2. rmmod hello_world

          在卸载后,再次查看日志以确认模块是否成功卸载。

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

    你可能感兴趣的文章
    Oracle:ORA-00911: 无效字符
    查看>>
    Text-to-Image with Diffusion models的巅峰之作:深入解读 DALL·E 2
    查看>>
    Tensorflow.python.framework.errors_impl.ResourceExhaustedError:无法分配内存[操作:AddV2]
    查看>>
    TCP基本入门-简单认识一下什么是TCP
    查看>>
    tableviewcell 中使用autolayout自适应高度
    查看>>
    Symbolic Aggregate approXimation(SAX,符号聚合近似)介绍-ChatGPT4o作答
    查看>>
    Orcale表被锁
    查看>>
    svn访问报错500
    查看>>
    sum(a.YYSR) over (partition by a.hy_dm) 不需要像group by那样需要分组函数。方便。
    查看>>
    ORCHARD 是什么?
    查看>>
    Struts2中使用Session的两种方法
    查看>>
    Stream API:filter、map和flatMap 的用法
    查看>>
    STM32工作笔记0032---编写跑马灯实验---寄存器版本
    查看>>
    ssm旅游信息管理系统的设计与实现bus56(程序+开题)
    查看>>
    order by rand()
    查看>>
    SSM(Spring+SpringMvc+Mybatis)整合开发笔记
    查看>>
    ViewHolder的改进写法
    查看>>
    Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
    查看>>
    org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
    查看>>
    sql查询中 查询字段数据类型 int 与 String 出现问题
    查看>>