1. 程式人生 > >Linux裝置驅動開發環境搭建

Linux裝置驅動開發環境搭建

1、linux原始碼的版本號與執行中的linux核心版本要一致(這裡用的是Ubuntu 12.04 LTS; Kernel 為3.11.0)

2、安裝開發工具(都安裝了方便後面使用,有些沒必要安裝)

->sudo apt-get install build-essential autoconf automake1.9 cvs subversion kernel-package libncurses5-dev
->sudo apt-get install bison flex
重點安裝的是build-essential和libncurses5-dev

3、檢視核心版本並下載相應的版本原始碼:

切換到root:su
檢視核心版本:uname -r
顯示:3.11.0-15-generic
去下載對應版本的原始碼到/usr/src目錄下,這裡下載到的是linux-3.11.tar.xz

4、解壓並且配置原碼

xz -d linux-3.11.tar.xz
得到 linux-3.11.tar
tar xf linux-3.11.tar
得到 linux-3.11
改名: mv linux-3.11 linux-source-3.11.0
進入:cd linux-source-3.11.0
相關配置:

make oldconfig
make prepare
make scripts

至此,已完成。
接下來可以編寫hello.c以及Makefile來做測試了。

載入模組:insmod hello.ko 或 modprobe hello
解除安裝模組:rmmod hello

檢視列印資訊:

cat /proc/kmsg 會一直列印,需要Ctrl-C手動終止
dmesg 或 dmesg | tail -N ,N為一數字,表示顯示最後N行

若在make時出現

WARNING: Symbol version dump /usr/src/linux-source-3.11.0/Module.symvers is missing; modules will have no dependencies and modversions.

且insmod時出現

insmod: error inserting ‘./hello.ko’: -1 Invalid module format

同時,在 dmesg 中出現

hello: no symbol version for module_layout

請參考: