Autotools使用

Autotools工具使用

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***命令。生成可执行文件。

Autotools使用_第1张图片

单路径使用autotools

准备文件: val.h、val.c、get.h、get.c、sum.h、sum.c 、main.c

--------val.h---------
#include 
#include 
#include 
 
int val(int *x);
 
 
--------val.c---------
#include "val.h"
 
int val(int *x) {
	puts("This is Value==");
	printf("X:%d \n", *x);
	return 0;
}
 
----------get.h----------
#include 
#include 
#include 
 
int get(int *x, int *y);
 
----------get.c-----------
#include "get.h"
 
int get(int *x, int *y) {
	puts("This is get");
	return (*x) * (*y);
}
 
-------sum.h--------
#include 
#include 
#include 
 
int sum(int *x, int *y);
 
--------sum.c---------
#include "sum.h"
#include "val.h"
 
int sum(int *x, int *y) {
	val(x);
	puts("This is SUM Method!=========HDH");
	return *x + *y;
}

--------main.c 入口文件-----

#include 
#include 
#include 
#include "sum.h"
#include "get.h"
 
 
//入口主函数
int main() {
	int x = 10;
	int y = 20;
	int z = sum(&x, &y);
	puts("This is Main");
	printf("Z:%d\n", z);
	x = 20;
	z = get(&x, &y);
	printf("Z:%d\n", z);
	return 1;
}

查看准备的文件

[root@node-1 wqz]# ls
get.c  get.h  main.c  sum.c  sum.h  val.c  val.h
Autoscan命令

在项目的目录下执行autoscan命令,这个命令组要是扫描工作目录,并且 生成configure.scan文件。 在把configure.scan文件重命名configure.ac文件

[root@node-1 wqz]# autoscan
[root@node-1 wqz]# ls
autoscan.log  configure.scan  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h
[root@node-1 wqz]# mv configure.scan configure.ac
[root@node-1 wqz]# ls
autoscan.log  configure.ac  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h

在次修改configure.ac文件

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_CONFIG_SRCDIR([get.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

修改为

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

AC_PREREQ([2.69])
AC_INIT(hello, 1.0, [email protected])  #修改为自己对应的
AC_CONFIG_SRCDIR([main.c])
AM_INIT_AUTOMAKE(hello, 1.0)    #增加必须要的,参数为软件名称和版本号,也可以直接写AM_INIT_AUTOMAKE,不加后面的参数,应为上面AC_INIT已经定义过了
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CONFIG_FILES([Makefile])  # 生成相应的Makefile文件,不同文件夹下的Makefile通过空格分隔。例如:AC_CONFIG_FILES([Makefile, src/Makefile]) 
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。
Aclocal命令

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

[root@node-1 wqz]# aclocal  
[root@node-1 wqz]# ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h

Autoconf命令

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

[root@node-1 wqz]# autoconf
[root@node-1 wqz]# ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  get.c  get.h  main.c  sum.c  sum.h  val.c  val.h
Autoheader命令

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

[root@node-1 wqz]# autoheader
[root@node-1 wqz]# ls
aclocal.m4      autoscan.log  configure     get.c  main.c  sum.h  val.h
autom4te.cache  config.h.in   configure.ac  get.h  sum.c   val.c

创建Makefile.am文件

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

vim Makefile.am

AUTOMAKE_OPTIONS = foreign  
bin_PROGRAMS = hello    
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后面我们会有一个章节专门讲这块内容。

Automake命令

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

[root@node-1 wqz]# automake --add-missing
configure.ac:7: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:7: https://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
Makefile.am: installing './INSTALL'
Makefile.am: error: required file './NEWS' not found  #这里报什么不存在就创建什么
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses
Makefile.am: installing './depcomp'
[root@node-1 wqz]# touch NEWS
[root@node-1 wqz]# touch README
[root@node-1 wqz]# touch AUTHORS
[root@node-1 wqz]# touch ChangeLog
[root@node-1 wqz]# automake --add-missing
configure.ac:7: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:7: https://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure命令

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

[root@node-1 wqz]# ./configure
[root@node-1 wqz]# ls
aclocal.m4      autoscan.log  config.h     config.status  COPYING  get.h       main.c       Makefile.in  README    sum.h
AUTHORS         ChangeLog     config.h.in  configure      depcomp  INSTALL     Makefile     missing      stamp-h1  val.c
autom4te.cache  compile       config.log   configure.ac   get.c    install-sh  Makefile.am  NEWS         sum.c     val.h

make命令

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

[root@node-1 wqz]# make
make  all-am
make[1]: 进入目录“/root/wqz”
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT val.o -MD -MP -MF .deps/val.Tpo -c -o val.o val.c
mv -f .deps/val.Tpo .deps/val.Po
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT get.o -MD -MP -MF .deps/get.Tpo -c -o get.o get.c
mv -f .deps/get.Tpo .deps/get.Po
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT sum.o -MD -MP -MF .deps/sum.Tpo -c -o sum.o sum.c
mv -f .deps/sum.Tpo .deps/sum.Po
gcc  -g -O2   -o hello main.o val.o get.o sum.o  
make[1]: 离开目录“/root/wqz”
[root@node-1 wqz]# ls
aclocal.m4      ChangeLog    config.log     COPYING  get.o       main.c       Makefile.in  stamp-h1  val.c
AUTHORS         compile      config.status  depcomp  hello       main.o       missing      sum.c     val.h
autom4te.cache  config.h     configure      get.c    INSTALL     Makefile     NEWS         sum.h     val.o
autoscan.log    config.h.in  configure.ac   get.h    install-sh  Makefile.am  README       sum.o
[root@node-1 wqz]# ./hello 
This is Value==
X:10 
This is SUM Method!=========HDH
This is Main
Z:30
This is get
Z:400

ref:https://blog.csdn.net/zhengqijun_/article/details/70105077

多路径使用autotools

ref :https://blog.csdn.net/thalo1204/article/details/49183911

准备文件: 建立两个文件夹 include、src(include里面放置头文件,src中放置源文件) 两个文件: pr.h 、 hello.c

[root@node-1 wqz]# mkdir include src
[root@node-1 wqz]# ls
include  src
-------include/pr.h---------
int pr()
{ 
    printf("helloworld");
    return 0;
}
-------src/hello.c----

#include 
#include "pr.h"
int main()
{
    int x;
    x=pr();
    return 0;
}
  1. 执行autoscan生成configure.scan文件

    [root@node-1 wqz]# autoscan
    [root@node-1 wqz]# ls
    autoscan.log  configure.scan  include  src
    
  2. 修改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_CONFIG_SRCDIR([include/pr.h])
    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_OUTPUT
    
    

    修改为

    
    #                                               -*- Autoconf -*-
    # Process this file with autoconf to produce a configure script.
    
    AC_PREREQ([2.69])
    AC_INIT(wqz-test, 1.1, [email protected])  # 修改
    AM_INIT_AUTOMAKE   # 增加
    AC_CONFIG_SRCDIR([src/hello.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
    
    
  3. 执行aclocal生成aclocal.m4文件以及cache文件夹,处理本地的宏定义。

    [root@node-1 wqz]# aclocal
    [root@node-1 wqz]# ls
    aclocal.m4  autom4te.cache  autoscan.log  configure.ac  include  src
    
    
  4. 执行autoheader生成config.h.in文件

    [root@node-1 wqz]# autoheader 
    [root@node-1 wqz]# ls
    aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure.ac  include  src
    
  5. 编写Makefile.am文件

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=include/pr.h src/hello.c
hello_CPPFLAGS=-I include/
  1. 执行automake命令
    automake必须要以下文件
    Autotools使用_第2张图片
    用automake –a可以自动生成以下文件
    Autotools使用_第3张图片

    所以要手动生成以下文件

    [root@node-1 wqz]# touch NEWS README AUTHORS ChangeLog
    

    也可以使用automake --add-missing让automake自动添加有一些必需的脚本文件。

    运行automake -a 是–add-missing的简写, -a, --add-missing add missing standard files to package
    生成Makefile.in

    [root@node-1 wqz]# automake --add-missing
    configure.ac:11: installing './compile'
    configure.ac:6: installing './install-sh'
    configure.ac:6: installing './missing'
    configure.ac:8: error: required file 'config.h.in' not found
    Makefile.am:3: warning: source file 'src/hello.c' is in a subdirectory,
    Makefile.am:3: but option 'subdir-objects' is disabled
    automake: warning: possible forward-incompatibility.
    automake: At least a source file is in a subdirectory, but the 'subdir-objects'
    automake: automake option hasn't been enabled.  For now, the corresponding output
    automake: object file(s) will be placed in the top-level directory.  However,
    automake: this behaviour will change in future Automake versions: they will
    automake: unconditionally cause object files to be placed in the same subdirectory
    automake: of the corresponding sources.
    automake: You are advised to start using 'subdir-objects' option throughout your
    automake: project, to avoid future incompatibilities.
    Makefile.am: installing './depcomp'
    [root@node-1 wqz]# touch NEWS README AUTHORS ChangeLog
    [root@node-1 wqz]# ls
    aclocal.m4  autom4te.cache  ChangeLog  config.h.in   depcomp  install-sh   Makefile.in  NEWS    src
    AUTHORS     autoscan.log    compile    configure.ac  include  Makefile.am  missing      README
    
    
  2. 执行autoconf, 由configure.ac aclocal.m4生成configure

[root@node-1 wqz]# autoconf
[root@node-1 wqz]# ls
aclocal.m4  autom4te.cache  ChangeLog  config.h.in  configure.ac  include     Makefile.am  missing  README
AUTHORS     autoscan.log    compile    configure    depcomp       install-sh  Makefile.in  NEWS     src
  1. 执行 ./configure 生成Makefile和config.h

    [root@node-1 wqz]# ./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... /usr/bin/mkdir -p
    checking for gawk... gawk
    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 whether make supports the include directive... yes (GNU style)
    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
    [root@node-1 wqz]# ls
    aclocal.m4      autoscan.log  config.h     config.status  depcomp     Makefile     missing  src
    AUTHORS         ChangeLog     config.h.in  configure      include     Makefile.am  NEWS     stamp-h1
    autom4te.cache  compile       config.log   configure.ac   install-sh  Makefile.in  README
    
    
  2. 执行make dist生成安装包

    [root@node-1 wqz]# make dist
    make  dist-gzip am__post_remove_distdir='@:'
    make[1]: 进入目录“/root/wqz”
    make  distdir-am
    make[2]: 进入目录“/root/wqz”
    if test -d "wqz-test-1.1"; then find "wqz-test-1.1" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "wqz-test-1.1" || { sleep 5 && rm -rf "wqz-test-1.1"; }; else :; fi
    test -d "wqz-test-1.1" || mkdir "wqz-test-1.1"
    test -n "" \
    || find "wqz-test-1.1" -type d ! -perm -755 \
    	-exec chmod u+rwx,go+rx {} \; -o \
      ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
      ! -type d ! -perm -400 -exec chmod a+r {} \; -o \
      ! -type d ! -perm -444 -exec /bin/sh /root/wqz/install-sh -c -m a+r {} {} \; \
    || chmod -R a+r "wqz-test-1.1"
    make[2]: 离开目录“/root/wqz”
    tardir=wqz-test-1.1 && ${TAR-tar} chof - "$tardir" | eval GZIP= gzip --best -c >wqz-test-1.1.tar.gz
    make[1]: 离开目录“/root/wqz”
    if test -d "wqz-test-1.1"; then find "wqz-test-1.1" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "wqz-test-1.1" || { sleep 5 && rm -rf "wqz-test-1.1"; }; else :; fi
    [root@node-1 wqz]# ls
    aclocal.m4      autoscan.log  config.h     config.status  depcomp     Makefile     missing  src
    AUTHORS         ChangeLog     config.h.in  configure      include     Makefile.am  NEWS     stamp-h1
    autom4te.cache  compile       config.log   configure.ac   install-sh  Makefile.in  README   wqz-test-1.1.tar.gz
    
    
  3. 解压并执行

    [root@node-1 wqz]# tar -zxf wqz-test-1.1.tar.gz 
    [root@node-1 wqz]# cd wqz-test-1.1/
    [root@node-1 wqz-test-1.1]# ls
    aclocal.m4  ChangeLog  config.h.in  configure.ac  include     Makefile.am  missing  README
    AUTHORS     compile    configure    depcomp       install-sh  Makefile.in  NEWS     src
    [root@node-1 wqz-test-1.1]# ./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... /usr/bin/mkdir -p
    checking for gawk... gawk
    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 whether make supports the include directive... yes (GNU style)
    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
    [root@node-1 wqz-test-1.1]# make
    make  all-am
    make[1]: 进入目录“/root/wqz/wqz-test-1.1”
    gcc -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT hello-hello.o -MD -MP -MF .deps/hello-hello.Tpo -c -o hello-hello.o `test -f 'src/hello.c' || echo './'`src/hello.c
    mv -f .deps/hello-hello.Tpo .deps/hello-hello.Po
    gcc  -g -O2   -o hello hello-hello.o  
    make[1]: 离开目录“/root/wqz/wqz-test-1.1”
    [root@node-1 wqz-test-1.1]# ls
    aclocal.m4  compile      config.log     configure.ac  hello-hello.o  Makefile     missing  src
    AUTHORS     config.h     config.status  depcomp       include        Makefile.am  NEWS     stamp-h1
    ChangeLog   config.h.in  configure      hello         install-sh     Makefile.in  README
    [root@node-1 wqz-test-1.1]# ./hello
    helloworld
    

你可能感兴趣的:(c语言,c++,开发语言)