autoMake工具使用实例(Hello, World!)

定义

Makefile的 基本结构不是 很复杂,但当一个程序开发人员开始写Makefile时,经常会怀疑自己写的 是 否符合惯例,而且自己写的 Makefile经常和自己的 开发环境相关联,当系统环境变量或路径发生了变化后,Makefile可能还要跟着修改.这样就造成了手工书写Makefile的 诸多问题,automake恰好能很好地帮助我们解决这些问题.
  
使用automake,程序开发人员只需要写一些简单的 含有预定义宏的 文件,由autoconf根据一个宏文件生成configure,由automake根据另一个宏文件生成Makefile.in,再使用configure依据Makefile.in来生成一个符合惯例的 Makefile.下面我们将详细介绍Makefile的 automake生成方法.

使用automake命令执行顺序

aclocal; 
autoconf; 
automake --add-missing; 
./configure; 
make; 
./helloworld

当然事先要先安装相关的软件库。这里不再多说。
下面用一个例子来说明autoMake使用方法。

关于hello,world.

首先创建个hello, world的代码:
文件名helloworld.c

#include 

int main()
{
    printf("hello, world!\n");
    return 0;
}

执行

$ autoscan
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/\${ <-- HERE [^\}]*}/ at /usr/bin/autoscan line 361.
$ ls
autoscan.log  configure.scan  helloworld.c

将configure.scan复制为configure.ac,并修改其中的内容为:

$ ls
autoscan.log  configure.ac  configure.scan  helloworld.c
$ cat configure.ac 
# -*- 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)

执行aclocal生成aclocal.m4文件:

$ aclocal
$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  configure.scan  helloworld.c

执行autoconf生成configure文件:

$ autoconf
$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  configure.scan  helloworld.c

然后开始创建Makefile.am文件:

$ cat Makefile.am 
UTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c

有了Makefile.am文件,我们就可以使用automake了:

$ $ automake --add-missing
configure.ac:4: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:4: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation

$ ls
aclocal.m4      autoscan.log  configure     configure.scan  depcomp       INSTALL     Makefile.am
autom4te.cache  compile       configure.ac  COPYING         helloworld.c  install-sh  missing

然后在执行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... no
checking for mawk... mawk
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: executing depfiles commands
$ ls
aclocal.m4      compile        configure       COPYING       INSTALL     Makefile.am
autom4te.cache  config.log     configure.ac    depcomp       install-sh  Makefile.in
autoscan.log    config.status  configure.scan  helloworld.c  Makefile    missing

有了Makefile,就可以直接make了:

$ make
gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -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  
$ ./helloworld 
hello, world!

不得不说,这个工具的强大,当然也生成了许多的文件。不过还是很好的。

你可能感兴趣的:(autoMake工具使用实例(Hello, World!))