helloworld模块

hello.c:

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world ");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world ");
}
module_init(hello_init);
module_exit(hello_exit);

Makefile:

KVERS = $(shell uname -r)

# Kernel modules
obj-m += hello.o

# Specify flags for the module compilation.
#EXTRA_CFLAGS=-g -O0

build: kernel_modules

kernel_modules:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules

clean:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean

加载模块:

insmod ./hello.ko

卸载模块:

rmmod hello

查看打印信息:

dmesg | tail

原文地址:https://www.cnblogs.com/poonpan/p/8243729.html