#include <linux/init.h> #include <linux/module.h> int hello_init(void){ printk("Hello init"); return 0; } void hello_exit(void){ printk("Hello exit"); } module_init(hello_init) module_exit(hello_exit)
/usr/src/linux-headers-5.4.0-58-generic/include/linux/init.h
ifneq ($(KERNELRELEASE),) obj-m := hello.o else KERN_DIR ?= /usr/src/linux-headers-$(shell uname -r)/ PWD := $(shell pwd) default: $(MAKE) -C $(KERN_DIR) M=$(PWD) modules endif clean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
结果.├── hello.c├── hello.ko├── .hello.ko.cmd├── hello.mod├── hello.mod.c├── .hello.mod.cmd├── hello.mod.o├── .hello.mod.o.cmd├── hello.o├── .hello.o.cmd├── Makefile├── modules.order└── Module.symvers
watch "dmesg | tail -20" dmesg -wH