linux編譯module
編譯模塊的make file 必須是Makefile,不能是makefile. //why?
ifneq ($(KERNELRELEASE),)
obj-m := your.o
mytest-objs := file1.o file2.o file3.o
else
KDIR := /lib/moles/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) moles
endif
把your換成你的source name ,然後保存為Mafefile ,make 一次就可以了。
㈡ linux內核模塊,怎麼編譯
我來說下吧 本身你這個問題問的有點歧義 不知道你問的是內核編譯 還是模塊編譯 兩個不是一個東西 盡管模塊載入後 也是內核的一部分 看看其他的回答 以為是單純的內核的編譯了 模塊本身在linux下面是可以分為靜態和動態載入的 要是採用靜態載入的話 就是從新編譯內核 和內核的編譯基本是一回事 但是多採用動態載入 這個也簡單點
從你的下面的模版可以看出 你是想寫驅動程序吧 驅動一般作為動態載入的就可以了 寫好你的c文件 格式和上面的差不多 然後GCC編譯 生成.o文件,不要生成可執行文件 ( 如果是玩Embedded 就下載到目標板了 minicom 的使用) 如果是就在linux機器上 直接執行 insmod lsmod rmmod 這些就好了 這里也是簡單的說下了 內核的編譯 寫驅動程序 本身就是個比較難得事情了 要個很長的時間去學習了 慢慢積累 好運
㈢ 如何編譯linux驅動模塊
第一步:准備源代碼
首先我們還是要來編寫一個符合linux格式的模塊文件,這樣我們才能開始我們的模塊編譯。假設我們有一個源文件mymod.c。它的源碼如下:
mymoles.c
1. #include <linux/mole.h> /* 引入與模塊相關的宏 */
2. #include <linux/init.h> /* 引入mole_init() mole_exit()函數 */
3. #include <linux/moleparam.h> /* 引入mole_param() */
4
5. MODULE_AUTHOR("Yu Qiang");
6. MODULE_LICENSE("GPL");
7
8. static int nbr = 10;
9. mole_param(nbr, int, S_IRUGO);
10.
11. static int __init yuer_init(void)
12.{
13. int i;
14. for(i=0; i<nbr; i++)
15. {
16. printk(KERN_ALERT "Hello, How are you. %d/n", i);
17. }
18. return 0;
19.}
20.
21.static void __exit yuer_exit(void)
22.{
23. printk(KERN_ALERT"I come from yuer's mole, I have been unlad./n");
24.}
25.
26. mole_init(yuer_init);
27. mole_exit(yuer_exit);
我們的源文件就准備的差不多了,這就是一個linux下的模塊的基本結構。第9行是導出我們的符號變數nbr。這樣在你載入這個模塊的時候可以動態修改這個變數的值。稍後將演示。yuer_init()函數將在模塊載入的時候運行,通過輸出的結果可以看到我們的模塊是否載入成功。
第二步:編寫Makefile文件
首先還是來看看我們Makefile的源文件,然後我們再來解釋;
Makefile
obj-m := moles.o #要生成的模塊名
moles-objs:= mymod.o #生成這個模塊名所需要的目標文件
KDIR := /lib/moles/`uname -r`/build
PWD := $(shell pwd)
default:
make -C $(KDIR) M=$(PWD) moles
clean:
rm -rf *.o .* .cmd *.ko *.mod.c .tmp_versions
ARM平台
Makefile
obj-m += mymod.o
KDIR := /home/workspace2/kernel/linux-2.6.25 #如果是用於arm平台,則內核路徑為arm內核的路徑
PWD = $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) moles
clean:
rm -rf *.o
在arm板上插入是
insmod mymod
如果出現以下錯誤
insmod: chdir(/lib/moles): No such file or directory
則運行
mkdir /lib/moles/2.6.25 (與arm內核版本相同)
並將mymod.ko文件復制到該目錄下
cp mymod.ko /lib/moles/2.6.25
然後再執行 (insmod 只在/lib/moles/2.6.25目錄下查找相關驅動模塊)
insmod mymod
現在我來說明一下這個Makefile。請記住是大寫的Makefile而不是小寫的makefile;
obj-m :這個變數是指定你要聲稱哪些模塊模塊的格式為 obj-m := <模塊名>.o
moles-objs :這個變數是說明聲稱模塊moles需要的目標文件 格式要求 <模塊名>-objs := <目標文件>
切記:模塊的名字不能取與目標文件相同的名字。如在這里模塊名不能取成 mymod;
KDIR :這是我們正在運行的操作系統內核編譯目錄。也就是編譯模塊需要的環境
M= :指定我們源文件的位置
PWD :這是當前工作路徑$(shell )是make的一個內置函數。用來執行shell命令。
第三步:編譯模塊
現在我們已經准備好了我們所需要的源文件和相應的Makefile。我們現在就可以編譯了。在終端進入源文件目錄輸入make
運行結果:
make[1]: Entering directory `/usr/src/linux-headers-2.6.24-24-generic'
CC [M] /home/yuqiang/桌面/mymole/mymoles.o
LD [M] /home/yuqiang/桌面/mymole/moles.o
Building moles, stage 2.
MODPOST 1 moles
CC /home/yuqiang/桌面/mymole/moles.mod.o
LD [M] /home/yuqiang/桌面/mymole/moles.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.24-24-generic'
第四步:載入/卸載我們的模塊
從上面的編譯中我可以看到。已經有一個moles.ko生成了。這就是我們的模塊了。現在我們就可以來載入了。
首先在終端輸入:sudo insmod moles.ko
現在我們來看看我們的模塊載入成功沒有呢?
在終端輸入:dmesg | tail -12 這是查看內核輸出信息的意思。tail -12 顯示最後12條;
顯示結果如下:
[17945.024417] sd 9:0:0:0: Attached scsi generic sg2 type 0
[18046.790019] usb 5-8: USB disconnect, address 9
[19934.224812] Hello, How are you. 0
[19934.224817] Hello, How are you. 1
[19934.224818] Hello, How are you. 2
[19934.224820] Hello, How are you. 3
[19934.224821] Hello, How are you. 4
[19934.224822] Hello, How are you. 5
[19934.224824] Hello, How are you. 6
[19934.224825] Hello, How are you. 7
[19934.224826] Hello, How are you. 8
[19934.224828] Hello, How are you. 9
看到了吧。我們的模塊的初始化函數yuer_init();已經成功運行了。說明我們的模塊已經載入成功;
現在我們再來卸載模塊試試看。
在終端輸入:sudo rmmod moles
在終端輸入:dmesg | tail -3
[19934.224826] Hello, How are you. 8
[19934.224828] Hello, How are you. 9
[20412.046932] I come from yuer's mole, I have been unlad.
可以從列印的信息中看到,我們的模塊的退出函數已經被執行了。說明我們的模塊已經被成功的卸載了。到目前位置我們就已經算是對模塊的編譯到編譯運行算是有了一個整體上的認識了。對於以後深入的學習還是應該有點幫助的。下面我們將在看看於模塊相關的一些簡單的操作。
第五步:載入模塊時傳遞參數
在終端輸入:sudo insmod mole_name.ko nbr=4
在終端輸入:dmesg | tail -6
顯示結果如下:
[20800.655694] Hello, How are you. 9
[21318.675593] I come from onefile mole, I have been unlad.
[21334.425373] Hello, How are you. 0
[21334.425378] Hello, How are you. 1
[21334.425380] Hello, How are you. 2
[21334.425381] Hello, How are you. 3
這樣我們就可以看到在模塊載入的時候動態設置了我們的一個變數。初始化函數中的循環只執行了4次。
可能你會問我怎麼知道一個模塊可以設置那些變數呢。當然,你可以先不設變數載入一次。然後可以在終端輸入ls /sys/mole/<moles_name>/parameters/來查看。在這里我們是這樣輸入的
在終端輸入:ls /sys/moedle/moles/parameters/
顯示結果:
nbr
如果我們的模塊載入成功了。最後我們還可以通過modinfo來查看我們的模塊信息。如下
在終端輸入:sudo modinfo moles.ko
顯示結果:
filename: moles.ko
license: GPL
author: Yu Qiang
srcversion: 20E9C3C4E02D130E6E92533
depends:
vermagic: 2.6.24-24-generic SMP mod_unload 586
parm: nbr:int
㈣ 怎麼編譯linux mole
先在源文件目錄下寫一個makefile文件,然後執行make後就編譯好了.
makefile主要部分格式:
obj-m += 源文件名.o
all:
make -C 內核路徑 M=當前路徑 moles
ok,做正確應該沒有什麼問題的.
㈤ 如何編譯一個linux下的驅動模塊
Linux內核源碼路徑:/usr/src/linux(這個源碼是從kernel.org網站download的2.4.18版本)
按照《linux設備驅動開發詳解》一書中的步驟實現經典例子"hello,world!"的例子。
具體步驟如下:
=============================================
1.源碼如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
進入目錄:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然後拷入上面書上的源碼。
2.編譯代碼:
1>.首先我在2.4內核的虛擬機上進行編譯,編譯過程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I選項指定內河源碼,也就是內核源碼樹路徑。編譯結果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在網上查詢有網友提示沒有引入kernel.h
解決:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次編譯仍然報KERN_ALERT沒有聲明
修改編譯條件-I,再次編譯:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接著我嘗試在2.6內核的虛擬機上進行編譯
編譯過程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
3.執行代碼,載入驅動模塊:
2.4內核載入模塊:
insmod ./hello.o
但是此時並沒有輸出printk列印的信息。但是可以在/var/log/messages 中看到列印的信息,這是由於KERN_ALERT優先順序不夠高。這里
需要修改為:KERN_EMERG。再次編譯,載入模塊即可以看到結果
2.6內核載入模塊:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能會出現insmod命令找不到的錯誤,這可能有下面幾個原因:
<1> 你的系統沒有安裝mole-init-tools工具,關於此問題,只需安裝即可,但是一般裝完系統是有這個命令的。
<2> 環境變數沒有添加導致不能使用該命令。使用echo $PATH即可查看PATH環境變數,發現沒有/sbin這個路徑,所以你當然不能使用insmod這個命令了。解決的方法很簡單,只需在命令行輸入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin這個目錄下,你可以使用whereis insmod查看)。
<3> insmod這個命令需要在root許可權下才能使用。
載入完成後你可以輸入lsmod查看hello這個模塊哦。
4.卸載驅動模塊:rmmod hello.
載入模塊後就可在屏幕上看到如下信息:Hello world enter.
卸載時就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world
另外,如果有多個文件,則按下列方式編寫Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o
轉載僅供參考,版權屬於原作者。祝你愉快,滿意請採納哦
㈥ 編譯linux內核時 make moles有啥用
make moles是編譯模塊,很多驅動還有功能在選的時候選成M的都是moles,不過直接make,不加任何參數,就是make all,包含make moles。不用額外加此make,但是在安裝的時候make install只是安裝bzimage,Systemmap。沒有把moles安裝好,還要額外的make moles_install,把模塊放到/lib/moles文件夾一個和內核名一樣的文件夾下,並且運行depmod生成模塊依賴關系文件,系統啟動時載入模塊就是從dep裡面讀取信息載入模塊。
㈦ 如何編譯一個linux下的驅動模塊
linux下編譯運行驅動
嵌入式linux下設備驅動的運行和linux x86 pc下運行設備驅動是類似的,由於手頭沒有嵌入式linux設備,先在vmware上的linux上學習驅動開發。
按照如下方法就可以成功編譯出hello world模塊驅動。
1、首先確定本機linux版本
怎麼查看Linux的內核kernel版本?
'uname'是Linux/unix系統中用來查看系統信息的命令,適用於所有Linux發行版。配合使用'uname'參數可以查看當前伺服器內核運行的各個狀態。
#uname -a
Linux whh 3.5.0-19-generic #30-Ubuntu SMPTue Nov 13 17:49:53 UTC 2012 i686 i686 i686 GNU/Linux
只列印內核版本,以及主要和次要版本:
#uname -r
3.5.0-19-generic
要列印系統的體系架構類型,即的機器是32位還是64位,使用:
#uname -p
i686
/proc/version 文件也包含系統內核信息:
# cat /proc/version
Linux version 3.5.0-19-generic(buildd@aatxe) (gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) ) #30-UbuntuSMP Tue Nov 13 17:49:53 UTC 2012
發現自己的機器linux版本是:3.5.0-19-generic
2、下載機器內核對應linux源碼
到下面網站可以下載各個版本linux源碼https://www.kernel.org/
如我的機器3.5.0版本源碼下載地址為:https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.bz2
下載完後,找一個路徑解壓,如我解壓到/linux-3.5/
然後很重要的一步是:執行命令uname -r,可以看到Ubuntu的版本信息是3.5.0-19-generic
。進入linux源碼目錄,編輯Makefile,將EXTRAVERSION = 修改為EXTRAVERSION= -19-generic。
這些都是要配置源碼的版本號與系統版本號,如果源碼版本號和系統版本號不一致,在載入模塊的時候會出現如下錯誤:insmod: error inserting 'hello.ko': -1 Invalid mole format。
原因很明確:編譯時用的hello.ko的kenerl 不是我的pc的kenerl版本。
執行命令cp /boot/config-3.5.0-19-generic ./config,覆蓋原有配置文件。
進入linux源碼目錄,執行make menuconfig配置內核,執行make編譯內核。
3、寫一個最簡單的linux驅動代碼hello.c
/*======================================================================
Asimple kernel mole: "hello world"
======================================================================*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("zeroboundaryBSD/GPL");
static int hello_init(void)
{
printk(KERN_INFO"Hello World enter\n");
return0;
}
static void hello_exit(void)
{
printk(KERN_INFO"Hello World exit\n ");
}
mole_init(hello_init);
mole_exit(hello_exit);
MODULE_AUTHOR("zeroboundary");
MODULE_DESCRIPTION("A simple HelloWorld Mole");
MODULE_ALIAS("a simplestmole");
4、寫一個Makefile對源碼進行編譯
KERN_DIR = /linux-3.5
all:
make-C $(KERN_DIR) M=`pwd` moles
clean:
make-C $(KERN_DIR) M=`pwd` clean
obj-m += hello.o
5、模塊載入卸載測試
insmod hello.ko
rmmod hello.ko
然後dmesg|tail就可以看見結果了
最後,再次編譯驅動程序hello.c得到hello.ko。執行insmod ./hello.ko,即可正確insert模塊。
使用insmod hello.ko 將該Mole加入內核中。在這里需要注意的是要用 su 命令切換到root用戶,否則會顯示如下的錯誤:insmod: error inserting 'hello.ko': -1 Operation not permitted
內核模塊版本信息的命令為modinfo hello.ko
通過lsmod命令可以查看驅動是否成功載入到內核中
通過insmod命令載入剛編譯成功的time.ko模塊後,似乎系統沒有反應,也沒看到列印信息。而事實上,內核模塊的列印信息一般不會列印在終端上。驅動的列印都在內核日誌中,我們可以使用dmesg命令查看內核日誌信息。dmesg|tail
可能還會遇到這種問題insmod: error inserting 'hello.ko': -1 Invalid mole format
用dmesg|tail查看內核日誌詳細錯誤
disagrees about version of symbolmole_layout,詳細看這里。
http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmoles/index.html
在X86上我的辦法是:
make -C/usr/src/linux-headers-3.5.0-19-generic SUBDIRS=$PWD moles
㈧ 如何編譯一個linux下的驅動模塊
這是一個簡單而完整的實例,對於理解Linux下的驅動模塊是非常有幫助的。
1.源碼如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
進入目錄:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然後拷入上面書上的源碼。
2.編譯代碼:
1>.首先我在2.4內核的虛擬機上進行編譯,編譯過程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I選項指定內河源碼,也就是內核源碼樹路徑。編譯結果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在網上查詢有網友提示沒有引入kernel.h
解決:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次編譯仍然報KERN_ALERT沒有聲明
修改編譯條件-I,再次編譯:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接著我嘗試在2.6內核的虛擬機上進行編譯
編譯過程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
3.執行代碼,載入驅動模塊:
2.4內核載入模塊:
insmod ./hello.o
但是此時並沒有輸出printk列印的信息。但是可以在/var/log/messages 中看到列印的信息,這是由於KERN_ALERT優先順序不夠高。這里
需要修改為:KERN_EMERG。再次編譯,載入模塊即可以看到結果
2.6內核載入模塊:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能會出現insmod命令找不到的錯誤,這可能有下面幾個原因:
<1> 你的系統沒有安裝mole-init-tools工具,關於此問題,只需安裝即可,但是一般裝完系統是有這個命令的。
<2> 環境變數沒有添加導致不能使用該命令。使用echo $PATH即可查看PATH環境變數,發現沒有/sbin這個路徑,所以你當然不能使用insmod這個命令了。解決的方法很簡單,只需在命令行輸入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin這個目錄下,你可以使用whereis insmod查看)。
<3> insmod這個命令需要在root許可權下才能使用。
載入完成後你可以輸入lsmod查看hello這個模塊哦。
4.卸載驅動模塊:rmmod hello.
載入模塊後就可在屏幕上看到如下信息:Hello world enter.
卸載時就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world
另外,如果有多個文件,則按下列方式編寫Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o
㈨ 如何編譯一個linux下的驅動模塊
按照《linux設備驅動開發詳解》一書中的步驟實現經典例子"hello,world!"的例子。
具體步驟如下:
=============================================
1.源碼如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
進入目錄:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然後拷入上面書上的源碼。
2.編譯代碼:
1>.首先我在2.4內核的虛擬機上進行編譯,編譯過程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I選項指定內河源碼,也就是內核源碼樹路徑。編譯結果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在網上查詢有網友提示沒有引入kernel.h
解決:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次編譯仍然報KERN_ALERT沒有聲明
修改編譯條件-I,再次編譯:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接著我嘗試在2.6內核的虛擬機上進行編譯
編譯過程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
㈩ 如何編譯一個linux下的驅動模塊
首先,我們要了解一下模塊是如何別被構造的。模塊的構造過程與用戶空間
的應用程序的構造過程有顯著不同;內核是一個大的、獨立的程序
,
對於它的各
個部分如何組合在一起有詳細的明確的要求。
Linux2.6
內核的構造過程也與以
前版本的內核構造過程不同;
新的構造系統用起來更加簡單,
並且可產生更加正
確的結果
,
但是它看起來和先前的方法有很大不同。內核的構造系統非常復雜
,
我們所看到的只是它的一小部分。
如果讀者想了解更深入的細節,
則應閱讀在內
核源碼中的
Document/kbuild
目錄下的文件。
在構造內核模塊之前,
有一些先決條件首先應該得到滿足。
首先,
讀者要保證你
有適合於你的內核版本的編譯器、模塊工具
,
以及其他必要工具。在內核文檔目
錄下的文件
Documentation/Changes
里列出了需要的工具版本;
在開始構造內
核前,
讀者有必要查看該文件,
並確保已安裝了正確的工具。
如果用錯誤的工具
版本來構造一個內核
(
及其模塊
)
,可能導致許多奇怪的問題。另外也要注意
,
使
用太新版本的編譯器偶爾可能也會導致問題。
一旦做好了上面的准備工作之後
,
其實給自己的模塊創建一個
makefile
則非常
簡單。實際上
,
對於本章前面展示的
" hello world"
例子
,
下面一行就夠了
:
obj-m := hello.o
如果讀者熟悉
make
,
但是對
Linux2.6
內核構造系統不熟悉的話
,
可能奇怪這個
makefile
如何工作。畢竟上面的這一行不是一個傳統的
makefile
的樣子。問
題的答案當然是內核構造系統處理了餘下的工作。上面的賦值語句
(
它利用了由
GNU make
提供的擴展語法
)
說明有一個模塊要從目標文件
hello.o
構造,而從
該目標文件構造的模塊名稱為
hello.ko.
如果我們想由兩個源文件
(
比如
file1.c
和
file2.c )
構造出一個名稱為
mole.ko
的模塊
,
則正確的
makefile
可如下編寫
:
obj-m := mole.o
mole-objs := file1.o file2.o
為了讓上面這種類型的
makefile
文件正常工作
,
必須在大的內核構造系統環境
中調用他們。假設讀者的內核源碼數位於
~/kernel-2.6
目錄
,
用來建立你的模
塊的
make
命令
(
在包含模塊源代碼和
makefile
的目錄下鍵入
)
應該是
:
make -C ~/kernel-2.6 M=`pwd` moles
這個命令首先是改變目錄到用
-C
選項指定的位置
(
即內核源代碼目錄
)
,其中保
存有內核的頂層
makefile
文件。這個
M=
選項使
makefile
在構造
moles
目
標前
,
返回到模塊源碼目錄。
然後,
moles
目標指向
obj-m
變數中設定的模塊,
在上面的例子里,我們將該變數設置成了
mole.o
。
上面這樣的
make
命令對於多個文件的編譯顯得不是很方便
,
於是內核開發者就
開發了一種
makefile
方式
,
這種方式使得內核樹之外的模塊構造變得更加容易。
代碼清單
1.4
展示了
makefile
的編寫方法:
代碼清單
1.4 makefile
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /source/linux-2.6.13
PWD := $(shell pwd)
moles:
$(MAKE) -C $(KERNELDIR) M=$(PWD) moles
moles_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) moles_install
clean:
rm -rf *.o *~ core .depend .*. *.ko *.mod.c .tmp_versions
.PHONY: moles moles_install clean
else
obj-m := hello.o
endif
我們再次看到了擴展的
GNU
make
語法在起作用。在一個典型的構造過程中,這
個
makefile
將被讀取兩次。當從命令行中調用這個
makefile ,
它注意到
KERNELRELEASE
變數尚未設置。我們可以注意到,已安裝的模塊目錄中存在一
個符號連接,
它指向內核的構造樹,
這樣這個
makefile
就可以定位內核的源代
碼目錄。如果讀者時間運行的內核並不是要構造的內核,則可以在命令行提供
KERNELDIR=
選項或者設置
KERNELDIR
環境變數
,
或者修改
makefile
中設置
KERNELDIR
的那一行。在找到內核源碼樹
,
這個
makefile
會調用
default:
目
標
,
這個目標使用先前描述過的方法第二次運行
make
命令
(
注意,在這個
makefile
里
make
命令被參數化成
$(MAKE))
,以便運行內核構造系統。在第二
次讀取
makefile
時,
它設置了
obj-m,
而內核的
makefile
負責真正構造模塊。
這種構造模塊的機制看起來很繁瑣,可是,一旦我們習慣了使用這種機制
,
則會
欣賞內核構造系統帶給我們的便利。需要注意的是
,
上面
makefile
並不完整,
一個真正的
makefile
應包含通常用來清除無用文件的目標
,
安裝模塊的目標等
等。一個完整的例子可以參考例子代碼目錄的
makefile
。