4.【linux驱动】hello_world

hello_world源码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");

static int __init hello_init(void){
	printk("hello init\n");
	return 0;
}

static void __exit hello_exit(void){
	printk("hello exit\n");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile

Makefile采用Kconifg机制,也就是调用内核的Makefile实现编译,只需要使用如下格式即可完成调用

make -C 调用Makefile的路径 M=当前路径 操作指令

完整的代码如下:

KERNEL_DIR := /home/minicoco/Dev/Dev/nanopi/kernel/linux-3.4.y

hello:
	make -C ${KERNEL_DIR} M=`pwd` modules

.PHONY:
clean:
	make -C ${KERNEL_DIR} M=`pwd` clean

obj-m += hello_world.o

用obj-m定义自己需要编译的模块,编译完成会输出到当前目录

加载卸载测试

insmod hello_world.ko
rmmod hello_world

猜你喜欢

转载自blog.csdn.net/qq_16054639/article/details/106555506
今日推荐