📄 1011.html
字号:
<HTML><HEAD><TITLE>
linux知识宝库/编程技术/linux下面Makefile探索</TITLE>
<META http-equiv=content-type content="text/html; charset=gb2312">
<META content=linuxtips,linux,php,mysql,apache name=keywords>
<STYLE type=text/css>A:link {
COLOR: #666666; TEXT-DECORATION: underline
}
A:active {
COLOR: #666666; TEXT-DECORATION: underline
}
A:visited {
COLOR: #666666; TEXT-DECORATION: underline
}
A:hover {
COLOR: #708090; TEXT-DECORATION: none
}
.articletitle {
FONT-WEIGHT: bold; FONT-SIZE: 18px; COLOR: #335588; LINE-HEIGHT: 26px; FONT-FAMILY: trebuchet ms, arial, verdana, helvetica, sans-serif; TEXT-DECORATION: none
}
INPUT {
FONT-WEIGHT: normal; FONT-SIZE: 11px; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
TEXTAREA {
FONT-WEIGHT: normal; FONT-SIZE: 11px; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
.bginput {
BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 8pt; BORDER-LEFT: #999999 1px solid; BORDER-BOTTOM: #999999 1px solid; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #f9f9f9
}
.text {
BORDER-RIGHT: #cccccc 1px solid; BORDER-TOP: #666666 1px solid; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #cccccc 1px solid
}
SELECT {
FONT-WEIGHT: normal; FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #ffffff
}
.firstalt {
BACKGROUND-COLOR: #eeeeee
}
.secondalt {
BACKGROUND-COLOR: #ffffff
}
.bigfont {
FONT-SIZE: 45px; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
.normalfont {
FONT-SIZE: 12px; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
.middlefont {
FONT-SIZE: 11px; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
.smallfont {
FONT-SIZE: 9px; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
.smallbold {
FONT-WEIGHT: bold; COLOR: #000000
}
.articletitle {
FONT-WEIGHT: bold; FONT-SIZE: 18px; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
.subhead {
FONT-WEIGHT: bold; FONT-SIZE: 13px; COLOR: #006699; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
#content A:link {
COLOR: #3366cc; TEXT-DECORATION: underline
}
#content A:active {
COLOR: #3366cc; TEXT-DECORATION: underline
}
#content A:visited {
COLOR: #3366cc; TEXT-DECORATION: underline
}
#content A:hover {
COLOR: #9966cc; TEXT-DECORATION: none
}
.content {
FONT-SIZE: 14px; LINE-HEIGHT: 130%; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
.titlefont {
FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: verdana, arial, helvetica, sans-serif,宋体
}
.bodyline {
BORDER-RIGHT: #aebdc4 1px solid; BORDER-TOP: #aebdc4 1px solid; BORDER-LEFT: #aebdc4 1px solid; BORDER-BOTTOM: #aebdc4 1px solid; BACKGROUND-COLOR: #ffffff
}
.cattitle {
FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #000000; LETTER-SPACING: 1px
}
.tableoutline {
BORDER-RIGHT: #c4c4c4 1px solid; BORDER-TOP: #c4c4c4 1px solid; BORDER-LEFT: #c4c4c4 1px solid; BORDER-BOTTOM: #c4c4c4 1px solid
}
.root_td {
PADDING-LEFT: 5px; FONT-WEIGHT: bold; FONT-SIZE: 12px; FONT-FAMILY: Verdana; HEIGHT: 22px; BACKGROUND-COLOR: #ffffff; font-color: #000000
}
.child_td {
PADDING-RIGHT: 10px; PADDING-LEFT: 10px; FONT-SIZE: 12px; PADDING-BOTTOM: 3px; COLOR: #000000; FONT-FAMILY: Verdana; HEIGHT: 22px; BACKGROUND-COLOR: #f5f5f5; TEXT-DECORATION: underline
}
#cattitle A:link {
COLOR: #000000; TEXT-DECORATION: none
}
#cattitle A:visited {
COLOR: #000000; TEXT-DECORATION: none
}
#cattitle A:active {
COLOR: #000000; TEXT-DECORATION: none
}
#cattitle A:hover {
COLOR: #000000; TEXT-DECORATION: none
}
</STYLE>
<SCRIPT language=JavaScript type=text/javascript>
<!--
if (parent.frames.length > 0) {
parent.location.href = self.document.location
}
function ToggleNode(nodeObject, imgObject){
if(nodeObject.style.display == '' || nodeObject.style.display == 'inline'){
nodeObject.style.display = 'none';
imgObject.src = 'images/plus.gif'/*tpa=http://www.linuxhero.com/docs/images/plus.gif*/;
}else{
nodeObject.style.display = 'inline';
imgObject.src = 'images/minus.gif'/*tpa=http://www.linuxhero.com/docs/images/minus.gif*/;
}
}
-->
</SCRIPT>
<BODY text=#000000 bgColor=#ffffff leftMargin=0 topMargin=0 marginwidth="0"
marginheight="0">
<CENTER>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TR>
<TD vAlign=top>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=296
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=76
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=100
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=98
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=79
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=103
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=26
border=0></TD>
<TD><IMG height=1 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1
border=0></TD></TR>
<TR>
<TD colSpan=3><A title=linux知识宝库
href="index.html" tppabs="http://www.linuxhero.com/docs/index.html"><IMG height=83
src="images/header_r1_c1.gif" tppabs="http://www.linuxhero.com/docs/images/header_r1_c1.gif" width=472 border=0
name=header_r1_c1></A></TD>
<TD colSpan=3>
<form action="http://www.linuxhero.com/docs/search.html" method=get>
<table
style="BORDER-RIGHT: #c4c4c4 1px solid; BORDER-TOP: #c4c4c4 1px solid; BORDER-LEFT: #c4c4c4 1px solid; BORDER-BOTTOM: #c4c4c4 1px solid"
cellspacing=0 cellpadding=3 width="95%" border=0 align="center">
<tbody>
<tr>
<td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif">
<div align=center><font class=normalfont>搜索文章:
<input type=hidden value=result name=action2>
<input type=radio checked value=title name=type>标题
<input type=radio value=content name=type>内容
<input type=image src="images/button_go.gif" tppabs="http://www.linuxhero.com/docs/images/button_go.gif" border=0 name=image2>
</font></div>
</td>
</tr>
<tr>
<td noWrap>
<div align="center">
<input maxlength=100 size=30 name=keyword2>
</div>
</td>
</tr></tbody>
</table>
</form>
</TD>
<TD rowSpan=2><IMG src="images/header_r1_c7.gif" tppabs="http://www.linuxhero.com/docs/images/header_r1_c7.gif" width=26 border=0 name=header_r1_c7></TD>
<TD><IMG height=83 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1 border=0></TD></TR>
<TR>
<TD background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif"><IMG height=22
src="images/header_r2_c1.gif" tppabs="http://www.linuxhero.com/docs/images/header_r2_c1.gif" width=296 border=0
name=header_r2_c1></TD>
<TD background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colSpan=5>
<DIV align=right><FONT class=normalfont>当前位置:
<A href="index.html" tppabs="http://www.linuxhero.com/docs/index.html">本站首页</A>
<font color="#FF6699">>></font>
<A href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</A> | <A href="copyright.html" tppabs="http://www.linuxhero.com/docs/copyright.html">版权说明</A></font></DIV>
</TD>
<TD><IMG height=22 src="images/spacer.gif" tppabs="http://www.linuxhero.com/docs/images/spacer.gif" width=1
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=10 cellPadding=0 width="100%" bgColor=#ffffff
border=0>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%" border=0>
<TR>
<TD vAlign=top align=middle width="60%">
<TABLE cellSpacing=0 cellPadding=0 width="100%"
background="images/back.gif" tppabs="http://www.linuxhero.com/docs/images/back.gif" border=0>
<TBODY>
<TR>
<TD vAlign=top width="80%">
<DIV align=center>
<FORM action="search.html" tppabs="http://www.linuxhero.com/docs/search.html" method=get>
</FORM>
<TABLE cellSpacing=0 cellPadding=0 width="95%"
border=0><TBODY>
<TR>
<TD background="images/bgi.gif" tppabs="http://www.linuxhero.com/docs/images/bgi.gif"
height=30></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=3 width="95%"
align=center border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%"
border=0>
<TBODY>
<TR>
<TD vAlign=top>
<p><FONT class=normalfont><B><font color=blue>linux下面Makefile探索</font></B></FONT><BR><FONT class=smallfont color=#ff9900>2004-04-23 15:18 pm</FONT><BR><FONT class=normalfont>作者:作者<br>来自:Linux知识宝库<br>联系方式:无名<br><br>Makefile 初探
<br>
==========================================
<br>
linux的内核配置文件有两个,一个是隐含的.config文件,嵌入到主Makefile中;另一个是include/linux/autoconf.h,嵌入到各个c源文件中,它们由make config、make menuconfig、make xconfig这些过程创建。几乎所有的源文件都会通过linux/config.h而嵌入autoconf.h,如果按照通常方法建立文件依赖关系(.depend),只要更新过autoconf.h,就会造成所有源代码的重新编绎。
<br>
<br>
为了优化make过程,减少不必要的重新编绎,linux开发了专用的mkdep工具,用它来取代gcc来生成.depend文件。mkdep在处理源文件时,忽略linux/config.h这样的头文件,识别源文件宏指令中具有"CONFIG_"特征的行。例如,如果有"#ifdef CONFIG_SMP"这样的行,它就会在.depend文件中输出$(wildcard /usr/src/linux/include/config/smp.h)。
<br>
<br>
include/config/下的文件是另一个工具split-include从autoconf.h中生成,它利用autoconf.h中的CONFIG_标记,生成与mkdep相对应的文件。例如,如果autoconf.h中有"#undef CONFIG_SMP"这一行,它就生成include/config/smp.h文件,内容为"#undef CONFIG_SMP"。这些文件名只在.depend文件中出现,内核源文件是不会嵌入它们的。每配置一次内核,运行split-include一次。split-include会检查旧的子文件的内容,确定是不是要更新它们。这样,不管autoconf.h修改日期如何,只要其配置不变,make就不会重新编绎内核。
<br>
<br>
如果系统的编绎选项发生了变化,linux也能进行增量编绎。为了做到这一点,make每编绎一个源文件时生成一个flags文件。例如编绎sched.c时,会在相同的目录下生成隐含的.sched.o.flags文件。它是Makefile的一个片断,当make进入某个子目录编绎时,会搜索其中的flags文件,将它们嵌入到Makefile中。这些flags代码测试当前的编绎选项与原来的是不是相同,如果相同,就将自已对应的目标文件加入FILES_FLAGS_UP_TO_DATE列表,然后,系统从编绎对象表中删除它们,得到FILES_FLAGS_CHANGED列表,最后,将它们设为目标进行更新。
<br>
<br>
下一步准备逐步深入的剖析Makefile代码。
<br>
<br>
==========================================
<br>
Makefile解读之二: sub-make
<br>
==========================================
<br>
linux各级内核源代码的子目录下都有Makefile,大多数Makefile要嵌入主目录下的Rule.make,Rule.make将识别各个Makefile中所定义的一些变量。变量obj-y表示需要编绎到内核中的目标文件名集合,定义O_TARGET表示将obj-y连接为一个O_TARGET名称的目标文件,定义L_TARGET表示将obj-y合并为一个L_TARGET名称的库文件。同样obj-m表示需要编绎成模块的目标文件名集合。如果还需进行子目录make,则需要定义subdir-y和subdir-m。在Makefile中,用"obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o"和"subdir-$(CONFIG_EXT2_FS) += ext2"这种形式自动为obj-y、obj-m、subdir-y、subdir-m添加文件名。有时,情况没有这么单纯,还需要使用条件语句个别对待。Makefile中还有其它一些变量,如mod-subdirs定义了subdir-m以外的所有模块子目录。
<br>
<br>
Rules.make是如何使make进入子目录的呢? 先来看subdir-y是如何处理的,在Rules.make中,先对subdir-y中的每一个文件名加上前缀"_subdir_"再进行排序生成subdir-list集合,再以它作为目标集,对其中每一个目标产生一个子make,同时将目标名的前缀去掉得到子目录名,作为子make的起始目录参数。subdir-m与subdir-y类似,但情况稍微复杂一些。由于subdir-y中可能有模块定义,因此利用mod-subdirs变量将subdir-y中模块目录提取出来,再与subdir-m合成一个大的MOD_SUB_DIRS集合。subdir-m的目标所用的前缀是"_modsubdir_"。
<br>
<br>
一点说明,子目录中的Makefile与Rules.make都没有嵌入.config文件,它是通过主Makefile向下传递MAKEFILES变量完成的。MAKEFILES是make自已识别的一个变量,在执行新的Makefile之前,make会首先加载MAKEFILES所指的文件。在主Makefile中它即指向.config。
<br>
<br>
<br>
==========================================
<br>
Makefile解读之三: 模块的版本化处理
<br>
==========================================
<br>
模块的版本化是内核与模块接口之间进行严格类型匹配的一种方法。当内核配置了CONFIG_MODVERSIONS之后,make dep操作会在include/linux/modules/目录下为各级Makefile中export-objs变量所对应的源文件生成扩展名为.ver的文件。
<br>
<br>
例如对于kernel/ksyms.c,make用以下命令生成对应的ksyms.ver:
<br>
<br>
gcc -E -D__KERNEL__ -D__GENKSYMS__ ksyms.c | /sbin/genksyms -k 2.4.1 > ksyms.ver
<br>
<br>
-D__GENKSYMS__的作用是使ksyms.c中的EXPORT_SYMBOL宏不进行扩展。genksyms命令识别EXPORT_SYMBOL()中的函数名和对应的原型,再根据其原型计算出该函数的版本号。
<br>
<br>
例如ksyms.c中有一行:
<br>
EXPORT_SYMBOL(kmalloc);
<br>
kmalloc原型是:
<br>
void *kmalloc(size_t, int);
<br>
genksyms程序对应的输出为:
<br>
#define __ver_kmalloc 93d4cfe6
<br>
#define kmalloc _set_ver(kmalloc)
<br>
在内核符号表和模块中,kmalloc将变成kmalloc_R93d4cfe6。
<br>
<br>
在生成完所有的.ver文件后,make将重建include/linux/modversions.h文件,它包含一系列#include指令行嵌入各个.ver文件。在编绎内核本身export-objs中的文件时,make会增加一个"-DEXPORT_SYMTAB"编绎标志,它使源文件嵌入modversions.h文件,将EXPORT_SYMBOL宏展开中的函数名字符串进行版本名扩展;同时,它也定义_set_ver()宏为一空操作,使代码中的函数名不受其影响。
<br>
在编绎模块时,make会增加"-include=linux/modversion.h -DMODVERSIONS"编绎标志,使模块中代码的函数名得到相应版本扩展。
<br>
<br>
由于生成.ver文件比较费时,make还为每个.ver创建了一个后缀为.stamp时戳文件。在make dep时,如果其.stamp文件比源文件旧才重新生成.ver文件,否则只是更新.stamp文件时戳。另外,在生成.ver和modversions.h文件时,make都会比较新文件和旧文件的内容,保持它们修改时间为最旧。
<br>
<br>
<br>
==========================================
<br>
Makefile解读之四: Rules.make的注释
<br>
==========================================
<br>
代码:
<br>
<br>
#
<br>
# This file contains rules which are shared between multiple Makefiles.
<br>
#
<br>
<br>
#
<br>
# False targets.
<br>
#
<br>
#
<br>
.PHONY: dummy
<br>
<br>
#
<br>
# Special variables which should not be exported
<br>
#
<br>
# 取消这些变量通过环境向make子进程传递。
<br>
unexport EXTRA_AFLAGS # as 的开关
<br>
unexport EXTRA_CFLAGS # cc 的开关
<br>
unexport EXTRA_LDFLAGS # ld 的开关
<br>
unexport EXTRA_ARFLAGS # ar 的开关
<br>
unexport SUBDIRS #
<br>
unexport SUB_DIRS # 编绎内核需进入的子目录,等于subdir-y
<br>
unexport ALL_SUB_DIRS # 所有的子目录
<br>
unexport MOD_SUB_DIRS # 编绎模块需进入的子目录
<br>
unexport O_TARGET # ld合并的输出对象
<br>
unexport ALL_MOBJS # 所有的模块名
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -