LINUX驅動版本的hello world

來源:互聯網
上載者:User
轉載自 jjzhang166最終編輯 jjzhang166

http://hi.baidu.com/ah%5F%5Ffu/blog/item/57b76201e27095de267fb5d4.html

終於下決心好好學習LINUX核心和驅動開發了,不怕千萬人恥笑,勇敢將自己的學習過程寫出來:

1、關於目錄
    /lib/modules/2.6.9-42.ELsmp/build/   這個是核心源碼所在的目錄
    一般使用這樣的命令進入這個目錄:cd /lib/modules/$(uname -r)/build/
   這個目錄實際上指向了:/usr/src/kernels/2.6.9-42.EL-smp-i686

2、編譯驅動所使用的makefile
    實際上編譯驅動的時候是使用預先提供的一個makefile的,位置在:
/lib/modules/$(uname -r)/build/Makefile
    注意:M是大寫的

3、網上抄錄的Linux驅動Hello world的源碼:
// hello.c
#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
    printk(KERN_ALERT "hello world!\n");
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "goodbye!\n");
}

module_init(hello_init);
module_exit(hello_exit);

4、寫個makefile來編譯這個驅動:(版本一,最簡單的)
#下面這行是檔案Makefile的內容,注意M是大寫的
obj-m := hello.o

把hello.c和Makefile儲存在同一目錄,然後執行:
make -C /lib/modules/`uname -r`/build SUBDIRS=$PWD modules
這樣驅動就編譯好了,成果是hello.ko檔案。
注意:makefile一定要寫成Makefile,如果寫成makefile就編譯不過。(折騰啊,就這一步耗了N多時間)

5、再寫另一種Makefile:(版本二:最省事的)
#以下是Makefile檔案的內容
obj-m := hello.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
    make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
    rm *.o *.ko

然後執行:make就編譯成功了,命令列不再加參數,很省事。
注意:all: 和clean:下面的行,前面是一個TAB鍵

6、載入驅動:
執行
insmod ./hello.ko
螢幕上沒反應。(因為我是在WINDOWS上用遠程終端連上去的嘛)
OK,先讓時光倒流,回到載入驅動以前,先另開一個視窗,執行:
tail -f /var/log/message
然後在原來的視窗裡執行:
insmod ./hello.ko
哈哈,/var/log/message檔案裡面看見了盼望已久的hello world!

7、查看驅動:
lsmod   看見 hello這個驅動在其中

8、卸載驅動:
rmmod hello 
看見/var/log/message裡顯示了goodbye

 

附錄為通過編譯的helloworld程式

hello.c

01 #include <linux/init.h>
02  
03 #include <linux/module.h>
04  
05  
06  
07 static int hello_init(void)
08  
09 {
10  
11     printk(KERN_INFO " Hello World enter\n");
12  
13     return 0;
14  
15 }
16  
17  
18  
19 static void hello_exit(void)
20  
21 {
22  
23     printk(KERN_INFO " Hello World exit\n ");
24  
25 }
26  
27  
28  
29 module_init(hello_init);
30  
31 module_exit(hello_exit);
32  
33  
34  
35 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
36  
37 MODULE_LICENSE("Dual BSD/GPL");
38  
39 MODULE_DESCRIPTION("A simple Hello World Module");
40  
41 MODULE_ALIAS("a simplest module");

Makefile

view sourceprint?
01 KVERS = $(shell uname -r)
02  
03 # Kernel modules
04 obj-m += hello.o
05  
06 # Specify flags for the module compilation.
07 #EXTRA_CFLAGS=-g -O0
08  
09 build: kernel_modules
10  
11 kernel_modules:
12     make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules
13  
14 clean:
15     make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.