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”.