Johannes 《Linux内核模块与设备驱动开发:编写Linux驱动程序》(1)hello world
·
学习linux 内核开发做学习笔记。
作者 :Johannes
开发平台:Raspberry Pi 3 数莓派3
程序源码
不考虑交叉编译以及环境配置,只学习了解视频。需要了解下makefile 以及linux 常用命令
lessson1 hello_world
/*
* a crucial part of the Linux kernel, providing the necessary infrastructure for
* dynamically loading and unloading kernel modules.
*/
#include <linux/module.h>
/* module init functions.modlue_init()/module_exit() are * used to define the
* initialization and cleanup functions of a module.
*/
#include <linux/init.h>
int my_init(void)
{
printk("hello - Hello, Kernel!\n");
return 0;
}
void my_exit(void)
{
printk("hello - Goodbye, Kernel!\n");
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
module.h 提供内核动态加载移除,init.h 提供内核函数如何加载移除。这两个头文件是内核开发必须的。
程序最后是内核开发的MODULE_LICENSE(“GPL”).内核的许可证。
这个程序十分简单的一个内核模块,有些地方不标准,但是够用。
最后make
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
makefile 简单说明:
obj-m += hello.o 表明会生成hello.ko
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
make all .实际的生成命令, 这里 shell uname -r 表明系统的内核版本,/lib/modules/$(shell uname -r)/build 等于表达内核目录PWD 表示当前目录路径
所以这个命令等于 make -C /lib/modules/xxx/build M=xxx(当前目录) modules
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐


所有评论(0)