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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. sudo yum install automake  


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

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

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


1. 源代码例子

入口文件main.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>    
  2. #include <stdlib.h>    
  3. #include <unistd.h>    
  4. #include "sum.h"    
  5. #include "get.h"    
  6.     
  7.     
  8. //入口主函数    
  9. int main() {    
  10.     int x = 10;    
  11.     int y = 20;    
  12.     int z = sum(&x, &y);    
  13.     puts("This is Main");    
  14.     printf("Z:%d\n", z);    
  15.     x = 20;    
  16.     z = get(&x, &y);    
  17.     printf("Z:%d\n", z);    
  18.     return 1;    
  19. }   

sum.h和sum.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4.   
  5. int sum(int *x, int *y);  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "sum.h"  
  2. #include "val.h"  
  3.   
  4. int sum(int *x, int *y) {  
  5.     val(x);  
  6.     puts("This is SUM Method!=========HDH");  
  7.     return *x + *y;  
  8. }  
val.h和val.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4.   
  5. int val(int *x);  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "val.h"  
  2.   
  3. int val(int *x) {  
  4.     puts("This is Value==");  
  5.     printf("X:%d \n", *x);  
  6.     return 0;  
  7. }  
get.h和get.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4.   
  5. int get(int *x, int *y);  
[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "get.h"  
  2.   
  3. int get(int *x, int *y) {  
  4.     puts("This is get");  
  5.     return (*x) * (*y);  
  6. }  

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

[plain]  view plain  copy
 print ? 在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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c2]$ autoscan  
  2. [admin@localhost test_c2]$ ls  
  3. autoscan.log  configure.scan  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h  
  4. [admin@localhost test_c2]$ mv configure.scan configure.ac  
  5. [admin@localhost test_c2]$ ls  
  6. 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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.   
  4. AC_PREREQ([2.69])  
  5. AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])  
  6. AC_CONFIG_SRCDIR([main.c])  
  7. #                                               -*- Autoconf -*-  
  8. # Process this file with autoconf to produce a configure script.  
  9.   
  10. AC_PREREQ([2.69])  
  11. AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])  
  12. AC_CONFIG_SRCDIR([main.c])  
  13. AC_CONFIG_HEADERS([config.h])  
  14.   
  15. # Checks for programs.  
  16. AC_PROG_CC  
  17.   
  18. # Checks for libraries.  
  19.   
  20. # Checks for header files.  
  21. AC_CHECK_HEADERS([stdlib.h unistd.h])  
  22.   
  23. # Checks for typedefs, structures, and compiler characteristics.  
  24.   
  25. # Checks for library functions.  
  26.   
  27. AC_OUTPUT  
我们修改成:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.   
  4. AC_PREREQ([2.69])  
  5. AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])  
  6. AC_CONFIG_SRCDIR([main.c])  
  7. #                                               -*- Autoconf -*-  
  8. # Process this file with autoconf to produce a configure script.  
  9.   
  10. AC_PREREQ([2.69])  
  11. AC_INIT(hello,1.0,test@qq.com)       
  12. AM_INIT_AUTOMAKE(hello,1.0)  
  13. AC_CONFIG_SRCDIR([main.c])  
  14. AC_CONFIG_HEADERS([config.h])  
  15.   
  16. # Checks for programs.  
  17. AC_PROG_CC  
  18.   
  19. # Checks for libraries.  
  20.   
  21. # Checks for header files.  
  22. AC_CHECK_HEADERS([stdlib.h unistd.h])  
  23.   
  24. # Checks for typedefs, structures, and compiler characteristics.  
  25.   
  26. # Checks for library functions.  
  27. AC_CONFIG_FILES([Makefile])  
  28. 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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c2]$ aclocal  
  2. [admin@localhost test_c2]$ ls  
  3. 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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c2]$ autoconf  
  2. [admin@localhost test_c2]$ ls  
  3. aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h  
  4. [admin@localhost test_c2]$   

5. Autoheader命令

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

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c2]$ autoheader  
  2. [admin@localhost test_c2]$ ls  
  3. aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure  configure.ac  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h  
  4. [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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. AUTOMARK_OPTIONS = foreign  
  2. bin_PROGRAMS = hello    
  3. 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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c2]$ automake --add-missing  
  2. configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:  
  3. configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation  
  4. configure.ac:6: installing './install-sh'  
  5. configure.ac:6: installing './missing'  
  6. Makefile.am: installing './INSTALL'  
  7. Makefile.am: error: required file './NEWS' not found  
  8. Makefile.am: error: required file './README' not found  
  9. Makefile.am: error: required file './AUTHORS' not found  
  10. Makefile.am: error: required file './ChangeLog' not found  
  11. Makefile.am: installing './COPYING' using GNU General Public License v3 file  
  12. Makefile.am:     Consider adding the COPYING file to the version control system  
  13. Makefile.am:     for your code, to avoid questions about which license your project uses  
  14. Makefile.am: installing './depcomp'  
  15. [admin@localhost test_c2]$ touch NEWS   
  16. [admin@localhost test_c2]$ touch README    
  17. [admin@localhost test_c2]$ touch AUTHORS  
  18. [admin@localhost test_c2]$ touch ChangeLog   
  19. [admin@localhost test_c2]$ automake --add-missing  
  20. configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:  
  21. configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation  
  22. [admin@localhost test_c2]$ ls  
  23. aclocal.m4  autom4te.cache  ChangeLog    configure     COPYING  get.c  INSTALL     main.c       Makefile.in  NEWS    sum.c  val.c  
  24. 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
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./configure  
  2. #具体命令省了  
  3. #可以看到生成了Makefile命令  
  4. [admin@localhost test_c2]$ ls  
  5. aclocal.m4  autom4te.cache  ChangeLog  config.h.in  config.status  configure.ac  depcomp  get.h      
  6. install-sh  Makefile     Makefile.in  NEWS      
  7. stamp-h1  sum.h  val.h  
  8. AUTHORS     autoscan.log    config.h   config.log   configure      COPYING       get.c    INSTALL  main.c       
  9.  Makefile.am  missing        
  10. README  sum.c     val.c  

9. make命令

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

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c2]$ make  
  2. make  all-am  
  3. make[1]: 进入目录“/home/admin/test_c2”  
  4. gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c  
  5. mv -f .deps/main.Tpo .deps/main.Po  
  6. gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT val.o -MD -MP -MF .deps/val.Tpo -c -o val.o val.c  
  7. mv -f .deps/val.Tpo .deps/val.Po  
  8. gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT get.o -MD -MP -MF .deps/get.Tpo -c -o get.o get.c  
  9. mv -f .deps/get.Tpo .deps/get.Po  
  10. gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT sum.o -MD -MP -MF .deps/sum.Tpo -c -o sum.o sum.c  
  11. mv -f .deps/sum.Tpo .deps/sum.Po  
  12. gcc  -g -O2   -o hello main.o val.o get.o sum.o    
  13. make[1]: 离开目录“/home/admin/test_c2”  
  14. [admin@localhost test_c2]$ ls  
  15. aclocal.m4  autom4te.cache  ChangeLog  config.h.in  config.status  configure.ac  depcomp  get.h    
  16. hello    install-sh  main.o    Makefile.am  missing  README    sum.c  sum.o  val.h  
  17. AUTHORS     autoscan.log    config.h   config.log   configure      COPYING       get.c      
  18. get.o  INSTALL  main.c      Makefile  Makefile.in  NEWS     stamp-h1  sum.h  val.c  val.o  
  19. [admin@localhost test_c2]$ ./hello   
  20. This is Value==  
  21. X:10   
  22. This is SUM Method!=========HDH  
  23. This is Main  
  24. Z:30  
  25. This is get  
  26. Z:400  


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

如果你的入口文件main.c和依赖的文件不是在同一个目录中的,使用Autotools来管理项目的时候会稍微复杂一下。

在不同的目录下,项目会生成*.a文件的静态连接(静态连接相当于将多个.o目标文件合成一个)。最外层的main.c会通过静态连接方式来实现连接。

1. 源代码例子

这个例子中会加入libevent和pthread,让例子稍显复杂,这样可以详细的介绍不同目录下的Autotools的使用。

我们创建两个目录:

include/  :放置.h头文件

src/ :放置.c 源文件

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c3]$ ls  
  2. include  main.c  src  

入口文件main.c:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "include/common.h"  
  2.   
  3. //入口主函数    
  4. int main() {  
  5.     puts("当前线程sleep 2秒");  
  6.     sleep(2);  
  7.     int x = 10;  
  8.     int y = 20;  
  9.     int z = sum(&x, &y);  
  10.     puts("This is Main");  
  11.     printf("Z:%d\n", z);  
  12.     x = 20;  
  13.     z = get(&x, &y);  
  14.     printf("Z:%d\n", z);  
  15.     return 1;  
  16. }  
common.h文件:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <event2/event.h>  
  5. #include <event2/bufferevent.h>  
  6. #include <pthread.h>  

get.h:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. int get(int *x, int *y);  
sum.h

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. int sum(int *x, int *y);  
val.h

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "common.h"  
  2. int val(int *x);  

get.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "../include/get.h"  
  2.   
  3. int get(int *x, int *y) {  
  4.         puts("This is get");  
  5.         return (*x) * (*y);  
  6. }  
sum.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "../include/sum.h"  
  2. #include "../include/val.h"  
  3.   
  4. int sum(int *x, int *y) {  
  5.         val(x);  
  6.         puts("This is SUM Method!=========HDH");  
  7.         return *x + *y;  
  8. }  
val.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "../include/val.h"  
  2.   
  3. int val(int *x) {  
  4.         //引入libevent的方法  
  5.         struct event_base *base; //定义一个event_base  
  6.         base = event_base_new(); //初始化一个event_base  
  7.         const char *x =  event_base_get_method(base); //查看用了哪个IO多路复用模型,linux一下用epoll    
  8.         printf("METHOD:%s\n", x);  
  9.         event_base_free(base);  //销毁libevent  
  10.   
  11.         puts("This is Value==");  
  12.         printf("X:%d \n", *x);  
  13.         return 0;  
  14. }  


2. 创建Makefile.am文件

在项目根目录下先创建Makefile.am文件。

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. AUTOMAKE_OPTIONS=foreign #软件等级  
  2. SUBDIRS=src  #先扫描子目录  
  3. bin_PROGRAMS=hello #软件生成后的可执行文件名称  
  4. hello_SOURCES=main.c #当前目录源文件  
  5. hello_LDADD=src/libpro.a #静态连接方式 连接src下生成的libpro.a文件  
  6. LIBS = -l pthread -l event #因为我们项目中用到了libevent和pthread,这个是动态连接  
在src/目录下创建Makefile.am文件。

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. noinst_LIBRARIES=libpro.a  #生成的静态库文件名称,noinst加上之后是只编译,不安装到系统中。  
  2. libpro_a_SOURCES=sum.c get.c val.c #这个静态库文件需要用到的依赖  
  3. include_HEADERS=../include/common.h ../include/sum.h ../include/get.h ../include/val.h #导入需要依赖的头文件  

说明:src/目录下面不加include_HEADERS也是可以运行的,但是在使用make dist打包命令后,并不会将include/文件夹打包进去,所以还是需要加上include_HEADERS。


3. 执行Autoscan命令

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

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c3]$ autoscan  
  2. [admin@localhost test_c3]$ ls  
  3. autoscan.log  configure.scan  include  main.c  Makefile.am  src  

修改configure.ac文件,主要添加AC_PROG_RANLIB(生成静态库);AC_PROG_LIBTOOL (用来生成动态库)

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.   
  4. AC_PREREQ([2.69])  
  5. AC_CONFIG_SRCDIR([main.c])  
  6. AC_INIT(hello,1.0,abc@126.com)  
  7. AM_INIT_AUTOMAKE(hello,1.0)  
  8. AC_PROG_RANLIB  
  9. AC_CONFIG_HEADERS([config.h])  
  10.   
  11. # Checks for programs.  
  12. AC_PROG_CC  
  13.   
  14. # Checks for libraries.  
  15.   
  16. # Checks for header files.  
  17. AC_CHECK_HEADERS([stdlib.h unistd.h])  
  18.   
  19. # Checks for typedefs, structures, and compiler characteristics.  
  20.   
  21. # Checks for library functions.  
  22.   
  23. AC_CONFIG_FILES([Makefile  
  24.                  src/Makefile])  
  25. AC_OUTPUT  


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

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c3]$ aclocal  
  2. [admin@localhost test_c3]$ ls  
  3. aclocal.m4  autom4te.cache  autoscan.log  configure.ac  include  main.c  Makefile.am  src  

5. Autoconf命令

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

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c3]$ autoconf  
  2. [admin@localhost test_c3]$ ls  
  3. aclocal.m4      autoscan.log  configure.ac  main.c       src  
  4. autom4te.cache  configure     include       Makefile.am  

6. Autoheader命令

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

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c3]$ autoheader  
  2. [admin@localhost test_c3]$ ls  
  3. aclocal.m4      autoscan.log  configure     include  Makefile.am  
  4. autom4te.cache  config.h.in   configure.ac  main.c   src  

7. Automake命令

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

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c3]$ touch NEWS  
  2. [admin@localhost test_c3]$ touch README  
  3. [admin@localhost test_c3]$ touch AUTHORS  
  4. [admin@localhost test_c3]$ touch ChangeLog  
  5. [admin@localhost test_c3]$ automake --add-missing  
  6. configure.ac:7: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:  
  7. configure.ac:7: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation  
  8. [admin@localhost test_c3]$ ls  
  9. aclocal.m4      autoscan.log  configure     include     Makefile.am  NEWS  
  10. AUTHORS         ChangeLog     configure.ac  install-sh  Makefile.in  README  
  11. autom4te.cache  config.h.in   depcomp       main.c      missing      src  

8. configure命令

第六步,执行./configure命令。./congigure主要把 Makefile.in 变成最终的 Makefile 文件。configure会把一些配置参数配置到Makefile文件里面。

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c3]$ ./configure   
  2. #命令省了  
  3. [admin@localhost test_c3]$ ls  
  4. aclocal.m4      ChangeLog    config.status  hello       main.o       missing  stamp-h1  
  5. AUTHORS         config.h     configure      include     Makefile     NEWS  
  6. autom4te.cache  config.h.in  configure.ac   install-sh  Makefile.am  README  
  7. autoscan.log    config.log   depcomp        main.c      Makefile.in  src  


9. make命令

第七步,执行make命令。make执行后,会生成hello的可执行文件。

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test_c3]$ make  
  2. make  all-recursive  
  3. make[1]: 进入目录“/home/admin/test_c3”  
  4. Making all in src  
  5. make[2]: 进入目录“/home/admin/test_c3/src”  
  6. gcc -DHAVE_CONFIG_H -I. -I..     -g -O2 -MT sum.o -MD -MP -MF .deps/sum.Tpo -c -o sum.o sum.c  
  7. mv -f .deps/sum.Tpo .deps/sum.Po  
  8. gcc -DHAVE_CONFIG_H -I. -I..     -g -O2 -MT val.o -MD -MP -MF .deps/val.Tpo -c -o val.o val.c  
  9. mv -f .deps/val.Tpo .deps/val.Po  
  10. rm -f libpro.a  
  11. ar cru libpro.a sum.o get.o val.o   
  12. ranlib libpro.a  
  13. make[2]: 离开目录“/home/admin/test_c3/src”  
  14. make[2]: 进入目录“/home/admin/test_c3”  
  15. gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c  
  16. mv -f .deps/main.Tpo .deps/main.Po  
  17. gcc  -g -O2   -o hello main.o src/libpro.a  -l pthread -l event   
  18. make[2]: 离开目录“/home/admin/test_c3”  
  19. make[1]: 离开目录“/home/admin/test_c3”  
  20. [admin@localhost test_c3]$ ./hello   
  21. 当前线程sleep 2秒  
  22. METHOD:epoll  
  23. This is Value==  
  24. X:10   
  25. This is SUM Method!=========HDH  
  26. This is Main  
  27. Z:30  
  28. This is get  
  29. Z:400  


Autotools运行流程

流程总结:

1. 执行autoscan命令。这个命令主要用于扫描工作目录,并且生成configure.scan文件

2. 修改configure.scan为configure.ac文件,并且修改配置内容

3. 执行aclocal命令。扫描 configure.ac 文件生成 aclocal.m4文件。

4. 执行autoconf命令。这个命令将 configure.ac 文件中的宏展开,生成 configure 脚本。

5. 执行autoheader命令。该命令生成 config.h.in 文件。

6. 新增Makefile.am文件,修改配置内容

7. 执行automake --add-missing命令。该命令生成 Makefile.in 文件。

8. 执行 ./congigure命令。将Makefile.in命令生成Makefile文件。

9. 执行make命令。生成可执行文件。


流程图:




Make命令详解

1. make命令:编译文件。make命令主要通过Makefile文件生成可执行文件。

2. make clean命令。清楚编译的文件,包括目标文件*.o和可执行文件

3.  make install 命令把目标文件安装到系统中。默认安装到/usr/local/bin目录下面。

4. make uninstall 命令,把目标文件从系统中卸载。

5. make dist 命令,打包发布。

如何使用发布的文件:

1. 下载到“hello-1.0.tar.gz”压缩文档

2. 使用“ tar -zxvf hello-1.0.tar.gz ”命令解压

3. 使用 “./configure” 命令,主要是生成Makefile命令,已经一些配置初始化。

4. 使用 “make” 命令编译源代码文件生成软件包。

5. 使用“make install ”命令来安装编译后的软件包到系统中。


Makefile.am解读

1. 可执行文件类型

可执行文件类型主要是只最终生成的可执行的文件。例如我们上面“c源文件同一目录下Autotools的使用”中的例子。

书写格式

说明

bin_PROGRAMS

生成的可执行文件名称。如果生成的可执行文件名称为多个,则可以通过空格的方式分隔。
bin_PROGRAMS:当运行make install命令的时候,可执行文件会默认安装到linux系统的/usr/local/bin目录下面
noinst_PROGRAMS:如果make install的时候不想被安装,可以使用noinst_PROGRAMS命令。
例子:bin_PROGRAMS=hello

hello_SOURCES

编译成可执行文件所依赖的.c源文件。多个源文件之间用空格分隔。hello为可执行文件名称。

hello_LDADD

编译成可执行文件过程中,连接所需的库文件,包括*.so的动态库文件和.a的静态库文件。

hello_LDFLAGS

连接的时候所需库文件的标识

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. bin_PROGRAMS=hello #软件生成后的可执行文件名称为hello  
  2. hello_SOURCES=main.c #当前目录源文件,如果当前目录有多个源文件,通过空格进行分隔  
  3. hello_LDADD=src/libpro.a #连接的时候所需的库文件  
  4. hello_LDFLAGS=    #连接的时候所需库文件的标识  
  5. LIBS= -l pthread -l event #<strong><span style="color:#FF0000;">第三方的库</span></strong>  


2. 静态库文件类型

静态库文件类型,一般会将c源码放在不同的文件夹中,并且每个文件夹中都会有各自的Makefile.am文件,并且会被编译成静态链接库 *.a格式的文件。

如果对静态库和动态库还没有一个概念,可以看我的《Linux c 开发 - 静态库和动态库》

注意:静态库使用中,需要对configure.ac中加入AC_PROG_RANLIB

书写格式

说明

noinst_LIBRARIES

生成静态库(*.a)或者动态库(*.so)的名称。
库文件一般以lib*.a或者lib*.so来命名。
noinst_LIBRARIES:当运行make install的时候,库文件不会被安装到linux默认的/usr/local/lib目录下。
lib_LIBRARIES:当运行make intsall的时候,则会被安装到/usr/local/lib目录下。
下面的例子:noinst_LIBRARIES=libpro.a

libpro_a_SOURCES

c的源文件,libpro_a即上面的livpro.a。多个文件用空格分开。

libpro_a_LDADD

加载所需的库文件。

libpro_a_LDFLAGS

编译的时候的连接标识。

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. noinst_LIBRARIES=libpro.a  #生成的静态库文件名称,noinst加上之后是只编译,不安装到系统中。  
  2. libpro_a_SOURCES=sum.c get.c val.c #这个静态库文件需要用到的源文件。  
  3. libpro_a_LDADD = #加载库文件  
  4. libpro_a_LDFLAGS= #连接的时候所需库文件的标识  

 
 
 
 

3. 头文件

我们一般需要导入一些*.h的头文件,如果你在Makefile.am中没有标识需要导入的头文件,可能在make dist打包的时候出现问题,头文件可能不会被打进包里面。

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. include_HEADERS=../include/common.h ../include/sum.h ../include/get.h ../include/val.h  #可以将头文件引入  

make install,头文件默认会被安装到linux系统/usr/local/include

4. 数据文件

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. data_DATA = data1 data2  


5. 常用变量

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. AUTOMAKE_OPTIONS=foreign #软件等级  
  2. SUBDIRS=src  #先扫描子目录,多个目录用空格隔开  
  3. LIBS = -l pthread -l event #因为我们项目中用到了libevent和pthread,这个是动态连接,在编译的时候会自动加上 -l pthread -l event  
  4. EXTRA_DIST = conf   #打包一些配置文件   
6. 安装目录

我们知道,默认情况下,执行make install命令,则会将文件安装到/usr/local/bin   /usr/local/include  /usr/local/lib目录下面。

我们可以通过命令./configure --prefix= 生成Makefile文件的时候,配置make install命令执行的时候的文件安装路径。

下面这个例子,我们在执行make install的时候,程序会被安装到/home/test目录下面。

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./configure --prefix=/home/test  

执行下面一系列命令:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ./configure --prefix=/home/test  
  2. make  
  3. sudo make install  
我们可以进入/home/test目录下看到相应的bin文件已经生成:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [admin@localhost test]$ ls  
  2. bin  include lib  


下面这些变量是已经定义好的安装路径的变量。

用户也可以修改这些变量。例如将bindir修改成$(prefix)/bin2

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. bindir = $(prefix)/bin。  
  2.   
  3. libdir = $(prefix)/lib。  
  4.   
  5. datadir=$(prefix)/share。  
  6.   
  7. sysconfdir=$(prefix)/etc。  
  8.   
  9. includedir=$(prefix)/include。  

假如我们有自定义的文件夹,我们需要将这个文件夹下的内容安装到安装目录,

则需要配置一个自定义的文件夹目录confdir

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. confdir=${prefix}/conf   #conf为名称  dir为每个文件夹变量必须带上  
  2. conf_DATA=conf/*     #这个是将conf/目录下的内容安装到confdir目录下  
  3. EXTRA_DIST=conf      #在make dist打包的时候 也要将扩展文件夹打包进去  

confdir为需要创建的文件夹目录。

conf_DATA为需要拷贝的文件内容到${prefix}/conf目录中去



  • 22
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值