Fedora下编译Hello模块驱动

来源:互联网 发布:平安科技 云计算 编辑:程序博客网 时间:2024/05/22 17:18

1.构建内核树

Fedora不需要编译出内核树,只需要下载就可以,终端输入命令 yum install kernel-devel 安装完成后,/usr/src/kernels/$(KVER)便是我们的内核树路径

 

2.建立Makefile文件

ifneq ($(KERNELRELEASE),)         obj-m:= hello.oelse         PWD:=$(shell pwd)         KVER?=$(shell uname -r)         KERNELDIR:= /usr/src/kernels/$(KVER)default:         $(MAKE)-C $(KERNELDIR) M=$(PWD) modulesendif clean:         rm-f *.ko *.mod.c *.mod.o *.o 

3.建立hello模块驱动.C文件

Hello.c#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void){         printk("hello,world\n");         return0;} static void hello_exit(void){         printk("GoodBye,world\n");} module_init(hello_init);module_exit(hello_exit); 


4.测试驱动

执行make,编译生成hello.ko文件

加载驱动,insmod hello.ko

卸载驱动,rmmod hello.ko

终端输入查看 cat /var/log/messages |tail

可以看到以下文字

Mar 4 16:16:15 localhost kernel: hello,world

Mar 4 16:16:22 localhost kernel: GoodBye,world

原创粉丝点击