zoukankan      html  css  js  c++  java
  • Linux下使用autoconf 和 automake 编译简单的HelloWorld

    使用过开源C/C++项目的同学都知道,标准的编译过程已经变成简单的三部曲:./configure /make/make install,使用起来很方便,不像平时自己写代码,要手写一堆复杂的makefile,而且换个编译环境,makefile还需修改。因此,本文将介绍如何使用 autoconf 和

    automake 两个工具来帮助我们自动地生成符合自由软件惯例的makefile了。

    一.执行步骤

     

    1.建立目录

    在自己的工作目录下建立一个helloworld目录,用来存放helloworld程序及相关文件

    $ mkdir helloworld

    $ cd helloworld

    2.编写helloworld.c

    $ vim helloworld.c

     

    #include <stdio.h>

    int main (int argc, char **argv)
    {
        printf("Hello Linux World! ");
        return 0;
    }

    $ ls

    helloworld.c

    3.运行autoscan 生成configure.scan文件

    使用autoscan 命令来帮助我们根据目录下的源代码生成一个 configure.in的模板文件

    $ autoscan

    $ ls

    autom4te.cache  autoscan.log  configure.scan  helloworld.c

    4.该写成configure.in(后来用.ac 后缀)

    将configure.scan该名为configure.in ,并且如下修改。

    $ mv configure.scan configure.in

    $ vim configure.in

     

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

    AC_INIT(helloworld.c)
    AM_INIT_AUTOMAKE(helloworld,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)

    $ ls

    autom4te.cache  autoscan.log  configure.in  helloworld.c

    5.运行aclocal 生成aclocal.m4 文件

    $ aclocal

    $ ls

    aclocal.m4  autom4te.cache  autoscan.log  configure.in  helloworld.c

    6.运行autoconf 生成 configure 文件

    $ autoconf

    $ ls

    aclocal.m4  autom4te.cache  autoscan.log  configure  configure.in  helloworld.c

    7.新建 Makefile.am

    $ vim Makefile.am


    AUTOMAKE_OPTIONS=foreign
    bin_PROGRAMS=helloworld
    helloworld_SOURCES=helloworld.c

    8.运行automake 生成 Makefile.in

    $ automake --add-missing

    configure.in:5: installing `./install-sh'
    configure.in:5: installing `./missing'
    Makefile.am: installing `./depcomp'

    $ ls

    aclocal.m4      autoscan.log  configure.in  helloworld.c  Makefile.am  missing
    autom4te.cache  configure     depcomp       install-sh    Makefile.in

    9.执行 configure 生成 Makefile

    $ ./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 for gcc... gcc
    checking for C compiler default output file name... a.out
    checking whether the C compiler works... yes
    checking whether we are cross compiling... no
    checking for suffix of executables...
    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 for style of include used by make... GNU
    checking dependency style of gcc... gcc3
    configure: creating ./config.status
    config.status: creating Makefile
    config.status: executing depfiles commands

    $ ls

    aclocal.m4      autoscan.log  config.status  configure.in  helloworld.c  Makefile     Makefile.in
    autom4te.cache  config.log    configure      depcomp       install-sh    Makefile.am  missing

    10.使用make命令编译源文件

    $ make

    gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="helloworld" -DVERSION="1.0" -I.     -g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo -c -o helloworld.o helloworld.c
    mv -f .deps/helloworld.Tpo .deps/helloworld.Po
    gcc  -g -O2   -o helloworld helloworld.o 

    $ ls

    aclocal.m4      autoscan.log  config.status  configure.in  helloworld    helloworld.o  Makefile     Makefile.in
    autom4te.cache  config.log    configure      depcomp       helloworld.c  install-sh    Makefile.am  missing

    11.运行 helloworld

    $ ./helloworld

    Hello Linux World!

    $ make clean

    test -z "helloworld" || rm -f helloworld
    rm -f *.o

    二.深入浅出

    1. autoscan

          autoscan 是用来扫描源代码目录,生成configure.scan 文件,该文件包含了由宏定义的系统配置的基本选项。

    2. aclocal

          aclocal 是一个perl 脚本程序,它根据 configure.in 文件的内容,自动生成 aclocal.m4 文件。

    3. autoconf

          autoconf 能根据configure.in 和aclocal.m4 产生configure 文件。configure 是一个脚本,它能设置源程序来适应各种不同的操作系统平台,并且根据不同的系统来产生合适的Makefile,从而可以使你的源代码能在不同的操作系统平台被编译出来。

          configure.in 文件的内容是一些宏,这些宏经过autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数 的shell 脚本--

    configure 。configure.in 文件中的宏的顺序并没有规定,但必须在所有宏的最前面和最后面分别加上AC_INIT 宏和AC_OUTPUT 宏。

          在configure.in 中:

    #    表示注释,这个宏后面的内容将被忽略

    AC_INIT(FILE)    这个宏用来检查源代码所在的路径。

    AM_INIT_AUTOMAKE(PACKAGE, VERSION)   这个宏描述了我们将要生成的软件包的名字及其版本号:PACKAGE 是软件包的名字,VERSION 是版本号。当你使用make dist 命令时,它会生成一个类似 helloworld-1.0.tar.gz 的软件发行包,其中就对应了软件包的名字和版本号。

    AC_PROG_CC   这个宏检查系统所用的C编译器

    AC_OUTPUT(FILE)   这个宏是我们要输出的Makefile 的名字

          其实,我们使用automake 时,实际上还需要用到其他的一些宏,但我们可以用aclocal 来帮我们产生,即aclocal.m4 文件。

    4.Makefile.am

          Makefile.am 是用来生成 Makefile.in 的,需要手工书写。Makefile.am 定义了如下内容:

    AUTOMAKE_OPTIONS   这个是automake 的选项。在执行automake 时,它会检查目录下是否存在标准GNU 软件包中应具备的各种文件,例如 AUTHORS、ChangeLog、NEWS等文件。我们将其设置成foreign 时,automake 会改用一般软件包的标准来检查。

    bin_PROGRAMS    这个是指定我们所要产生的可执行文件的文件名。

    helloworld_SOURCES   这个是指定产生“helloworld”时所需要的源代码。如果用到了多个源文件,可用空格符号将其隔开。如果在bin_PROGRAMS 中定义了多个可执行文件,则对应每个可执行文件都要定义相应的filename_SOURCES。

    5.automake

          automake 可以根据configure.in 和Makefile.am 来产生 Makefile.in 。

    选项 --add-missing 的定义是”add missing standard files to package“,它会让automake 加入一个标准的软件包所必须的一些文件。

    6.Makefile

    make    根据Makefile 编译源代码,链接生成目标文件和可执行文件。

    make clean   清除上次make 命令所产生的object 文件及可执行文件

    make distclean   类似make clean,但同时也将configure 生成的文件全部删除掉,包括Makefile。

    make install   将编译成功的可执行文件安装到系统目录中,一般为 /usr/local/bin 目录。

    make dist   产生发布软件包文件(distribution package)。

    make distcheck   生成发布软件包并对其进行测试检查,以确定发布包的正确性。这个操作将自动把压缩包文件解压,然后执行configure 命令,并且执行make ,来确认编译不出现错误,最后提示你的软件包已经准备好了,可以发布了。

    三.过程图示

  • 相关阅读:
    Mysql mysql lost connection to server during query 问题解决方法
    【转】程序员必须知道的几个Git代码托管平台
    FIFO存储器
    JUCE_FIFO实现分析
    【转】HashMap实现原理分析
    【转】int && 非常量右值
    【API】注册表编程基础-RegCreateKeyEx、RegSetValueEx
    【API】文件操作编程基础-CreateFile、WriteFile、SetFilePointer
    【逆向知识】裸函数(Naked函数)
    【逆向知识】堆栈图-汇编中的函数
  • 原文地址:https://www.cnblogs.com/zhanbiqiang/p/4109316.html
Copyright © 2011-2022 走看看