一、 Linux 原始码包安装过程
用于 linux 原始码安装站群软件,一般下载原始码包得到档案:file.tar.gz 和 file.tar.bz2 格式
(1) 解压缩

解压命令为:
  tar jxvf file.tar.bz2
  tar zxvf file.tar.gz

(2) 配置档案

cd …/file
./configure    ……
(3) 编译档案
  make

(4) 安装档案
 
make install
(5) 删除一些临时档案

make clean
make clean:清除编译产生的可执行档案及目标档案 (object file,*.o)
make distclean:除了清除可执行档案和目标档案外,把 configure 所产生的 Makefile 也清除掉
make dist:将程式和相关的档案包装成一个压缩档案以供释出。执行完在目录下会产生一个以 PACKAGE-VERSION.tar.gz 为名称的档案。 PACKAGE 和 VERSION 这两个变数是根据 configure.in 档案中 AM_INIT_AUTOMAKE(PACKAGE,VERSION) 的定义。在此范例中会产生 test-1.0.tar.gz 的档案。
make distcheck:和 make dist 类似,但是加入检查包装后的压缩档案是否正常。这个目标除了把程式和相关档案包装成 tar.gz 档案外,还会自动把这个压缩档案解开,执行 configure,并且进行 make all 的动作,确认编译无误后,会显示这个 tar.gz 档案可供释出了。这个检查非常有用,检查过关的包,基本上可以给任何一个具备 GNU 开发环境-的人去重新编译

(5) 解除安装档案
make uninstall

二、编译和执行一个简单的 main.c 档案
编译和执行一个简单的 c 语言原始档 main.c, 需要自动生成一个 makefile,
以下是步骤:
第一步:
建立一个档案 main.c 在一定的目录/home/hufeihu/project/main 下面:

————————————————
在 main.c 档案中写入一下的语句

#include
int main(int argc, char** argv)
{
printf(“Hello, Auto Makefile!n”);
return 0;
}
————————————————
此时状态如下:
root@hufeihu:/home/hufeihu/project/main# pwd
/home/hufeihu/project/mainroot@hufeihu:/home/hufeihu/project/main# ls
main.c
root@hufeihu:/home/hufeihu/project/main#
第二步:
———-
执行 autoscan , 自动建立两个档案: autoscan.log configure.scan
此时状态如下:

root@hufeihu:/home/hufeihu/project/main# autoscan
root@hufeihu:/home/hufeihu/project/main# ls
autoscan.log  configure.scan  main.c
root@hufeihu:/home/hufeihu/project/main#
第三步:
———-
修改 configure.scan 的档名为 configure.in

检视 configure.in 的内容:

————————————————
# -*-Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

 
————————————————
解读以上的档案:
————————————————
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
# AC_PREREQ:
# 确保使用的是足够新的 Autoconf 版本。如果用于建立 configure 的 Autoconf 的版
# 本比 version 要早,就在标准错误输出列印一条错误讯息并不会建立 configure 。
AC_PREREQ(2.69)
#
# 初始化, 定义站群软件的基本资讯, 包括设定包的全称, 版本号以及报告 BUG 时需要用的邮箱地址
#
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
#
# 用来侦测所指定的原始码档案是否存在,来确定原始码目录的有效性
#
AC_CONFIG_SRCDIR([main.c])
#
# 用于生成 config.h 档案,以便 autoheader 使用
#
AC_CONFIG_HEADER([config.h])
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
#
# 建立输出档案。在`configure.in’ 的末尾呼叫本巨集一次。
#
AC_OUTPUT
————————————————
修改动作:
1. 修改 AC_INIT 里面的引数: AC_INIT(main,1.0, hufeihu@163.com)
2. 新增巨集 AM_INIT_AUTOMAKE, 它是 automake 所必备的巨集,也同前面一样,PACKAGE 是所要产生站群软件套件的名称,VERSION 是版本编号。
3. 在 AC_OUTPUT 后新增输出档案 Makefile
修改后的结果:

————————————————
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT(main, 1.0, hufeihu@163.com)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(main,1.0)
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT([Makefile])
————————————————
第四步:
执行 aclocal, 生成一个 “aclocal.m4” 档案和一个缓冲资料夹 autom4te.cache,该档案主要处理本地的巨集定义。
此时的状态是:
root@hufeihu:/home/hufeihu/project/main# aclocal
aclocal: warning: autoconf input should be named ‘configure.ac’, not ‘configure.in’
root@hufeihu:/home/hufeihu/project/main# ls
aclocal.m4  autom4te.cache  autoscan.log  configure.in  main.c
root@hufeihu:/home/hufeihu/project/main#

第五步:
执行 autoconf, 目的是生成 configure
此时的状态是:
root@hufeihu:/home/hufeihu/project/main# autoconf
root@hufeihu:/home/hufeihu/project/main# ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.in  main.c
root@hufeihu:/home/hufeihu/project/main#

第六步:
执行 autoheader,它负责生成 config.h.in 档案。该工具通常会从 “acconfig.h” 档案中复制使用者附加的符号定义,因此此处没有附加符号定义,所以不需要建立 “acconfig.h” 档案。
此时的状态是:
root@hufeihu:/home/hufeihu/project/main#  autoheader
root@hufeihu:/home/hufeihu/project/main# ls
aclocal.m4      autoscan.log  configure     main.c
autom4te.cache  config.h.in   configure.in
root@hufeihu:/home/hufeihu/project/main#

第七步:
下面即将执行 automake, 但在此之前应该做一下准备工作!
首先
建立一个 Makefile.am. 这一步是建立 Makefile 很重要的一步,automake 要用的指令码配置档案是 Makefile.am,使用者需要自己建立相应的档案。之后,automake 工具转换成 Makefile.in 。
这个 Makefile.am 的内容如下:

————————————————
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=main.c
————————————————
下面对该指令码档案的对应项进行解释。
其中的 AUTOMAKE_OPTIONS 为设定 automake 的选项。由于 GNU(在第 1 章中已经有所介绍)对自己释出的站群软件有严格的规范,比如必须附 带许可证宣告档案 COPYING 等,否则 automake 执行时会报错。 automake 提供了三种站群软件等级:foreign 、 gnu 和 gnits,让用 户选择采用,预设等级为 gnu 。在本例使用 foreign 等级,它只检测必须的档案。
bin_PROGRAMS 定义要产生的执行档名。如果要产生多个执行档案,每个档名用空格隔开。
main_SOURCES 定义 “main” 这个执行程式所需要的原始档案。如果”main” 这个程式是由多个原始档案所产生的,则必须把它所用到的所有原 始档案都列出来,并用空格隔开。例如:若目标体 “main” 需要 “main.c” 、 “sunq.c” 、 “main.h” 三个依赖档案,则定义 main_SOURCES=main.c sunq.c main.h 。要注意的是,如果要定义多个执行档案,则对每个执行程式都要定义相应的 file_SOURCES 。
其次
使用 automake 对其生成 “configure.in” 档案,在这里使用选项 “—adding-missing” 可以让 automake 自动新增有一些必需的指令码档案。
执行后的状态是:
————————————————
root@hufeihu:/home/hufeihu/project/main# automake –add-missing
configure.in:8: installing `./missing’
configure.in:8: installing `./install-sh’
Makefile.am: installing `./depcomp’
root@hufeihu:/home/hufeihu/project/main# ls
aclocal.m4      config.h.in   configure.in~ main.c        Makefile.in
autom4te.cache configure     depcomp        Makefile.am missing
autoscan.log    configure.in install-sh     Makefile.am~
root@hufeihu:/home/hufeihu/project/main#
————————————————
第八步
执行 configure,在这一步中,通过执行自动配置设定档案 configure,把 Makefile.in 变成了最终的 Makefile 。
执行的结果如下:

————————————————
root@hufeihu:/home/hufeihu/project/main# ./configure
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for a thread-safe mkdir -p… /bin/mkdir -p
checking for gawk… gawk
checking whether make sets $(MAKE)… yes
checking whether make supports nested variables… yes
checking for gcc… gcc
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether gcc accepts -g… yes
checking for gcc option to accept ISO C89… none needed
checking whether gcc understands -c and -o together… yes
checking for style of include used by make… GNU
checking dependency style of gcc… gcc3
checking that generated files are newer than configure… done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
root@hufeihu:/home/hufeihu/project/main#
root@hufeihu:/home/hufeihu/project/main# ls
aclocal.m4      config.h.in    configure.in   main.c        Makefile.in
autom4te.cache config.log     configure.in~ Makefile     missing
autoscan.log    config.status depcomp        Makefile.am  stamp-h1
config.h        configure      install-sh     Makefile.am~
root@hufeihu:/home/hufeihu/project/main#
————————————————
第九步
执行 make,对配置档案 Makefile 进行测试一下
此时的状态如下:

————————————————
root@hufeihu:/home/hufeihu/project/main# make
make  all-am
make[1]: Entering directory ‘/home/hufeihu/project/main’
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc  -g -O2   -o main main.o  
make[1]: Leaving directory ‘/home/hufeihu/project/main’
root@hufeihu:/home/hufeihu/project/main#

root@hufeihu:/home/hufeihu/project/main#  ls
aclocal.m4      autoscan.log config.h.in config.status configure.in   depcomp    main    main.o    Makefile.am   Makefile.in stamp-h1
autom4te.cache config.h      config.log   configure      configure.in~ install-sh main.c Makefile Makefile.am~ missing
[root@localhost main]#
————————————————
第十步
执行生成的档案 main:
————————————————
root@hufeihu:/home/hufeihu/project/main#  ./main
Hello, Auto Makefile!
总结一下,使用 automake 自动生成 makefile 的过程主要有
1 、建立好原始档以后到原始档所在目录
2 、 autoscan 命令 将 configure.scan 档案修改为 configure.in
          修改 configure.in 档案中的内容:
               AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS) 修改为 AC_INIT(main, 1.0,hufeihu@163.com)
        在 AC_CONFIG_HEADER([config.h]) 后面新增 AM_INIT_AUTOMAKE(main,1.0)
          在最后新增 AC_OUTPUT([Makefile])
3 、执行 aclocal
4 、执行 autoconf
5 、执行 autoheader
6 、建立 Makefile.am 档案,内容为
     AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main 如果有多个用空格分开
     main_SOURCES=main.c 定义 main 所需原始档,多个可执行档案分别定义
7 、执行 automake –add-missing
8 、执行./configure
9 、执行 make

在第六步中需要自己写 Makefile.am 档案,特别是其中的 main_SOURCES 需要把生成 main 所以来的档案都包含进来。并且那些间接依赖的档案也需要包含进来。比如说我有三个档案:main.cpp Add.cpp Add.h  Num.h Num.cpp 其中在 main.cpp 中包含了 Add.h  在 Add.cpp 中包含了 Num.h 这样在完成 main 的依赖档案时就需要包含以上所有的问个档案 main.cpp Add.cpp Add.h  Num.h Num.cpp 才可以。