Nano之HelloWorld驱动 (学习如何单独编译.ko模块)

转大佬

https://blog.csdn.net/u012577474/article/details/102913573

 

1、准备下面两个文件:

1 Makefile 
2 nano_hello_module.c

 

nano_hello_module.c

 1 #include <linux/kernel.h>
 2 #include <linux/module.h>
 3 
 4 static int __init nano_hello_module_init(void)
 5 {
 6     printk("Hello yfw, Nano module is installed !\n");
 7     return 0;
 8 }
 9 
10 static void __exit nano_hello_module_cleanup(void)
11 {
12     printk("Good-bye yfw, Nano module was removed!\n");
13 }
14 
15 module_init(nano_hello_module_init);
16 module_exit(nano_hello_module_cleanup);
17 
18 MODULE_LICENSE("GPL");

 

Makefile

 

1 把下面3个参数改成你自己的:

2 obj-m (.o文件)

3 KERNELDIR (内核目录全路径)

4 CROSS_ARCH (架构及其编译器) 

 

 1 #set KERNELDIR and CROSS_COMPILE path yourself
 2 #
 3 ifneq   ($(KERNELRELEASE),)
 4 obj-m   :=   nano_hello_module.o
 5 else
 6  KERNELDIR   :=   /home/yang/yfw_nano/linux5.2/linux-nano-5.2-tf
 7  PWD   :=   $(shell pwd)
 8  CROSS_ARCH := ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
 9 default: 
10         $(MAKE) $(CROSS_ARCH) -C  $(KERNELDIR)   M=$(PWD)   modules 
11 
12 install: 
13         $(MAKE)  $(CROSS_ARCH) -C  $(KERNELDIR)   M=$(PWD)   modules_install 
14 clean: 
15         rm   -rf   *.o   *~   core   .depend   .*.cmd   *.ko   *.mod.c   .tmp_versions *.symvers *.d *.markers *.order
16 
17 .PHONY:   modules   modules_install   clean  main
18 endif

 

2、编译,运行

1 make

把nano_hello_module.ko 拷贝到目标板运行(运行结果如下)

1 # ls
2 nano_hello_module.ko  
3 # insmod nano_hello_module.ko 
4 [ 8329.180768] nano_hello_module: loading out-of-tree module taints kernel.
5 [ 8329.200683] Hello yfw, Nano module is installed !
6 # rmmod nano_hello_module
7 [ 8358.476347] Good-bye yfw, Nano module was removed!
posted @ 2020-01-14 14:57  XZHDJH  阅读(438)  评论(0)    收藏  举报