今天写了一个非常简单的字符驱动模块,源程序问题不大,makefile脚本不知道哪出错了。试了一下午,竟然是少了个"$"来引用变量。特此记录下!
1.如下为源程序:
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/kernel.h> 4 MODULE_LICENSE("GPL");//许可权限的声明 5 static int __init hello_init(void) 6 { 7 printk(KERN_DEBUG"hello world\n");//定义输出级别 8 return 0; 9 } 10 static void __exit hello_exit(void) 11 { 12 printk(KERN_DEBUG"byebye\n"); 13 } 14 module_init(hello_init);//内核模块加载函数 15 module_exit(hello_exit);//内核模块卸载函数
2.如下为makefile脚本:
1 #!/bin/bash 2 ifneq ($(KERNELRELEASE),) 3 obj-m := hello.o 4 else 5 PWD :=$(shell pwd) 6 KDIR := /lib/modules/$(shell uname -r)/build 7 all: 8 make -C $(KDIR) M=$(PWD) 9 clean: 10 rm -rf *.o *.ko *.mod.c *.symvers *.c- *- 11 endif
脚本最好在第一行用如下语句规定初始脚本:
#!/bin/bash
要不然有可能发生如下报错:
/bin/sh: -c: line 0 syntax error near unexpected token ‘(‘
我把第五行的 $ 符号漏掉了。其余都是常规写法。
3.make一下。生成如下相关文件:
4.加载模块,查看模块,卸载模块。
5.通过dmesg命令查看内核输出信息。
1)装载模块
2)卸载模块
原文:https://www.cnblogs.com/kunshanpipixia/p/13908109.html