Linux设备驱动程序学习(一)入门必须的hello world

来源:互联网 发布:人工智能芯片上市公司 编辑:程序博客网 时间:2024/06/05 23:55

还是从最简单的“hello world”开始,源代码文件hello_world.c:

#include <linux/init.h>#include <linux/module.h>static int hello_world_init(void){printk(KERN_ALERT "hello, world!\n");return 0;}static void hello_world_exit(void){printk(KERN_ALERT "goodbye, cruel world!\n");}module_init(hello_world_init);module_exit(hello_world_exit);MODULE_LICENSE("Dual BSD/GPL");MODULE_AUTHOR("Keith Liu");


编译代码的Makefile文件内容如下:

ifneq ($(KERNELRELEASE),)obj-m := hello_world.oelseKERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesecho $(KERNELRELEASE)echo $(KERNELDIR)echo $(PWD)endif
其中的echo语句是我为了方便查看变量的内容而添加的,可以取消掉。

需要注意的一点是:

ifneq与

($(KERNELRELEASE),)
之间必须要有空格分割,否则编译时会报错误。

原创粉丝点击