hello.c:
/* hello.c */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world/n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, hello/n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
Makefile:
#makefile for hello.c file
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
obj-m:=hello.o
all :
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD)
.PHONY : clean
clean :
rm -rf .*.cmd *.mod.c *.o *.ko .tmp*
编译:
make
测试:
加载模块
insmod hello.ko
删除模块
rmmod hello