)linux驅動程式開發環境配置

來源:互聯網
上載者:User

剛看 O'REILLY 寫的《LINUX 裝置驅動程式》時。作者一再強調在編寫驅動程式時必須 建立核心樹。所謂核心樹,我的理解和網上資料說的一致就是核心源碼的一種邏輯形式。那怎麼建立呢?為此上網“翻雲覆雨”起來而結果卻是“慘敗而歸“。
為此託了一天又4個小時(當然包括吃飯睡覺的時間),連個簡單的 hello wrold 都沒實現。(書中p22頁最簡單也最沒用的驅動事列)
不過功夫不負有心人。在今天終於弄明白了怎麼回事。下面就請讓我慢慢道來吧。

先查看自己OS使用的核心版本

shana@shana:~$ uname -r
2.6.22-14-generic /* 這是我顯示的結果 */

如果安裝系統時,自動安裝了源碼。在 /usr/src 目錄下有對應的使用的版本目錄。例如下(我是自己下的)

shana@shana:/usr/src$ ls
linux-headers-2.6.22-14
linux-headers-2.6.22-14-generic
linux-source-2.6.22 /*這個就是解壓後的源碼目錄 */
linux-source-2.6.22.tar.bz2 /* 這是我下的源碼 包 */
shana@shana:/usr/src$

如果沒有源碼。(一般ubuntu 都沒有吧)
查看一下可一下載的源碼包(切記不要使用超級使用者使用此命令否則……會提示沒有此命令)

shana@shana:/usr/src$ apt-cache search linux-source
linux-source - Linux kernel source with Ubuntu patches
xen-source-2.6.16 - Linux kernel source for version 2.6.17 with Ubuntu patches
linux-source-2.6.22 - Linux kernel source for version 2.6.22 with Ubuntu patches
shana@shana:/usr/src$

我選擇了 linux-source-2.6.22 - Linux kernel source for version 2.6.22 with Ubuntu patches 這個~
然後 install 之

shana@shana:/usr/src$ sudo apt-get install linux-source-2.6.22

下載完成後,在/usr/src下,檔案名稱為:linux-source-2.6.22.tar.bz2,是一個壓縮包,解壓縮既可以得到整個核心的原始碼:

注意 已經切換到超級使用者模式

root@shana:/usr/src#tar jxvf linux-source-2.6.20.tar.bz2

解壓後產生一個新的目錄/usr/src/linux-source-2.6.22,所有的原始碼都在該目錄下。

進入該目錄

開始配置核心 選擇最快的原版的配置(預設)方式 (我是如此)

root@shana:/usr/src/linux-source-2.6.22 # make oldconfig

當然你也可以使用 自己喜歡的配置方式 如 menuconfig , xconfig(必須有GTK環境吧)。反正不用剪裁什麼,所以不管那種方式能配置它就行了。

完成後,開始make 吧 這兒比較久 一般有1一個小時吧。(保證空間足夠 我編譯完成後 使用了1.8G) 我分區時分給/目錄30G的空間,我沒遇到這問題。倒是我朋友遇到了。

shana@shana:/usr/src/linux-source-2.6.22$ make

shana@shana:/usr/src/linux-source-2.6.22$ make bzImage

當然,第一個make也可以不執行,直接make bzImage。執行結束後,可以看到在目前的目錄下產生了一個新的檔案: vmlinux, 其屬性為-rwxr-xr-x。

然後 :

root@shana:/usr/src/linux-source-2.6.22#make modules /* 編譯 模組 */

root@shana:/usr/src/linux-source-2.6.22#make modules_install /* 安裝 模組 */

執行結束之後,會在/lib/modules下產生新的目錄/lib/modules/2.6.22-14-generic/
。 在隨後的編譯模組檔案時,要用到這個路徑下的build目錄。至此,核心編譯完成。可以重啟一下系統。

至此 核心樹就建立啦 原來不是很難.....

寫一個 最簡單 最沒用的驅動吧
我在 /home/shana/linux_q/ 目錄下建立2個文字檔 hello.c Makefile

//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, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

程式我就不解釋了……

Makefile 檔案

obj-m := hello.o
KERNELDIR := /lib/modules/2.6.22-14-generic/build
PWD := $(shell pwd)

modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

如果以上你都完成了在 make 時出現這樣的錯誤

shana@shana:~/linux _驅動開發$ make
make: 沒有什麼可以做的為 `modules'。

原因很簡單 你肯定是從我這直接複製的吧~~~呵呵,Makefile格式錯誤啦~
解決辦法就是 你把如 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install 移動到行首 然後按Tab 鍵自動對齊
這時裡邊的變數都變成綠色了~然後在 make 吧

shana@shana:~/linux _驅動開發$ make
make -C /lib/modules/2.6.22-14-generic/build M=/home/shana/linux_驅動開發 modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.22-14-generic'
CC [M] /home/shana/linux_驅動開發/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/shana/linux_驅動開發/hello.mod.o
LD [M] /home/shana/linux_驅動開發/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.22-14-generic'
shana@shana:~/linux _驅動開發$

shana@shana:~/linux _驅動開發$ ls -l
總用量 124
-rw-r--r-- 1 shana shana 303 2008-03-16 10:43 hello.c
-rw-r--r-- 1 shana shana 49039 2008-03-16 12:11 hello.ko
-rw-r--r-- 1 shana shana 687 2008-03-16 12:11 hello.mod.c
-rw-r--r-- 1 shana shana 25840 2008-03-16 12:11 hello.mod.o
-rw-r--r-- 1 shana shana 24360 2008-03-16 12:11 hello.o
-rw-r--r-- 1 shana shana 8344 2008-03-16 09:17 linux_qudong_qu.txt
-rw-r--r-- 1 shana shana 266 2008-03-16 12:09 Makefile
-rw-r--r-- 1 shana shana 0 2008-03-16 12:11 Module.symvers
shana@shana:~/linux _驅動開發$

然後載入模組 (超級使用者)

root@shana:/home/shana/linux _ 驅動開發# insmod ./hello.ko

按照書上的例子 會在終端顯示 hello , world 但是運行後什麼都沒有出現 (原因不解)

root@shana:/home/shana/linux _ 驅動開發# insmod ./hello.ko
root@shana:/home/shana/linux _ 驅動開發#

查看載入模組

root@shana:/home/shana/linux _ 驅動開發# lsmod
Module Size Used by
hello 2560 0

已經載入上咯~~

刪除模組

root@shana:/home/shana/linux _ 驅動開發# rmmod hello
root@shana:/home/shana/linux _ 驅動開發#

那程式的輸出在那呢?書中說明 如果不出現在終端 則會寫進 syslog 檔案中

shana@shana:~/linux _驅動開發$ cat /var/log/syslog |grep world
Mar 16 12:14:53 shana kernel: [ 5937.529297] Hello, world
Mar 16 12:16:05 shana kernel: [ 6009.439036] Goodbye, cruel world
shana@shana:~/linux _驅動開發$

至此 全部工作都完成了。是否對你有用呢?

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/ruixj/archive/2010/06/12/5667551.aspx

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.