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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章