linux --> Autoconf和Automake使用

 

   

1、生成Makefile流程图

 

2、具体实例

  执行命令顺序:autoscan; aclocal; autoconf; autoheader; automake --add-missing; ./configure; make; ./helloworld;javascript

一、建目录

  在你的工做目录下建一个helloworld目录,用来存放helloworld程序及相关文件,如在/home/my/build下:html

$ mkdir helloword
$ cd helloworld

 

二、 helloworld.c

  编辑文件,完成后保存退出。如今在helloworld目录下就应该有一个你本身写的helloworld.c了。前端

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

 

三、生成configure

  $ autoscan
  $ ls
  configure.scan helloworld.c 

执行后在hellowrold目录下会生成一个文件:configure.scan,能够拿它做为configure.ac的蓝本。java

 

四、生成configure.ac

   如今将configure.scan更名为configure.ac,而且编辑它,按下面的内容修改,去掉无关的语句:
复制代码
   #                                               -*- 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_INIT(HelloWorld, 1.0, jiangtengfei007@163.com) AM_INIT_AUTOMAKE(HelloWorld, 1.0) #这句话很重要,不然下面aclocal出错 AC_CONFIG_SRCDIR([HelloWorld.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_CONFIG_FILES([Makefile]) AC_OUTPUT
复制代码

 

五、执行aclocal和autoconf

  而后执行命令aclocal和autoconf,分别会产生aclocal.m4及configure两个文件:shell

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

  能够看到configure.ac内容是一些宏定义,这些宏经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。app

  autoconf 是用来生成自动配置软件源代码脚本(configure)的工具。configure脚本能独立于autoconf运行,且在运行的过程当中,不须要用户的干预。函数

  要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4。工具

  aclocal根据configure.ac文件的内容,自动生成aclocal.m4文件。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。post

  autoconf从configure.ac这个列举编译软件时所须要各类参数的模板文件中建立configure。测试

  autoconf须要GNU m4宏处理器来处理aclocal.m4,生成configure脚本。

  m4是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏能够是内嵌的,也能够是用户定义的。除了能够展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操做,循环等。m4既能够做为编译器的前端,也能够单独做为一个宏处理器.

 
 

六、执行autoheader

  在执行automake --add-missing以前执行autoheade,  生成config.h.in
 
 

七、新建Makefile.am

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=helloworld helloworld_SOURCES=helloworld.c 

  automake会根据你写的Makefile.am来自动生成Makefile.in。Makefile.am中定义的宏和目标,会指导automake生成指定的代码。例如,宏bin_PROGRAMS将致使编译和链接的目标被生成。

 

八、运行automake

复制代码
➜  AutoConf_ automake --add-missing
configure.ac:7: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see: configure.ac:7: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation configure.ac:12: installing './compile' configure.ac:7: installing './install-sh' configure.ac:7: installing './missing' Makefile.am: installing './depcomp'
复制代码

  automake会根据Makefile.am文件产生一些文件,包含最重要的Makefile.in。

 

九、执行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... ./install-sh -c -d checking for gawk... no checking for mawk... no checking for nawk... no checking for awk... awk 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 
➜  ls -l Makefile -rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile 
复制代码

你能够看到,此时Makefile已经产生出来了。

 

十、使用Makefile编译代码

➜  make
/Applications/Xcode.app/Contents/Developer/usr/bin/make  all-am gcc -DHAVE_CONFIG_H -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

➜  ./helloworld
Hello World!!!!

  这样helloworld就编译出来了,你若是按上面的步骤来作的话,应该也会很容易地编译出正确的helloworld文件。你还能够试着使用一些其余的make命令,如make clean,make install,make dist,看看它们会给你什么样的效果。感受如何?本身也能写出这么专业的Makefile,老板必定会对你另眼相看。

 

3、命令详解 

一、 autoscan

  autoscan是用来扫描源代码目录生成configure.scan文件的。autoscan能够用目录名作为参数,但若是你不使用参数的话,那么autoscan将认为使用的是当前目录。autoscan将扫描你所指定目录中的源文件,并建立configure.scan文件。

 

二、 configure.scan

  configure.scan包含了系统配置的基本选项,里面都是一些宏定义。咱们须要将它更名为configure.ac

 

三、 aclocal

  aclocal是一个perl 脚本程序。aclocal根据configure.ac文件的内容,自动生成aclocal.m4文件。aclocal的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。

 

四、 autoconf

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

  configure.ac文件的内容是一些宏,这些宏通过autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。configure.ac文件中的宏的顺序并无规定,可是你必须在全部宏的最前面和最后面分别加上AC_INIT宏和AC_OUTPUT宏。

  在configure.ini中:

复制代码
  #号表示注释,这个宏后面的内容将被忽略。

  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后咱们会获得aclocal.m4文件。   产生了configure.ac和aclocal.m4 两个宏文件后,咱们就可使用autoconf来产生configure文件了。

 

五、 Makefile.am

  Makefile.am是用来生成Makefile.in的,须要你手工书写。Makefile.am中定义了一些内容:

  AUTOMAKE_OPTIONS  //在执行automake时,它会检查目录下是否存在标准GNU软件包中应具有的各类文件,例如AUTHORS、ChangeLog、NEWS等文件。咱们将其设置成foreign时,automake会改用通常软件包的标准来检查。   bin_PROGRAMS  //这个是指定咱们所要产生的可执行文件的文件名。若是你要产生多个可执行文件,那么在各个名字间用空格隔开。   helloworld_SOURCES  //这个是指定产生“helloworld”时所须要的源代码。若是它用到了多个源文件,那么请使用空格符号将它们隔开。好比须要helloworld.h,helloworld.c那么请写成helloworld_SOURCES= helloworld.h helloworld.c。

  若是你在bin_PROGRAMS定义了多个可执行文件,则对应每一个可执行文件都要定义相对的filename_SOURCES。

 

六、 automake

  使用automake,根据configure.in和Makefile.am来产生Makefile.in。

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

  用automake产生出来的Makefile.in文件是符合GNU Makefile惯例的,接下来咱们只要执行configure这个shell 脚本就能够产生合适的 Makefile 文件了。

  

七、 Makefile

  在符合GNU Makefiel惯例的Makefile中,包含了一些基本的预先定义的操做:

复制代码
  make      //根据Makefile编译源代码,链接,生成目标文件,可执行文件。   make clean  //清除上次的make命令所产生的object文件(后缀为“.o”的文件)及可执行文件。   make install  //将编译成功的可执行文件安装到系统目录中,通常为/usr/local/bin目录。   make list  //产生发布软件包文件(即distribution package)。这个命令会将可执行文件及相关文件打包成一个tar.gz压缩的文件用来做为发布软件的软件包。它会在当前目录下生成一个名字相似“PACKAGE-VERSION.tar.gz”的文件。PACKAGE和VERSION,是咱们在configure.ac中定义的AM_INIT_AUTOMAKE(PACKAGE, VERSION)。   make distcheck  //生成发布软件包并对其进行测试检查,以肯定发布包的正确性。这个操做将自动把压缩包文件解开,而后执行configure命令,而且执行make,来确认编译不出现错误,最后提示你软件包已经准备好,能够发布了。   make distclean  //相似make clean,但同时也将configure生成的文件所有删除掉,包括Makefile。
相关文章
相关标签/搜索