gcc編譯驅動
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
⑵ 如何用arm-linux-gcc編譯驅動的,煩勞告訴我完整命令 ,如果可以的話 把整個流程告訴我下,多謝了
簡單的告訴你
yum install gcc* -y
⑶ 如何編寫Linux 驅動程序
以裝載和卸載模塊為例:
1、首先輸入代碼
#include <linux/init.h>
#include <linux/mole.h>

⑷ 如何編譯一個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
轉載僅供參考,版權屬於原作者。祝你愉快,滿意請採納哦
⑸ 如何用arm-linux-gcc編譯驅動程序,Makefile文件怎麼寫
回復
http://bbs.chinaunix.net/viewthread.php?tid=1921952
上面說的方法我還是不是很明白啊,我現在簡直沒辦法了,昨天晚我看到一個資料上面寫的helloworld驅動的例子有兩份Makefile,一份是x86機子上的,一份是arm平台上的,arm上的這樣寫:ifneq
($(KERNELRELEASE),)obj-m:=hello.oelseKDIR/usr/src/kernels/opt/EmbedSky/linux-2.6.30.4/all:
make
-C
$(KDIR)
M=$(PWD)
moles
ARCH=arm
CROSS_COMPLIE=arm-linux-clean:
rm
-f
*.ko
*.o
*.mod.o
*.mod.c
*.symversendif看到後我想應該是要用用於移植的內核來編譯吧,於是我乾脆把資料提供的內核拷貝到PC的Linux系統中然後解壓、編譯直至安裝,然後就再模仿例子寫了Makefile如上所示,然後make後終於通過編譯成功了,然後我再下載到arm板上安裝,卻又出現下面的錯誤:insmod:
can't
insert
'hello.ko':
unknown
symbol
in
mole,
or
unknown
parameter。我就又徹底暈了。到底是怎麼回事,應該怎麼辦的呢?
⑹ 我可以直接用gcc編譯一個驅動程序嗎
驅動程序肯定不能這么編譯啦,驅動程序要用內核來編譯的。Makefile也很復雜,我也看不太懂,但是Makefile裡面要制定內核所在的目錄以及生產的驅動文件名。這個Makefile可以直接去網上找吧,實在不行我傳給你一個。把Makefile和驅動的
c文件
放在同一個目錄下,make一下就可以產生.ko的驅動文件。移到
開發板
上insmod就可以測試了。但貌似要想在內核里配置上這個模塊。
