autotools使用

关于Autotools的使用


我们前面的章节中已经讲到了Makefile的使用(点击进入查看文章)。我们知道在Linux下面如果编译一个比较大型的项目,我们可以通过Makefile的方式来完成。

但是,我们又蛋疼了,Makefile拥有复杂的语法结构,甚至让人难以领会,当我们项目非常大的时候,维护Makefile会成为一件非常头疼的事情。于是我们就有了autotools工具,专门用来生成Makefile,这个工具让我们很大程度的降低了开发的难度。

Autotools并不是一个工具,而是一系列工具:

1. autoscan

2. aclocal

3. autoconf

4. autoheader

5. automake

记住,这一系列工具看着复杂,最终的目标还是生成Makefile


一般情况下系统中都会默认安装这一系列工具,如果未安装,则在Centeros中可以通过下面命令安装:

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo yum install automake  


c源文件同一目录下Autotools的使用

如果你的源文件都放在同一个目录下面,那么使用Autotools的时候会相对简单很多。比较著名的开源软件Memcache也是放在同一目录下的,你可以去看下它的源码包。

下面会按照步骤来实现同一目录下的Autotools工具的使用。


1. 源代码例子

入口文件main.c

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>   #include <stdlib.h>   #include <unistd.h>   #include "sum.h"   #include "get.h"   //入口主函数   int int int int , z);    
  2. , z);    
  3. return }   

 

sum.h和sum.c

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h> #include <stdlib.h> #include <unistd.h> intintint *y);  
[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include "sum.h" #include "val.h" intintint return }  

val.h和val.c

 

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h> #include <stdlib.h> #include <unistd.h> intint *x);  
[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include "val.h" intint return }  

get.h和get.c

 

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h> #include <stdlib.h> #include <unistd.h> intintint *y);  
[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include "get.h" intintint return }  

上面这个例子,我们在Makefile这篇文章中已经讲解过如何来手工编写Makefile编译。这边的话我们继续使用这个例子,实现Autotools的工具编译。

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [admin@localhost test_c2]$ ls  
  2. get.c  get.h  main.c  sum.c  sum.h  val.c  val.h  

 


2. Autoscan命令

第一步,我们需要在我们的项目目录下执行autoscan命令。这个命令主要用于扫描工作目录,并且生成configure.scan文件。并且configure.scan需要重命令成configure.ac,然后编辑这个配置,我们才能继续执行后面的命令。

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [admin@localhost test_c2]$ autoscan  
  2. autoscan.log  configure.ac  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h  

我们需要编辑configure.ac文件,首先我们打开configure.ac文件:

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #                                               -*- Autoconf -*-  
  2. AC_OUTPUT  

我们修改成:

 

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #                                               -*- Autoconf -*-  
  2. AC_OUTPUT  

configure.ac标签说明:


标签

说明

AC_PREREQ

声明autoconf要求的版本号

AC_INIT

定义软件名称、版本号、联系方式

AM_INIT_AUTOMAKE

必须要的,参数为软件名称和版本号

AC_CONFIG_SCRDIR

宏用来侦测所指定的源码文件是否存在, 来确定源码目录的有效性.。此处为当前目录下main.c。

AC_CONFIG_HEADER

宏用于生成config.h文件,以便 autoheader 命令使用。

AC_PROG_CC

指定编译器,默认GCC

AC_CONFIG_FILES

生成相应的Makefile文件,不同文件夹下的Makefile通过空格分隔。例如:AC_CONFIG_FILES([Makefile, src/Makefile]) 

AC_OUTPUT

用来设定 configure 所要产生的文件,如果是makefile,configure 会把它检查出来的结果带入makefile.in文件产生合适的makefile。

 

 

3. Aclocal命令

第二步,执行aclocal命 令。扫描 configure.ac 文件生成 aclocal.m4文件, 该文件主要处理本地的宏定义,它根据已经安装的宏、用户定义宏和 acinclude.m4 文件中的宏将 configure.ac 文件需要的宏集中定义到文件 aclocal.m4 中。

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [admin@localhost test_c2]$ aclocal  
  2. aclocal.m4  autom4te.cache  autoscan.log  configure.ac  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h  

 


4. Autoconf命令

第三步,执行autoconf命令。这个命令将 configure.ac 文件中的宏展开,生成 configure 脚本。这个过程可能要用到aclocal.m4中定义的宏。

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [admin@localhost test_c2]$ autoconf  
  2. [admin@localhost test_c2]$   


 

5. Autoheader命令

第四步,执行autoheader命令。该命令生成 config.h.in 文件。该命令通常会从 "acconfig.h” 文件中复制用户附加的符号定义。该例子中没有附加的符号定义, 所以不需要创建 "acconfig.h” 文件。

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [admin@localhost test_c2]$ autoheader  
  2. [admin@localhost test_c2]$   


 

6. 创建Makefile.am文件

第五步,创建Makefile.am文件。Automake工具会根据 configure.in 中的参量把 Makefile.am 转换成 Makefile.in 文件。最终通过Makefile.in生成Makefile文件,所以Makefile.am这个文件非常重要,定义了一些生成Makefile的规则

Makefile.am:

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. AUTOMARK_OPTIONS = foreign  
  2. hello_SOURCES = main.c val.h val.c get.h get.c sum.h sum.c  

 

1. AUTOMAKE_OPTIONS:由于GNU对自己发布的软件有严格的规范, 比如必须附带许可证声明文件COPYING等,否则automake执行时会报错. automake提供了3中软件等级:foreign, gnu和gnits, 供用户选择。默认级别是gnu. 在本例中, 使用了foreign等级, 它只检测必须的文件。

2. bin_PROGRAMS = hello :生成的可执行文件名称,生成多个可执行文件,可以用空格隔开。

3. hello_SOURCES:生成可执行文件hello需要依赖的源文件。其中hello_为可执行文件的名称。

具体Makefile.am后面我们会有一个章节专门讲这块内容。


7. Automake命令

第六步,执行automake --add-missing命令。该命令生成 Makefile.in 文件。使用选项 "--add-missing" 可以让 Automake 自动添加一些必需的脚本文件。如果发现一些文件不存在,可以通过手工 touch命令创建。

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [admin@localhost test_c2]$ automake --add-missing  
  2. AUTHORS     autoscan.log    config.h.in  configure.ac  depcomp  get.h  install-sh  Makefile.am  missing      README  sum.h  val.h  


8. configure命令

第七步,估计大家都对 ./congigure这个命令很熟悉吧。大部分linux软件安装都先需要执行./congigure,然后执行make和make install命令。

./congigure主要把 Makefile.in 变成最终的 Makefile 文件。configure会把一些配置参数配置到Makefile文件里面。

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. ./configure  
  2. README  sum.c     val.c  


9. make命令

第八步,执行make命令,执行make命令后,就生成了可执行文件hello

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [admin@localhost test_c2]$ make  
  2. Z:400  
posted @ 2020-03-18 17:32  周乐磊  阅读(348)  评论(0)    收藏  举报