<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > kbuild系統-內核模塊的編譯

kbuild系統-內核模塊的編譯

作者: 時(shí)間:2012-07-30 來(lái)源:網(wǎng)絡(luò ) 收藏

Linux是一種單體,但是通過(guò)動(dòng)態(tài)加載的方式,使它的開(kāi)發(fā)非常靈活方便。那么,它是如何的呢?我們可以通過(guò)分析它的Makefile入手。以下是一個(gè)簡(jiǎn)單的hello內核的Makefile.

本文引用地址:http://dyxdggzs.com/article/148696.htm

ifneq ($(KERNELRELEASE),)

obj-m:=hello.o

else

KERNELDIR:=/lib/modules/$(shell uname -r)/build

PWD:=$(shell pwd)

default:

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

clean:

rm -rf *.o *.mod.c *.mod.o *.ko

endif

當我們寫(xiě)完一個(gè)hello,只要使用以上的makefile。然后make一下就行。

假設我們把hello模塊的源代碼放在/home/study/prog/mod/hello/下。

當我們在這個(gè)目錄運行make時(shí),make是怎么執行的呢?

LDD3第二章第四節“和裝載”中只是簡(jiǎn)略地說(shuō)到該Makefile被執行了兩次,但是具體過(guò)程是如何的呢?

首先,由于make 后面沒(méi)有目標,所以make會(huì )在Makefile中的第一個(gè)不是以.開(kāi)頭的目標作為默認的目標執行。于是default成為make的目標。make會(huì )執行$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

。

shell是make內部的函數,假設當前內核版本是2.6.13-study,所以$(shell uname -r)的結果是 2.6.13-study

這里,實(shí)際運行的是

make -C /lib/modules/2.6.13-study/build M=/home/study/prog/mod/hello/ modules

/lib/modules/2.6.13-study/build是一個(gè)指向內核源代碼/usr/src/linux的符號鏈接。

可見(jiàn),make執行了兩次。第一次執行時(shí)是讀hello模塊的源代碼所在目錄/home/study/prog/mod/hello/下的Makefile。第二次執行時(shí)是執行/usr/src/linux/下的Makefile時(shí).

但是還是有不少令人困惑的問(wèn)題:

1.這個(gè)KERNELRELEASE也很令人困惑,它是什么呢?在/home/study/prog/mod/hello/Makefile中是沒(méi)有定義這個(gè)變量的,所以起作用的是else...endif這一段。不過(guò),如果把hello模塊移動(dòng)到內核源代碼中。例如放到/usr/src/linux/driver/中,KERNELRELEASE就有定義了。

在/usr/src/linux/Makefile中有

162 KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)$(LOCALVERSION)

這時(shí)候,hello模塊也不再是單獨用make,而是在內核中用make modules進(jìn)行編譯。

用這種方式,該Makefile在單獨編譯和作為內核一部分編譯時(shí)都能正常工作。

2.這個(gè)obj-m := hello.o什么時(shí)候會(huì )執行到呢?

在執行:

make -C /lib/modules/2.6.13-study/build M=/home/study/prog/mod/hello/ modules

時(shí),make 去/usr/src/linux/Makefile中尋找目標modules:

862 .PHONY: modules

863 modules: $(vmlinux-dirs) $(if $(KBUILD_BUILTIN),vmlinux)

864 @echo ' Building modules, stage 2.';

865 $(Q)$(MAKE) -rR -f $(srctree)/scripts/Makefile.modpost

可以看出,分兩個(gè)stage:

1.編譯出hello.o文件。

2.生成hello.mod.o hello.ko

在這過(guò)程中,會(huì )調用

make -f scripts/Makefile.build bj=/home/study/prog/mod/hello

而在 scripts/Makefile.build會(huì )包含很多文件:

011 -include .config

012

013 include $(if $(wildcard $(obj)/Kbuild), $(obj)/Kbuild, $(obj)/Makefile)

其中就有/home/study/prog/mod/hello/Makefile

這時(shí) KERNELRELEASE已經(jīng)存在。

所以執行的是:

obj-m:=hello.o

關(guān)于make modules的更詳細的過(guò)程可以在scripts/Makefile.modpost文件的注釋中找到。如果想查看make的整個(gè)執行過(guò)程,可以運行make -n。

由此可見(jiàn),內核的Kbuild龐大而復雜。



關(guān)鍵詞: 編譯 模塊 內核 系統 kbuild

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>