寫一個簡單的hello字元驅動模組
阿新 • • 發佈:2020-11-01
今天寫了一個非常簡單的字元驅動模組,源程式問題不大,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)解除安裝模組