源文件:
#include<linux/init.h>
#include<linux/module.h>MODULE_LICENSE("Dual BSD/GPL");
static int __init hello_init(void)
{
printk(KERN_ALERT"Hello world enter!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_ALERT"Hello world exit!\n");
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("ZHANG DELAI");
MODULE_DESCRIPTION("A simple hello world Module");
MODULE_ALIAS("a simplest module");
MODULE_VERSION("1.0");
Makefile:
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*
.PHONY: modules modules_install clean
else
obj-m := hello.o
endif
在终端执行:1.make
2.insmod hello.ko
3.rmmod hello.ko
4.dmesg可以看到加载和卸载模块时的打印信息。