Linux下的源码包编译和安装原理

一、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 <stdio.h>

int main(int argc, char** argv)

{

    printf("Hello, Auto Makefile!\n");

    return 0;

}

------------------------------------------------

此时状态如下:

root@hufeihu:/home/hufeihu/project/main# pwd /home/hufeihu/project/main

root@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才可以。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值