- 論壇徽章:
- 0
|
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
st1\:*{behavior:url(#ieooui) }
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
嵌入式Linux驅(qū)動(dòng)開(kāi)發(fā)筆記
1.1
模塊的編譯
Linux驅(qū)動(dòng)一般以模塊module的形式來(lái)加載,首先需要把驅(qū)動(dòng)編譯成模塊的形式。簡(jiǎn)單的例子,
Begin of hello.c file
#include
#include
#include
static int __init test_init(void)
{
printk("init
module\n");
return 0;
}
static void __exit test_exit(void)
{
printk("exit
modules\n");
}
module_init(test_init);
module_exit(test_exit);
Over of hello.c file
Makefile為,
PWD = $(shell pwd)
KERNEL_SRC = /usr/src/linux-source-2.6.15/
obj-m := test.o
module-objs := test.o
all:
$(MAKE)
-C $(KERNEL_SRC) M=$(PWD) modules
clean:
rm *.ko
rm *.o
在test.c和Makefile所在的目錄下運(yùn)行make,如果看到類似輸出
make -C /usr/src/linux-source-2.6.15/
M=/home/vmeth modules
make[1]: Entering directory `/usr/src/linux-source-2.6.15'
CC [M] /home/vmeth/hello.o
Building modules, stage 2.
MODPOST
CC /home/vmeth/hello.mod.o
LD [M] /home/vmeth/hello.ko
make[1]: Leaving directory `/usr/src/linux-source-2.6.15'
一般用下面的Makefile,
# Makefile2.6
ifneq ($(KERNELRELEASE),)
#kbuild syntax. dependency relationshsip of files and
target modules are listed here.
mymodule-objs := hello.o
obj-m := hello.o
else
PWD := $(shell
pwd)
KVER ?= $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
$(MAKE)
-C $(KDIR) M=$(PWD)
clean:
rm -rf
.*.cmd *.o *.mod.c *.ko .tmp_versions
endif
KERNELRELEASE 是在內(nèi)核源碼的頂層Makefile中定義的一個(gè)變量,在第一次讀取執(zhí)行此Makefile時(shí),KERNELRELEASE沒(méi)有被定義,所以make將讀取執(zhí)行else之后的內(nèi)容。
當(dāng)make的目標(biāo)為all時(shí),-C $(KDIR) 指明跳轉(zhuǎn)到內(nèi)核源碼目錄下讀取那里的Makefile;M=$(PWD) 表明然后返回到當(dāng)前目錄繼續(xù)讀入、執(zhí)行當(dāng)前的Makefile。
當(dāng)從內(nèi)核源碼目錄返回時(shí),KERNELRELEASE已被被定義,kbuild也被啟動(dòng)去解析kbuild語(yǔ)法的語(yǔ)句,make將繼續(xù)讀取else之前的內(nèi)容。else之前的內(nèi)容為kbuild語(yǔ)法的語(yǔ)句, 指明模塊源碼中各文件的依賴關(guān)系,以及要生成的目標(biāo)模塊名。
每個(gè)內(nèi)核的名字都包含了它的版本號(hào),這也是 uname -r 命令顯示的值。
下面附件中是一個(gè)例子,
![]()
文件:MyNotes.rar
大小:1KB
下載:
下載
1.2 待續(xù)
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u2/70351/showart_1003342.html |
|