Linux驱动开发最简Sample

0.安装相应软件包(笔者使用VMWare + Ubuntu18.04.1,amd64)、内核头文件等,忽略。

1.编写驱动程序代码(hello.c):

#include <linux/init.h> 
#include <linux/module.h> 
#include <linux/kernel.h> 
 
MODULE_LICENSE("Dual BSD/GPL"); 
  
static int hello_init(void)  { 
    printk(KERN_ALERT "hello,I am areful
"); 
    return 0; 
}

static void hello_exit(void)  { 
    printk(KERN_ALERT "goodbye,kernel
"); 
} 
 
module_init(hello_init); 
module_exit(hello_exit); 
 
MODULE_AUTHOR("Areful"); 
MODULE_DESCRIPTION("This is a simple example!
"); 
MODULE_ALIAS("A simplest example");

  

2.编写Makefile文件:

obj-m := hello.o
KERNELBUILD :=/lib/modules/$(shell uname -r)/build
all: 
	make -C $(KERNELBUILD) M=$(shell pwd) modules
clean: rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers .tmp_versions

3.编译(命令行运行make):

make

  

4.安装驱动模块:

sudo insmod hello.ko

  

5.卸载驱动模块:

sudo rmmod hello.ko

  

6.查看系统日志:

dmesg | tail -5

  

输出如下:

原文地址:https://www.cnblogs.com/areful/p/10381600.html