A tutorial to the linux driver
the code
The *hello world" example
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT"Hello, world\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT"Goodbye, cruel world\n");
return 0;
}
module_init(hello_init);
module_exit(hello_exit);
the makefile
ifneq (($KERNELRELEASE),)
obj-m := hello.o
else
endif
HEADDIR := /lib/modules/4.19.97-v71+/build/
all:
make -C $(HEADDIR) M=$(PWD) modules
clean:
make -C $(HEADDIR) M=$(PWD) clean
put Makefile in the same folder with hello.c, then make
results
if compiled successfully, you will get the file hello.ko. In the next step,
insert the module
insmod hello.ko
dmesg | tail
here you will see “Hello, world”.
remove the module
rmmod hello
dmesg | tail
you will find “Goodbye, cruel world”.
本文介绍了一个简单的Linux驱动示例,通过实现一个打印'Hello, world!'和'Goodbye, cruel world!'的模块来演示如何创建、编译及加载卸载Linux内核模块。文章包含了必要的源代码和Makefile配置。
5196

被折叠的 条评论
为什么被折叠?



