https://blog.youkuaiyun.com/tigerjb/article/details/6010997#1607120添加链接描述
编写一个最简单的文件hello.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "hello,i am edsionte\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "goodbye,kernel\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("TZ");
MODULE_DESCRIPTION("this is example\n");
MODULE_ALIAS("A simplest example");
编写一个makefile文件
//注意all下面一行 make之前一定是一个tab键
obj-m:=hello.o
kernel_path:=/usr/src/linux-headers-$(shell uname -r)
all:
make -C $(kernel_path) M=$(PWD) modules
clean:
make -C $(kernel_path) M=$(PWD) clean
【1】然后make编译链接 注意这里有时候可能会报错,如若报错就执行
make CONFIG_STACK_VALIDATION=
便可以编译成功
【2】insmod hello.ko(加载模块)
【3】dmesg查看结果
【4】make clean 清楚中间生成的文件