【Linux驱动篇】hello_world
最基本的驱动文件结构:
# 1、头文件
# 2、模块的进出口注册
# 3、开源证书声明(非必要,但是不写加载内核时就会提示)
# 4、驱动入口,出口函数
以下是简单Linux驱动例子:
1、编写驱动文件
helloworld.c
//包含头文件
#include <linux/init.h>
#include <linux/module.h>
//驱动入口函数
static int hello_init(void)
{
printk("hello world\n");
return 0;
}
//驱动出口函数
static void hello_exit(void)
{
printk("byb byb\n");
}
//模块的进出口注册
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");//开源证书声明
Makefile
obj-m += helloworld.o #先写生成的中间文件的名字是什么,-m 的意思是把我们的驱动编译成模块
KDIR:= Linux内核源码路径 #Linux内核源码
PWD?=$(shell pwd) #获取当前目录的变量
all:
make -C $(KDIR) M=$(PWD) modules #make 会进入内核源码的路径,然后把当前路径下的代码编译成 模块
2、编译驱动文件
# 在当前目录下直接编译
make
注意:
编译前记得配置环境
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-