📄 0520source_code_and_tarball.htm
字号:
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en"><html><head> <meta http-equiv="Content-Type" content="text/html; "> <meta name="Author" content="VBird"> <meta name="GENERATOR" content="Mozilla/4.5 [zh-TW] (WinNT; I) [Netscape]"> <title>鸟哥的 Linux 私房菜 -- 原始码与Tarball 套件管理员</title><!-- saved from url=(0022)http://internet.e-mail --><style> <!-- body{background-image=url(http://linux.vbird.org/VBirdLinux.jpg);background-ATTACHMENT=FIXED} --> </style></head><body nosave="" background="0520source_code_and_tarball_files/VBirdLinux.jpg"><center><center><b><font size="+2" color="#3333ff"><font face="SimSun">鸟哥的</font><font face="Times New Roman,Times"> Linux </font><font face="SimSun">与</font><font face="Times New Roman,Times"> ADSL </font><font face="SimSun">私房菜</font></font></b><br><a href="http://linux.vbird.org/" target="_top"><img src="0520source_code_and_tarball_files/VBirdTitle2.jpg" nosave="" border="0" height="25" width="90"></a><a href="http://linux.vbird.org/linux_basic"><img src="0520source_code_and_tarball_files/icon_system.gif" nosave="" border="0" height="25" width="90"></a><a href="http://linux.vbird.org/linux_server"><img src="0520source_code_and_tarball_files/icon_server.gif" nosave="" border="0" height="25" width="90"></a><a href="http://linux.vbird.org/linux_security"><img src="0520source_code_and_tarball_files/icon_security.jpg" nosave="" border="0" height="25" width="90"></a><a href="http://phorum.vbird.org/" target="_blank"><img src="0520source_code_and_tarball_files/icon_forums.gif" nosave="" border="0" height="25" width="90"></a><a href="http://linux.vbird.org/adsl"><img src="0520source_code_and_tarball_files/icon_adsl.gif" nosave="" border="0" height="25" width="90"></a><br><hr width="100%"></center><font size="+2" color="#3333ff"><a href="http://linux.vbird.org//linux_basic/0520source_code_and_tarball.php" target="_self"><font face="SimSun">原始码与</font><font face="Times New Roman,Times">Tarball </font><font face="SimSun">套件管理员</font></a></font></center><div align="right"><font size="-1" color="#3333ff" face="SimSun">最近更新日期:2004/04/10</font></div><font size="-2" color="#3333ff" face="SimSun"> </font><table bgcolor="#ffcccc" border="1" cols="1" width="100%"><tbody><tr><td><font size="-1" color="#000099">我们在 <a href="http://linux.vbird.org//linux_basic/0110whatislinux.php">Linux是什么</a> 一文当中,提到了 GNU 与 GPL 还有开放源码等咚咚,不过,前面都还没有提到真正的开放源码是什么的讯息!在这一章当中,我们将藉由Linux 操作系统里面的执行文件,来理解什么是可执行的程序,以及了解什么是编译器。另外,与程序息息相关的函式库(library)的信息也需要了解一番!不过,在这个章节当中,鸟哥并不是要您成为一个开放源码的程序设计师,而是希望您可以了解如何将开放源码的程序设计、加入函式库的原理、透过编译而成为可以执行的 binary file ,最后该执行档可被我们所使用的一连串过程!</font><br><font size="-1" color="#000099"> </font><br><font size="-1" color="#000099">了解上面的咚咚有什么好处呢?!因为在Linux 的世界里面,我们常常需要自行安装套件在自己的 Linux系统上面,所以如果您有简单的程序编译概念,那么将很容易进行套件的安装,甚至在发生套件编译过程中的错误时,您也可以自行作一些简易的修订呢!而最传统的套件安装过程,自然就是由原始码编译而来的啰!所以,在这里我们将介绍最原始的套件管理方式:使用Tarball 来安装与升级管理我们的套件喔!</font></td></tr></tbody></table><font size="-2" color="#000099"> </font><br><font size="+1" color="#000099"><a href="#intro">前言</a>:</font><br><font color="#000099"><font size="+1"> :</font><a href="#intro_whatisopensource">什么是开放源码、编译器与可执行档</a>?</font><br><font color="#000099"><font size="+1"> :</font><a href="#intro_library">什么是函式库</a>?</font><br><font color="#000099"><font size="+1"> :</font><a href="#intro_make">什么是 make 与 configure</a> ?</font><br><font color="#000099"><font size="+1"> :</font><a href="#intro_tarball">什么是Tarball 的套件</a>?</font><br><font color="#000099"><font size="+1"> :</font><a href="#intro_howto">如何安装与升级套件</a>?</font><br><font size="+1" color="#000099"><a href="#simple_ex">一个简单的范例</a>:</font><br><font color="#000099"><font size="+1"> :</font><a href="#simple_ex_hello">印出Hello World</a></font><br><font color="#000099"><font size="+1"> :</font><a href="#simple_ex_function">子程序的编译</a></font><br><font color="#000099"><font size="+1"> :</font><a href="#simple_ex_lib">加入连结的函式库</a></font><br><font color="#000099"><font size="+1"> :</font><a href="#simple_ex_gcc">gcc的用法</a></font><br><font size="+1" color="#000099"><a href="#make">make 的简易用法</a>:</font><br><font color="#000099"><font size="+1"> :</font><a href="#make_why">为什么要用make</a> ?</font><br><font color="#000099"><font size="+1"> :</font><a href="#make_makefile">make的基本语法与变量</a></font><br><font size="+1" color="#000099"><a href="#tarball">Tarball 的管理与建议</a>:</font><br><font color="#000099"><font size="+1"> :</font><a href="#tarball_pack">使用原始码管理套件所需要的基础套件</a></font><br><font color="#000099"><font size="+1"> :</font><a href="#tarball_step">Tarball安装的基本步骤</a></font><br><font color="#000099"><font size="+1"> :</font><a href="#tarball_common">一般Tarball 套件安装的建议事项( 如何移除?升级? )</a></font><br><font color="#000099"><font size="+1"> :</font><a href="#tarball_exntp">一个简单的范例、利用ntp 来示范</a></font><br><font color="#000099"><font size="+1"> :</font><a href="#tarball_patch">利用patch 更新原始码</a></font><br><font size="+1" color="#000099"><a href="#library">函式库管理</a>:</font><br><font color="#000099"><font size="+1"> :</font><a href="#library_dyna_sta">动态与静态函式库</a></font><br><font color="#000099"><font size="+1"> :</font><a href="#library_ldconfig">ldconfig与 /etc/ld.so.conf</a>, <a href="#library_ldd">ldd</a>,</font><br><font color="#000099"><font size="+1"><a href="#check">检验套件软件的正确性</a></font>:</font><br><font color="#000099"><font size="+1"> :</font><a href="#check_md5sum">md5sum</a></font><br><font size="+1" color="#000099"><a href="#important">重点回顾</a></font><br><font size="+1" color="#000099"><a href="#reference">参考资源</a></font><br><font size="+1" color="#000099"><a href="#FAQ">课后练习</a></font><br><hr width="100%"><a name="intro"></a><img src="0520source_code_and_tarball_files/penguin-m.gif" alt="" nosave="" align="middle" height="34" width="25"><font size="+1" color="#000099">前言:</font><blockquote>如果鸟哥想要在我的 Linux 服务器上面跑网页服务器( WWW server)这项服务,那么我应该要做些什么事呢?呵呵!当然就一定需要『<font color="#000066">安装网页服务器的套件</font>』啰!如果鸟哥的服务器上面没有这个套件的话,那当然也就无法启用 WWW 的服务啦!所以啦,想要在您的Linux 上面进行一些有的没的功能,学会『<font color="#000066">如何安装套件</font>』是很重要的一个课题!<br> <br>咦!安装套件有什么难的?在 Windows 操作系统上面安装套件时,不是只要一直给他按『下一步』就可以安装妥当了吗?话是这样说没错啦,不过,也由于如此,所以在Windows 系统上面的软件都是一模一样的,也就是说,您『<font color="#000066">无法修改该软件的原始程序代码</font>』,因此,万一您想要增加或者减少该软件的某些功能时,呵呵!大概只能求助于当初发行该软件套件的厂商了!<br> <br>或许你会说:『唉呦!我不过是一般人,不会用到多余的功能,所以不太可能会更动到程序代码的部分吧!?』如果您这么想的话,很抱歉~是有问题的!怎么说呢?像目前网络上面的病毒、黑客软件、臭虫程序等等,都可能对您的主机上面的某些软件造成影响(<i><font color="#000066">这是因为软件开发者在写作之初可能并没有想到某些问题所致!</font></i>),导致主机的当机或者是其它数据损毁等等的伤害。如果您可以藉由安全信息单位所提供的修订方式进行修改,那么您将可以很快速的自行修补好该软件的漏洞,而不必一定要等到套件开发商提供修补的程序包哩!要知道,<font color="#000066">提早补洞</font>是很重要的一件事。<br> <br>这样说可以了解 Linux 的优点了吗?!没错!因为 <font color="#000066">Linux上面的套件几乎都是经过 GPL 的授权</font>,所以每个套件几乎均提供原始程序代码,并且您可以自行修改该程序代码,以符合您个人的需求呢!很棒吧!这就是开放源码(Open source )的优点啰!不过,到底什么是开放源码?这些程序代码是什么咚咚?又Linux 上面可以执行的相关套件档案与开放源码之间是如何转换的?不同版本的Linux 之间能不能使用同一个执行档?或者是该执行档需要由原始程序代码的部分重新进行转换?这些都是需要厘清观念的。底下我们先就原始程序代码与可执行档来进行说明。<br> <br><hr size="2" width="100%"><a name="intro_whatisopensource"></a><img src="0520source_code_and_tarball_files/penguin-s.gif" nosave="" align="middle" height="23" width="17"><font color="#000099">什么是开放源码、编译器与可执行档?</font><blockquote>在讨论程序代码是什么之前,我们先来谈论一下什么是可执行档?我们说过,在Linux 系统上面,一个档案能不能被执行看的是有没有可执行的那个权限( 具有x permission ),不过,<font color="#000066">Linux 系统上真正认识的可执行文件其实是二进制档案(binary file )</font>,例如 /usr/bin/passwd, /bin/touch 这些个档案即为 binary的可执行档案!或许您会说,咦! shell scripts 不是也可以执行吗?!其实 shellscripts 只是利用 shell ( 例如 bash )这支程序的功能进行一些判断式,而最终执行的除了bash 提供的功能外,仍是呼叫一些已经编译好的 binary 档案来执行的呢!( <i><font color="#000066">bash本身就是 binary file 喔!</font></i>)那么我怎么知道一个档案是否为 binary呢?!还记得我们在<a href="http://linux.vbird.org//linux_basic/0220filemanager.php#suid_sgid_sticky">Linux档案与目录管理</a> 里面提到的 file 这个指令的功能吗?!对啦!用他就是了!我们现在来测试一下:<br> <table bgcolor="#000000" border="1" cols="1" width="500"><tbody><tr><td><font size="-1" color="#cccccc" face="SimSun"># 先以系统的档案测试看看:</font><br><font size="-1" face="SimSun"><font color="#ffffff">[root@testroot]# </font><b><font color="#ffff00">file /bin/bash</font></b></font><br><font size="-1" color="#ffffff" face="SimSun">/bin/bash:ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux2.2.5, dynamically linked (uses shared libs), stripped</font><br><font size="-1" color="#ffffff" face="SimSun"> </font><br><font size="-1" color="#cccccc" face="SimSun"># 如果是我们在<a href="http://linux.vbird.org//linux_basic/0340bashshell-scripts.php#first_scripts">shellscript</a> 那个章节写的 test01-hello.sh 档案呢?</font><br><font size="-1" face="SimSun"><font color="#ffffff">[root@testroot]# </font><b><font color="#ffff00">file test01-hello.sh</font></b></font><br><font size="-1" color="#ffffff" face="SimSun">test01-hello.sh:Bourne-Again shell script text executable</font></td></tr></tbody></table><font color="#ff0000">  </font><br>看到了吧!如果是 binary file 而且是可以执行的时候,他就会显示执行文件类别( ELF 32-bit LSB executable ),同时会说明是否使用<b><font color="#000066">动态函式库(shared libs )</font></b>,而如果是一般的 script ,那他就会显示出 text executables之类的字样!(<i><font color="#000066">事实上,test01-hello.sh 的数据显示出Bourne-Again ... 那一行,是因为您的 scripts 上面第一行有宣告 #!/bin/bash的缘故,如果您将 script 的第一行拿掉,呵呵!那么不管 test01-hello.sh 的权限为何,他其实显示的是ASCII 文字文件的信息喔!</font></i>)<br> <br>既然 Linux 操作系统真正认识的其实是 binary file ,那么我们是如何做出这样的一支binary  的程序呢?!首先,我们必须要写程序,用什么东西写程序?就是一般的文书处理器啊!我都喜欢使用<a href="http://linux.vbird.org//linux_basic/0310vi.php">vi</a>来进行程序的撰写,写完的程序就是所谓的原始程序代码啰!<b><font color="#000066">这个程序代码档案其实就是一般的纯文字文件(text file )</font></b>。在完成这个原始码档案的编写之后,再来就是要将这个档案『<b><font color="#000066">编译</font></b>』成为操作系统看的懂得binary file 啰!而要编译自然就需要『<b><font color="#000066">编译器</font></b>』来动作,经过编译器的编译之后,就会产生一支可以执行的binary file 啰。<br> <br>举个例子来说,在 Linux 上面最标准的程序语言为 C ,所以我使用 C 的语法进行原始程序代码的书写,写完之后,以Linux 上标准的 C 语言编译器<b><font color="#000066">gcc</font></b> 这支程序来编译,就可以制作一支可以执行的binary file 啰。整个的流程有点像这样:<br> <center><a name="fig_1"></a><img src="0520source_code_and_tarball_files/source_to_binary.gif" nosave="" height="190" width="502"><br><font color="#000066">图一、简易的 gcc 编译流程</font></center> <br>事实上,在编译的过程当中,还会产生所谓的<b><font color="#000066">目标文件(Object file )</font></b>,这些档案是以 *.o 的附文件名样式存在的!至于 C 语言的原始码档案通常以*.c 作为附档名。此外,有的时候,我们会在程序当中『引用、呼叫』其它的外部子程序,或者是利用其它套件提供的『函数功能』,这个时候,我们就必须要在编译的过程当中,将该函式库给他加进去,如此一来,编译器就可以将所有的程序代码与函式库作一个连结(Link )以产生正确的执行档啰。</blockquote><hr width="100%"><a name="intro_library"></a><img src="0520source_code_and_tarball_files/penguin-s.gif" nosave="" align="middle" height="23" width="17"><font color="#000099">什么是函式库?</font><blockquote>在前一小节的<a href="#fig_1">图一</a>示意图中,在编译的过程里面有提到函式库。好啦,那么什么是函式库呢?先举个例子来说,我们的Linux 系统上,系统通常已经提供一个可以进行身份验证的模块,称为 PAM ,这个PAM 提供的功能可以让很多的程序在被执行的时候,除了可以验证使用者登入的信息外,还可以将身份确认的数据记录在登录文件(log file, 请查阅后续的 <a href="http://linux.vbird.org//linux_basic/0570syslog.php">认识登录档</a> 一文 )里面,以方便系统管理员的追踪!既然有这么好用的功能,那如果我要编写具有身份认证功能的程序时,直接引用该PAM 的功能就好啦,如此一来,我就不需要重新设计认证机制啰!也就是说,只要在我写的程序代码里面,设定去呼叫
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -