一、源文件结构概述

GNU组织提供的都是源代码,供用户自行编译使用。比如著名的apache web服务器就是典型的源文件:

 

wKiom1UCBCrB1vyYAAjB6_s8ZJA143.jpg 

 

我们可以下载这个源代码,在windows平台上通过firezillar上传到linux机器上:

wKioL1UCBWKTGTOAAAYnLUieYH8410.jpg 

 

 

[root@localhost ~]# cd /yum/bin_src/

[root@localhost bin_src]# ll

total 4940
-rw-r--r--. 1 root root 5054838 Aug 17 17:51 httpd-2.4.12.tar.bz2


如果该程序包不是通过filezillaftp工具上传的,而是直接从服务器上下载的,则为了确保本地的程序包能和服务器上的程序包时间一致,建议使用 ntpdate SERVER_IP将本地时间调整为服务器时间

[root@localhost bin_src]# date

Sun Aug 17 17:57:25 EDT 2014

[root@localhost bin_src]# ntpdate 192.168.56.103

17 Aug 18:00:32 ntpdate[24526]: no server suitable for synchronization found
# 由于本地还没有搭建成服务器,故无法同步,后文中将介绍服务器的搭建


[root@localhost bin_src]# tar xf httpd-2.4.12.tar.bz2 

展开该压缩包

[root@localhost bin_src]# ls

httpd-2.4.12  httpd-2.4.12.tar.bz2

[root@localhost bin_src]# cd httpd-2.4.12

[root@localhost httpd-2.4.12]# ls server/

buildmark.c      gen_test_char.dsp  provider.c        util_expr_parse.c    util_pcre.c
config.c         listen.c           request.c         util_expr_parse.h    util_regex.c
...
error_bucket.c   NWGNUmakefile      util_ebcdic.c     util_md5.c
gen_test_char.c  protocol.c         util_expr_eval.c  util_mutex.c


[root@localhost httpd-2.4.12]# cd server/

[root@localhost server]# cat buildmark.c 

# 可以看到以.c结尾的文件的内容,是c语言源代码
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 ...
 * limitations under the License.
 */
 
#include "ap_config.h"
#include "httpd.h"
 
#if defined(__DATE__) && defined(__TIME__)
static const char server_built[] = __DATE__ " " __TIME__;
#else
static const char server_built[] = "unknown";
#endif
 
AP_DECLARE(const char *) ap_get_server_built()
{
    return server_built;
}

 

.软件包的安装步骤

server目录中有*.c文件即为源程序,由于之前安装过包组Development tools, Server Platform Development Desktop Platform Development, 因此用户可以使用gcc来编译这些文件。但是要成功安装编译一个源程序,是不能简单使用gcc *.c的,因为这些*.c文件互相存在依赖关系,有一定的编译顺序,如果不按照顺序进行,则会出错。为了简化操作,出现了make工具:

[root@localhost server]# which make

/usr/bin/make
[root@localhost server]# man make
MAKE(1)                       LOCAL USER COMMANDS                      MAKE(1)
NAME
       make - GNU make utility to maintain groups of programs ——
 # make可以管理一组文件,即管理一个工程 
SYNOPSIS
       make [ -f makefile ] [ options ] ... [ targets ] ...


 make的出现大大简化了编译工作,但make的使用需要配合一个配置文件 MakefileMakefile指定了具体的编译方式,如编译的顺序,编译的优化方式等等。这个文件通常很难手动写出来,而是使用一个脚本configure,结合Makefile.in来生成Makefile文件:

[root@localhost httpd-2.4.12]# ls

ABOUT_APACHE     BuildAll.dsp    configure.in  InstallBin.dsp  NOTICE            server
acinclude.m4     BuildBin.dsp    docs          LAYOUT          NWGNUmakefile     srclib
Apache-apr2.dsw  buildconf       emacs-style   libhttpd.dsp    os                support
Apache.dsw       CHANGES         httpd.dsp     LICENSE         README            test
apache_probes.d  CMakeLists.txt  httpd.spec    Makefile.in     READMENaNake      VERSIONING
ap.d             config.layout   include       Makefile.win    README.platforms
build            configure       INSTALL       modules         ROADMAP

 

通常configure这个脚本也很难手动生成,而是软件包的作者使用autoconf这个工具,结合软件包的各种参数来自动生成的:

 

[root@localhost httpd-2.4.12]# rpm -qi autoconf

Name        : autoconf                     Relocations: (not relocatable)
...
Description :
GNU Autoconf is a tool for configuring source code and Makefiles.
Using Autoconf, programmers can create portable and configurable
packages, since the person building the package is allowed to
specify various configuration options.
# configure文件通常是由软件包的作

 

configure脚本需要结合Makefile.in来生成Makefile文件,而Makefile.in文件是使用automake这个工具来生成的:

 

root@localhost httpd-2.4.12]# rpm -qi automake

Name        : automake                     Relocations: (not relocatable)
...
Summary     : A GNU tool for automatically creating Makefiles
Description :
Automake is a tool for automatically generating `Makefile.in'
files compliant with the GNU Coding Standards.

 

综上,编译安装一个软件包的过程如下图:

wKiom1UCBFmyDudfAAF12lOOpvk089.jpg 

 

可能我们会有一个疑问,既然make最终使用的Makefile文件,那么为何不直接将配置写入到Makefile中,而要多此一举的生产configure文件呢?这是因为./configure有如下作用:

1)查编译环境是否完备; 2) 让用户定制编译配置(通过脚本选项,如使用--help来查看)

[root@localhost httpd-2.4.12]# ./configure --help

`configure' configures this package to adapt to many kinds of systems.
 
Usage: ./configure [OPTION]... [VAR=VALUE]...
 
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.
 
Defaults for the options are specified in brackets.
 
Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
 
Installation directories:
# 以下选项多为通用选项
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/apache2]
# 前缀,指定安装路径,以后如果要删除该程序,也可以指定该路径
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
 # 指定conf文件的目录,如果不指定,则自动创建为/usr/local/apache2/conf
  --mandir=DIR            man documentation [DATAROOTDIR/man]
# 指定man目录,如果不指定,则自动创建为/usr/local/apache2/man
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]
  ...
 --with-suexec-gidmin    Minimal allowed GID
  --with-suexec-logfile   Set the logfile
 ...
Report bugs to the package provider.