首页 > 学院 > 开发设计 > 正文

编写hello.ko文件

2019-11-09 17:14:22
字体:
来源:转载
供稿:网友

安装内核头文件

apt-get install linux-headers-`uname -r

编写hello.c文件

6 #include <linux/init.h> 7 #include <linux/module.h> 8 MODULE_LICENSE("Dual BSD/GPL"); 9 10 static int hello_init(void) 11 { 12 PRintk(KERN_ERR"Hello,world./n"); 13 return 0x0; 14 } 15 static void hello_exit(void) 16 { 17 printk(KERN_ERR"Hello,exit./n"); 18 return; 19 } 20 module_init(hello_init); 21 module_exit(hello_exit);

编写Makefile

25 obj-m += hello.o 26 CURRENT_PATH := $(shell pwd) 27 LINUX_KERNEL := $(shell uname -r) 28 LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL) 29 all: 30 make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) moduels 31 clean: 32 make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean

33 编译ko文件 35 make 36 37 加载内核文件 38 insmod hello.ko 39 40 查看内核文件是否加载成功 41 dmesg 42 43 查看内核模块信息 44 modinfo hello.ko 45 46 卸载内核模块 47 rmmod hello.ko 48 49


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表