初级会员
- 积分
- 81
- 金钱
- 81
- 注册时间
- 2012-10-7
- 在线时间
- 1 小时
|
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">在</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">操作系统中,普遍使用</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ELF</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">格式作为可执行程序或者程序生成过程中的中间格式。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ELF</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">(</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Executable and Linking Format</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">,可执行连接格式)是</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">UNIX</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">系统实验室(</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">USL</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">)作为应用程序二进制接口(</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Application BinaryInterface</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">,</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ABI</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">)而开发和发布的。工具接口标准委员会(</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">TIS</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">)选择了正在发展中的</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ELF</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">标准作为工作在</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">32</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">位</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Intel</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">体系上不同操作系统之间可移植的二进制文件格式。源代码到可执行程序的转换时需要经历以下过程:<br />
<center><img src="/data/attachment/forum/2014/4/7/848c20f42924f6ed49b89d5d51f0a8d3_501.jpg" align="center" alt="" border="0" /></center><br />
<br />
</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoListParagraph" style="margin-left:36.0pt;text-indent:-36.0pt;line-height:19.5pt;mso-list:l0 level1 lfo1;background:white;"><!--[if !supportLists]--><span class="apple-converted-space"><span lang="EN-US">(1)<span style="font-size:7pt;line-height:normal;font-family:'Times New Roman';"> </span></span></span><!--[endif]--><span class="apple-converted-space"><span style="background-color:white;background-position:initial initial;background-repeat:initial initial;">预处理<span lang="EN-US"><o:p></o:p></span></span></span></p>
<p class="MsoListParagraph" style="margin-left:36.0pt;text-indent:-36.0pt;line-height:19.5pt;mso-list:l0 level1 lfo1;background:white;"><!--[if !supportLists]--><span class="apple-converted-space"><span lang="EN-US">(2)<span style="font-size:7pt;line-height:normal;font-family:'Times New Roman';"> </span></span></span><!--[endif]--><span class="apple-converted-space"><span style="background-color:white;background-position:initial initial;background-repeat:initial initial;">编译<span lang="EN-US"><o:p></o:p></span></span></span></p>
<p class="MsoListParagraph" style="margin-left:36.0pt;text-indent:-36.0pt;line-height:19.5pt;mso-list:l0 level1 lfo1;background:white;"><!--[if !supportLists]--><span class="apple-converted-space"><span lang="EN-US">(3)<span style="font-size:7pt;line-height:normal;font-family:'Times New Roman';"> </span></span></span><!--[endif]--><span class="apple-converted-space"><span style="background-color:white;background-position:initial initial;background-repeat:initial initial;">汇编<span lang="EN-US"><o:p></o:p></span></span></span></p>
<p class="MsoListParagraph" style="margin-left:36.0pt;text-indent:-36.0pt;line-height:19.5pt;mso-list:l0 level1 lfo1;background:white;"><!--[if !supportLists]--><span class="apple-converted-space"><span lang="EN-US">(4)<span style="font-size:7pt;line-height:normal;font-family:'Times New Roman';"> </span></span></span><!--[endif]--><span class="apple-converted-space"><span style="background-color:white;background-position:initial initial;background-repeat:initial initial;">链接<span lang="EN-US"><o:p></o:p></span></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="font-size:12pt;font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;"> </span></span><span class="apple-converted-space"><span style="font-size:12pt;font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">预处理阶段:</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;">读取</span><span lang="EN-US">c</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:calibri;mso-hansi-theme-font:minor-latin;">源程序,对其中的伪指令(以</span><span lang="EN-US">#</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:calibri;mso-hansi-theme-font:minor-latin;">开头的指令)和特殊符号进行处理</span><span lang="EN-US" style="background:white;"><br />
</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;">伪指令主要包括以下四个方面:</span><span lang="EN-US" style="background:white;"><br />
</span><span lang="EN-US"> </span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:calibri;mso-hansi-theme-font:minor-latin;">(</span><span lang="EN-US">1</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;">)宏定义指令(</span><span lang="EN-US">2</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:calibri;mso-hansi-theme-font:minor-latin;">)条件编译指令(</span><span lang="EN-US">3</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;">)头文件包含指令(</span><span lang="EN-US">4</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:calibri;mso-hansi-theme-font:minor-latin;">)特殊符号</span><span lang="EN-US"> </span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;">预编译程序所完成的基本上是对源程序的</span><span lang="EN-US">“</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;">替代</span><span lang="EN-US">”</span></span><span class="apple-converted-space"><span style="font-family:宋体;mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:calibri;mso-hansi-theme-font:minor-latin;">工作。经过此种替代,生成一个没有宏定义、没有条件编译指令、没有特殊符号的输出文件。这个文件的含义同没有经过预处理的源文件是相同的,但内容有所不同。</span><span lang="EN-US"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">编译是指把用高级语言编写的程序转换成相应处理器的汇编语言程序的过程。从本质上讲,编译是一个文本转换的过程。对嵌入式系统而言,一般要把用</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">C</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">语言编写的程序转换成处理器的汇编代码。编译过程包含了</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">C</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">语言的语法解析和汇编码的生成两个步骤。</span><span lang="EN-US"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="margin-left:21pt;text-indent:-21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">l </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">汇编是从汇编语言程序生成目标系统的二进制代码(机器代码)的过程。机器代码的生成和处理器有密切的联系。对于一款特定的处理器,其汇编语言和二进制的机器代码是一一对应的。汇编过程的输入是汇编代码,这个汇编代码可能来源于编译过程的输出,也可以是直接用汇编语言书写的程序。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="margin-left:21pt;text-indent:-21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">l </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">连接是指将汇编生成的多段机器代码组合成一个可执行程序。一般来说,通过编译和汇编过程,每一个源文件将生成一个目标文件。连接器的作用就是将这些目标文件组合起来,组合的过程包括了代码段、数据段等部分的合并,以及添加相应的文件头。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">GCC</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">是</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">下主要的程序生成工具,它除了编译器、汇编器、连接器外,还包括一些辅助工具。对于最后编译出来的可执行程序,当我们执行它的时候,操作系统又是如何反应的呢?如图</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">2</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">所示:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="center" style="text-align:center;line-height:19.5pt;mso-pagination:widow-orphan;background:white;"><span class="apple-converted-space"><span lang="EN-US"><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"> <v:f eqn="sum @0 1 0"> <v:f eqn="sum 0 0 @1"> <v:f eqn="prod @2 1 2"> <v:f eqn="prod @3 21600 pixelWidth"> <v:f eqn="prod @3 21600 pixelHeight"> <v:f eqn="sum @0 0 1"> <v:f eqn="prod @6 1 2"> <v:f eqn="prod @7 21600 pixelWidth"> <v:f eqn="sum @8 21600 0"> <v:f eqn="prod @7 21600 pixelHeight"> <v:f eqn="sum @10 21600 0"> </v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"> <o:lock v:ext="edit" aspectratio="t"> </o:lock></v:path></v:stroke></v:shapetype><v:shape id="图片_x0020_2" o:spid="_x0000_i1031" type="#_x0000_t75" alt="http://hi.csdn.net/attachment/201203/12/0_1331537040ALw9.gif" style="width:243.75pt;height:207pt;visibility:visible;mso-wrap-style:square;"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image001.gif" o:title="0_1331537040ALw9"> </v:imagedata></v:shape></span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">的操作系统提供了一系列的接口,这些接口被称为系统调用(</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">System Call</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">)。系统调用</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">"</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">提供的是机制,而不是策略</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">"</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">C</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">语言的库函数通过调用系统调用来实现,库函数对上层提供了</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">C</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">语言库文件的接口。在应用程序层,通过调用</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">C</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">语言库函数和系统调用来实现功能。一般来说,应用程序大多使用</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">C</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">语言库函数实现其功能,较少使用系统调用。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ELF</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">文件格式包括三种主要的类型:可执行文件、可重定向文件、共享库。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">1</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">.可执行文件(应用程序)</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">可执行文件包含了代码和数据,是可以直接运行的程序。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">2</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">.可重定向文件(</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">*.o</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">)</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">可重定向文件又称为目标文件,它包含了代码和数据(这些数据是和其他重定位文件和共享的</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">object</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">文件一起连接时使用的)。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">*.o</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">文件参与程序的连接(创建一个程序)和程序的执行(运行一个程序),它提供了一个方便有效的方法来用并行的视角看待文件的内容,这些</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">*.o</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">文件的活动可以反映出不同的需要。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">下,我们可以用</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">gcc -c</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">编译源文件时可将其编译成</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">*.o</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">格式。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">3</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">.共享文件(</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">*.so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">)</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">也称为动态库文件,它包含了代码和数据(这些数据是在连接时候被连接器</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ld</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">和运行时动态连接器使用的)。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">那么到底什么是库呢?</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行。库分静态库和动态库两种。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">静态库:这类库的名字一般是</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">libxxx.a</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">,</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">xxx</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">为库的名字。利用静态函数库编译成的文件比较大,因为整个函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后的执行程序不需要外部的函数库支持,因为所有使用的函数都已经被编译进去了。当然这也会成为他的缺点,因为如果静态函数库改变了,那么你的程序必须重新编译。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">动态库:这类库的名字一般是</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">libxxx.M.N.so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">,同样的</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">xxx</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">为库的名字,</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">M</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">是库的主版本号,</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">N</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">是库的副版本号。当然也可以不要版本号,但名字必须有。相对于静态函数库,动态函数库在编译的时候并没有被编译进目标代码中,你的程序执行到相关函数时才调用该函数库里的相应函数,因此动态函数库所产生的可执行文件比较小。由于函数库没有被整合进你的程序,而是程序运行时动态的申请并调用,所以程序的运行环境中必须提供相应的库。动态函数库的改变并不影响你的程序,所以动态函数库的升级比较方便。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">系统有几个重要的目录存放相应的函数库,如</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">/lib /usr/lib</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">当要使用静态的程序库时,连接器会找出程序所需的函数,然后将它们拷贝到执行文件,由于这种拷贝是完整的,所以一旦连接成功,静态程序库也就不再需要了。然而,对动态库而言,就不是这样。动态库会在执行程序内留下一个标记指明当程序执行时,首先必须载入这个库。由于动态库节省空间,</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">下进行连接的缺省操作是首先连接动态库,也就是说,如果同时存在静态和动态库,不特别指定的话,将与动态库相连接。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"> <o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"> 1</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">、静态链接库</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"> </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">我们先制作自己的静态链接库,然后再使用它。制作静态链接库的过程中要用到</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">gcc</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">和</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ar</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">命令。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"> </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">准备两个源码文件</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">main.c</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">和</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">hello.c</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">,</span><span style="background-color:white;background-position:initial initial;background-repeat:initial initial;"> </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">内容如下图示:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left"><span class="apple-converted-space"><span lang="EN-US"><v:shape id="图片_x0020_37" o:spid="_x0000_i1030" type="#_x0000_t75" style="width:411pt;height:303pt;visibility:visible;mso-wrap-style:square;"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image002.jpg" o:title="AO96ZE`5LPS$`EHCAL}W77Q"> </v:imagedata></v:shape></span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">接下来使用命令把</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">hello.c </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">制作成静态库文件</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">libhello.a</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left"><span class="apple-converted-space"><span lang="EN-US"><v:shape id="图片_x0020_39" o:spid="_x0000_i1029" type="#_x0000_t75" style="width:411pt;height:115.5pt;visibility:visible;mso-wrap-style:square;"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image003.jpg" o:title="%G]_U8ODJR_P6%WZ9_CA3U0"> </v:imagedata></v:shape></span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">由上面知道静态库文件</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">libmytest.a</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">已经生成,用</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">file</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">命令查看其属性,发现它确实是归档压缩文件。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">在</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">main.c</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">中调用了</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">hello.c</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">实现的函数,使用静态库的方式编译得到一个可执行文件</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">main_a<o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p> </o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">2</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">、动态库</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21.5pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">动态库的后缀为</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">*.so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">。在</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">发行版中大多数的动态库基本都位于</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">/usr/lib</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">和</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">/lib</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">目录下。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">有时候当我们的应用程序无法运行时,它会提示我们说它找不到什么样的库,或者哪个库的版本又不合它胃口了等等之类的话。那么应用程序它是怎么知道需要哪些库的呢?我们前面已几个学了个很棒的命令</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ldd</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">,用就是用来查看一个文件到底依赖了那些</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">库文件。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">依旧使用刚才的源码文件</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">将其中的</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">hello.c</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">制作成一个名为</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">libhello.so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">的动态链接库文件:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"> 1</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">、先生成目标</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">.o</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">文件:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">[root@localhost make_so_a]# gcc -c hello.c -o hello.o<o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">2</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">、再生成</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">文件:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">[root@localhost make_so_a]# gcc -fpic -shared hello.o -o libhello.so<o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">-shared</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">该选项指定生成动态连接库(让连接器生成</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">T</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">类型的导出符号表),不用该标志外部程序无法连接。相当于一个可执行文件。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">-fpic</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">接下来,就是如何使用这个动态库了。使用动态链接的方式生成最终的可执行文件</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left"><span class="apple-converted-space"><span lang="EN-US"><v:shape id="图片_x0020_41" o:spid="_x0000_i1028" type="#_x0000_t75" style="width:411pt;height:103.5pt;visibility:visible;mso-wrap-style:square;"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image004.jpg" o:title="YV~O_Z9~FRKW}UV9PQEH6D2"> </v:imagedata></v:shape></span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">使用“</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">-lhello</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">”标记来告诉</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">GCC</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">驱动程序在连接阶段引用共享函数库</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">libhello.so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">。“</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">-L./</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">”标记告诉</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">GCC</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">函数库位于当前目录。否则</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">GNU</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">连接器会查找标准系统函数目录。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">这里我们注意,</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">ldd</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">的输出它说我们的</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">libtest.so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">它没找到。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">这是因为生成的可执行文件执行时搜索路径的顺序是:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">1. </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">环境变量</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">LD_LIBRARY_PATH</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">指定的动态库搜索路径</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">2. </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">配置文件</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">/etc/ld.so.conf</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">中指定的动态库搜索路径</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">3. </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">默认的动态库搜索路径</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">/lib<o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">4. </span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">默认的动态库搜索路径</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">/usr/lib<o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p> </o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">我们可以使用以下几种方法</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">方法一:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">使用</span><span lang="EN-US">ldconfig</span></span><span class="apple-converted-space"><span style="font-family:宋体;">命令,它是一个动态链接库管理命令</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><br />
</span><span lang="EN-US">ldconfig </span></span><span class="apple-converted-space"><span style="font-family:宋体;">命令的用途</span><span lang="EN-US">,</span></span><span class="apple-converted-space"><span style="font-family:宋体;">主要是在默认搜寻目录</span><span lang="EN-US">(/lib</span></span><span class="apple-converted-space"><span style="font-family:宋体;">和</span><span lang="EN-US">/usr/lib)</span></span><span class="apple-converted-space"><span style="font-family:宋体;">以及动态库配置文件</span><span lang="EN-US">/etc/ld.so.conf</span></span><span class="apple-converted-space"><span style="font-family:宋体;">内所列的目录下</span><span lang="EN-US">,</span></span><span class="apple-converted-space"><span style="font-family:宋体;">搜索出可共享的动态</span> </span><span class="apple-converted-space"><span style="font-family:宋体;">链接库</span><span lang="EN-US">(</span></span><span class="apple-converted-space"><span style="font-family:宋体;">格式如前介绍</span><span lang="EN-US">,lib*.so*),</span></span><span class="apple-converted-space"><span style="font-family:宋体;">进而创建出动态装入程序</span><span lang="EN-US">(ld.so)</span></span><span class="apple-converted-space"><span style="font-family:宋体;">所需的连接和缓存文件</span><span lang="EN-US">.</span></span><span class="apple-converted-space"><span style="font-family:宋体;">缓存文件默认为</span><span lang="EN-US"> /etc/ld.so.cache,</span></span><span class="apple-converted-space"><span style="font-family:宋体;">此文件保存已排好序的动态链接库名字列表</span><span lang="EN-US">.</span></span><span class="apple-converted-space"><span lang="EN-US" style="font-size:12pt;font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;"> </span><span lang="EN-US">ldconfig</span></span><span class="apple-converted-space"><span style="font-family:宋体;">通常在系统启动时运行</span><span lang="EN-US">,</span></span><span class="apple-converted-space"><span style="font-family:宋体;">而当用户安装了一个新的动态链接库时</span><span lang="EN-US">,</span></span><span class="apple-converted-space"><span style="font-family:宋体;">就需要手工运行这个命令</span><span lang="EN-US">.<o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;">执行如下:</span></span><span class="apple-converted-space"><span lang="EN-US" style="font-size:12pt;font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;"> </span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left"><span class="apple-converted-space"><span lang="EN-US"><v:shape id="图片_x0020_43" o:spid="_x0000_i1027" type="#_x0000_t75" style="width:417pt;height:156pt;visibility:visible;mso-wrap-style:square;"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image005.jpg" o:title="W](~ZDS`9)RP@[~}{{Z4C)X"> </v:imagedata></v:shape></span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">方法二:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">将生成的动态库文件拷贝到</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">系统默认搜索的动态库文件的目录中,如</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">/lib /usr/lib</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">目录下,然后生成可执行文件并执行,如图示:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US"><v:shape id="图片_x0020_48" o:spid="_x0000_i1026" type="#_x0000_t75" style="width:414.75pt;height:188.25pt;visibility:visible;mso-wrap-style:square;"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image006.png" o:title=""> </v:imagedata></v:shape></span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p> </o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">方法三:</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">添加系统的环境变量</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">LD_LIBRARY_PATH</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">指定动态库的目录</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">执行可以看到,使用当前目录下的动态库文件链接生成可执行文件</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">main_so</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">,但是在执行时提示找不到相应的动态库,这时我们通过向系统添加环境变量</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">LD_LIBRARY_PATH</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">告诉动态链接库的地址,如图所示</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span lang="EN-US"><v:shape id="图片_x0020_51" o:spid="_x0000_i1025" type="#_x0000_t75" style="width:414pt;height:160.5pt;visibility:visible;mso-wrap-style:square;"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image007.png" o:title=""> </v:imagedata></v:shape></span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p>
<p class="MsoNormal" align="left" style="text-indent:21pt;line-height:19.5pt;background-color:white;background-position:initial initial;background-repeat:initial initial;"><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">通过本文的叙述和练习相信大家应该对</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;">Linux</span></span><span class="apple-converted-space"><span style="font-family:宋体;background-color:white;background-position:initial initial;background-repeat:initial initial;">的库函数机制有了些许了解,最主要的是学会怎么去开发使用库文件。</span><span lang="EN-US" style="background-color:white;background-position:initial initial;background-repeat:initial initial;"><o:p></o:p></span></span></p> |
|