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

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

这篇文章完全参考:原文:https://blog.csdn.net/u012247418/article/details/83684126  

原文讲解的特别详细,希望大家都去看原文

发这篇博客的原因完全只是为了方便自己以后查阅。

 

1. 构建测试模块:hello

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

cd linux-3.4/drivers/

mkdir hello

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

hello.c:

#include 
//所有模块都需要的头文件#include
// init&exit相关宏#include
MODULE_LICENSE("GPL");MODULE_AUTHOR("baoli");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:

obj-$(CONFIG_HELLO) += hello.o

Kconfig:

menu "HELLO TEST Driver "comment "HELLO TEST Driver Config" config HELLO	tristate "hello module test"	default m	help	This is the hello test driver  endmenu

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

进入linux-3.4/drivers/

1)编辑Makefile,在后面添加一行:

obj-$(CONFIG_HELLO) += hello/

2)编辑Kconfig,在后面添加一行:

source "drivers/hello/Kconfig"

注:某些内核版本需要同时在arch/arm/Kconfig中添加:source "drivers/hello/Kconfig"

 

2. make menuconfig配置

1)执行:make menuconfig ARCH=arm

2)选择并进入:Device Drivers选项

可以看到新增 HELLO TEST Driver选项

3)进入 HELLO TEST Driver选项

可以选择<m> <y> <n>,分别为编译成内核模块、编译进内核、不编译。

 

3. 编译

退出保存后

开始编译内核

1)如果选择编译成模块<m>

编译内核过程中,会有如下输出:

LD drivers/hello/built-in.o

CC [M] drivers/hello/hello.o

CC drivers/hello/hello.mod.o

LD [M] drivers/hello/hello.ko

2)如果选择编译进内核<y>

编译内核过程中,会有如下输出:

CC drivers/hello/hello.o

LD drivers/hello/built-in.o

 

编译完成后,drivers/hello/下新增hello.o和hello.ko,并且/linux-3.4/output/lib/modules/3.4.39/下也会有hello.ko。

 

你可能感兴趣的文章
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
mongodb定时备份数据库
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue的使用(一)必要的开发环境
查看>>
MQ 重复消费如何解决?
查看>>
mqtt broker服务端
查看>>
MQTT 保留消息
查看>>
MQTT 持久会话与 Clean Session 详解
查看>>
MQTT工作笔记0007---剩余长度
查看>>
MQTT工作笔记0009---订阅主题和订阅确认
查看>>
Mqtt搭建代理服务器进行通信-浅析
查看>>
MS Edge浏览器“STATUS_INVALID_IMAGE_HASH“兼容性问题
查看>>
ms sql server 2008 sp2更新异常
查看>>
MS UC 2013-0-Prepare Tool
查看>>
MSBuild 教程(2)
查看>>
msbuild发布web应用程序
查看>>