linux中的编译驱动到内核方式

 更新时间:2025年09月08日 09:43:38   作者:Paper_Love  
这篇文章主要介绍了linux中的编译驱动到内核方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

编译驱动到内核

1 创建/kernel/drivers/helloworld文件夹

新建/kernel/drivers/hello文件夹
新建/kernel/drivers/hello/hello.c文件
新建/kernel/drivers/hello/Makefile文件
新建/kernel/drivers/hello/Kconfig文件

2 编写驱动程序文件

(/kernel/drivers/hello/hello.c)

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Hello World exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_DESCRIPTION("A Sample Hello World Module");
MODULE_ALIAS("A Sample module");

3 编写Makefile文件

(/kernel/drivers/hello/Makefile)

obj-$(CONFIG_HELLO) += hello.o

4 编写Kconfig文件

(/kernel/drivers/hello/Kconfig)

menu "ADD_SUB"

config HELLO
	tristate "helloworld module test"

endmenu

5 修改父目录Makefile文件

(/kernel/drivers/Makefile)

obj-$(CONFIG_HELLO) += hello/

6 修改父目录Kconfig文件

(/kernel/drivers/Kconfig)

source "drivers/hello/Kconfig"

注意:先执行make xxx_defconfig再执行make menuconfig

编译驱动到内核有两种方式

1 在make xxx_defconfig内添加

CONFIG_HELLO = y

2 在make menuconfig内使能

找到kconfig编写的helloworld module test选项

  • y表示编译文件到内核
  • m表示编译文件为模块,会在/kernel/drivers/hello目录下生成hello.ko文件。
  • n表示不编译文件到内核

配置完成以后Save保存到.config文件中,最后Exit退出。

​总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

最新评论