Linux內核編程 -- Hello Mod

1. hello mod

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

 static int __init hello_start(void){
     printk("hello,this moudle start work!\n");
     return 0;
 }

 static void __exit hello_exit(void){
     printk("hello,this moule exit!\n");
 }

 module_init(hello_start);
 module_exit(hello_exit);

2.Makefile

 obj-m=hello_ker.o
 CURRENT_PATH:=$(shell pwd)
 LINUX_KERNEL:=$(shell uname -r)
 LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)

 all:
     make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
 clean:
     mkae -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean

一些問題

1) 在Makefile中,其中LINUX_PATH :=$(shell uname -r)很重要,內核的程序在編譯時,需要的頭文件當然得符合當前系統的版本。當然也可以指定編譯的版本,但前提是自己得有相應的linux源代碼或者頭文件。
2) 關於C語言的語法問題。由於自身C的編程習慣,在傳參時如果函數參數沒有參數,一般會以函數()結束

static int __exit printkhello()

但是在 make編譯的過程中,會出現以下問題:

error: function declaration isn’t a prototype [-Werror=strict-prototypes]

意思大意就是函數原型不匹配的錯誤。

後來解決方法是在func()括號中添加了func(void)結束,在linux中的內核編程中,以後用void表示無參就可以了。

3) 接下來這個問題就比較坑爹了。由於英語水平不高,以至於module單詞接連拼錯,出現了很多的error。後來和朋友交流,起初他們也常犯這些低級錯誤。

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