autotools使用的流程图

6e711537766173b7f4e95be17d13bf96.png

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

准备编译的源码

 

查看文件列表

lin@lin-host:~/Desktop/test_c2$ ls
get.c  get.h  main.c  sum.c  sum.h  val.c  val.h

Autoscan命令

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

lin@lin-host:~/Desktop/test_c2$ autoscan

查看文件列表

lin@lin-host:~/Desktop/test_c2$ ls 
autoscan.log configure.scan get.c get.h main.c sum.c sum.h val.c val.h

并且configure.scan需要重命令成configure.ac,然后编辑这个配置,我们才能继续执行后面的命令。

lin@lin-host:~/Desktop/test_c2$ mv configure.scan configure.ac

查看文件列表

lin@lin-host:~/Desktop/test_c2$ ls
 autoscan.log configure.ac get.c get.h main.c sum.c sum.h val.c val.h

编辑configure.ac文件

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

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

AC_PREREQ([2.69])
AC_INIT([hello], [1.0.0], [libra13179@163.com])
AM_INIT_AUTOMAKE([-Wall -Werror])
AC_CONFIG_SRCDIR([main.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_CONFIG_FILES([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 中。

lin@lin-host:~/Desktop/test_c2$ aclocal

查看文件列表

lin@lin-host:~/Desktop/test_c2$ ls 
aclocal.m4 autoscan.log configure.ac~ get.h sum.c val.c autom4te.cache configure.ac get.c main.c sum.h val.h

Autoconf命令

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

lin@lin-host:~/Desktop/test_c2$ autoconf

查看文件列表

lin@lin-host:~/Desktop/test_c2$ ls -l total 248 -rw-rw-r-- 1 lin lin 41927 3月 18 17:57 aclocal.m4 drwxr-xr-x 2 lin lin 4096 3月 18 17:58 autom4te.cache -rw-rw-r-- 1 lin lin 0 3月 18 17:36 autoscan.log -rwxrwxr-x 1 lin lin 164719 3月 18 17:58 configure -rw-rw-r-- 1 lin lin 544 3月 18 17:57 configure.ac -rw-rw-r-- 1 lin lin 544 3月 18 17:54 configure.ac~ -rw-rw-r-- 1 lin lin 90 3月 18 15:58 get.c -rw-rw-r-- 1 lin lin 86 3月 18 15:58 get.h -rw-rw-r-- 1 lin lin 346 3月 18 15:54 main.c -rw-rw-r-- 1 lin lin 132 3月 18 15:56 sum.c -rw-rw-r-- 1 lin lin 86 3月 18 15:56 sum.h -rw-rw-r-- 1 lin lin 100 3月 18 15:57 val.c -rw-rw-r-- 1 lin lin 78 3月 18 15:57 val.h
View Code

Autoheader命令

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

lin@lin-host:~/Desktop/test_c2$  autoheader

查看文件列表

lin@lin-host:~/Desktop/test_c2$ ls -l total 252 -rw-rw-r-- 1 lin lin 41927 3月 18 17:57 aclocal.m4 drwxr-xr-x 2 lin lin 4096 3月 18 17:58 autom4te.cache -rw-rw-r-- 1 lin lin 0 3月 18 17:36 autoscan.log -rw-rw-r-- 1 lin lin 1437 3月 18 17:59 config.h.in -rwxrwxr-x 1 lin lin 164719 3月 18 17:58 configure -rw-rw-r-- 1 lin lin 544 3月 18 17:57 configure.ac -rw-rw-r-- 1 lin lin 544 3月 18 17:54 configure.ac~ -rw-rw-r-- 1 lin lin 90 3月 18 15:58 get.c -rw-rw-r-- 1 lin lin 86 3月 18 15:58 get.h -rw-rw-r-- 1 lin lin 346 3月 18 15:54 main.c -rw-rw-r-- 1 lin lin 132 3月 18 15:56 sum.c -rw-rw-r-- 1 lin lin 86 3月 18 15:56 sum.h -rw-rw-r-- 1 lin lin 100 3月 18 15:57 val.c -rw-rw-r-- 1 lin lin 78 3月 18 15:57 val.h
View Code

创建Makefile.am文件

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

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

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

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

Automake命令

执行automake --add-missing命令。该命令生成 Makefile.in 文件。使用选项 "--add-missing" 可以让 Automake 自动添加一些必需的脚本文件。

lin@lin-host:~/Desktop/test_c2$ automake --add-missing
configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
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'

如果发现一些文件不存在,可以通过手工 touch命令创建。

lin@lin-host:~/Desktop/test_c2$touch NEWS README AUTHORS ChangeLog

继续

lin@lin-host:~/Desktop/test_c2$ automake --add-missing

查看文件列表

lin@lin-host:~/Desktop/test_c2$ ls
aclocal.m4      compile        COPYING  install-sh    missing  val.c
AUTHORS         config.h.in    depcomp  main.c        NEWS     val.h
autom4te.cache  configure      get.c    Makefile.am   README
autoscan.log    configure.ac   get.h    Makefile.am~  sum.c
ChangeLog       configure.ac~  INSTALL  Makefile.in   sum.h
View Code

./ configure命令

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

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

lin@lin-host:~/Desktop/test_c2$ ./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 how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for stdlib.h... (cached) yes
checking for unistd.h... (cached) yes
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

查看文件列表

lin@lin-host:~/Desktop/test_c2$ ls
aclocal.m4      config.h       configure.ac~  install-sh    missing   val.c
AUTHORS         config.h.in    COPYING        main.c        NEWS      val.h
autom4te.cache  config.log     depcomp        Makefile      README
autoscan.log    config.status  get.c          Makefile.am   stamp-h1
ChangeLog       configure      get.h          Makefile.am~  sum.c
compile         configure.ac   INSTALL        Makefile.in   sum.h
View Code

make命令

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

```
lin@lin-host:~/Desktop/test_c2$ make
make  all-am
make[1]: Entering directory `/home/lin/Desktop/test_c2'
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]: Leaving directory `/home/lin/Desktop/test_c2'
```

 


在当前目录下有Makefile文件,我们可以做的工作有:
*    make all:产生设定的目标,即生成所有的可执行文件。使用make也可以达到此目的。
*    make clean:删除之前编译时生成的可执行文件及目标文件(形如*.o的中间文件)。
*    make distclean:除了删除可执行文件和目标文件以外,把configure所产生的 Makefile文件也清除掉。通常在发布软件前执行该命令。
*    make install:将使用make all或make命令产生的可执行文件以软件的形式安装到系统中。若使用bin_PROGRAMS宏,程序将会被安装到 /usr/local/bin下,否则安装到预定义的目录下。
*    make dist:将程序和相关的文档包装为一个压缩文档以供发布。执行完该命令,在当前目录下会产生一个名为PACKAGE-VERSION.tar.gz的文件。PACKAGE 和 VERSION 这两个参数是来自configure.in文件中的AM_INIT_AUTOMAKE(PACKAGE,
VERSION)。如在上个例子中执行make dist命令,会产生名为“.tar.gz”的文件。
*   make distcheck:与make dist类似,但是加入了检查包装以后的压缩文件是否正常。
 
其他参考
https://linux.cn/article-11218-1.html

 

posted on 2020-03-24 09:24  陌鉎こ城sHi  阅读(316)  评论(0编辑  收藏  举报