1:簡單代碼linux
#include<linux/init.h> #include<linux/module.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("jiuyueguang"); MODULE_DESCRIPTION("SIMPLE MODULE DRIVER"); static int hello_init(void){ printk(KERN_INFO"hello word\n"); return 0; } static void hello_exit(void){ printk(KERN_INFO"hello word exit\n"); } module_init(hello_init); module_exit(hello_exit);
2:查看printk輸出信息shell
dmesg | tail
3:帶參數類型的模塊簡單代碼ui
#include<linux/init.h> #include<linux/module.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("jiuyueguang"); MODULE_DESCRIPTION("SIMPLE MODULE DRIVER"); static char *name="no name"; static int age=0; static int hello_init(void){ printk(KERN_INFO"hello word name=%s and age=%d\n",name ,age); return 0; } static void hello_exit(void){ printk(KERN_INFO"hello word exit name=%s and age=%d \n",name ,age); } module_init(hello_init); module_exit(hello_exit); module_param(name,charp,S_IRUGO); module_param(age,int,S_IRUGO);
安裝帶參數的模塊:spa
sudo insmod hello_param.ko
安裝的時候沒有加參數,採用程序中的默認值輸出:code
hello word name=no name and age=0
安裝的時候加參數:blog
sudo insmod hello_param.ko name='jiuyueguang' age=25
輸出:cmd
hello word name=jiuyueguang and age=25
4:我本身的Makefileit
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* module* a.out .PHONY: modules modules_install clean else obj-m := hello_param.o //每次修改這裏就夠了 endif