autotools 自动编译系列简介

autotools安装

 

1.介绍


Linux环境下,我们编译程序啥的都是一般用的GCC&&GDB等等工具,直接使用GCC命令进行编译操作。这种方式一般是适用于程序文件比较少,组织结构比较简单的情况。但是,当我们程序文件比较的多的时候,或者是程序文件组织结构比较的复杂(例如在程序文件夹中存在文件夹多层嵌套以及复杂引用等),此时我们如果是直接使用GCC一点一点的编译工作量会非常的大,而且万一程序修改了,还要重新的再工作一遍。为此,我们有了make工具,依靠Makefile辅助文件,我们可以方便的进行工程的管理,以及编译操作。当程序很复杂的时候,依靠我们去手工的建立、维护Makefile文件是非常的不现实的,不仅很复杂,而且费时费力,还容易出错。为此,就有了我们的_Autotools_工具或者 CMake 工具栈,而本文重点介绍的是 Autotool 管理 Makefile 的工具链。autotools 只要输入工程中的目标文件、依赖文件、文件目录等信息就可以自动生成Makefile。这时使用autotools工具就是一个不错的选择,只要输入工程中的目标文件、依赖文件、文件目录等信息就可以自动生成Makefile。autotools工具是个系列工具,主要有:aclocal、autoscan、autoconf、autoheader、automake。

 

2.autotools组成

名称功能
autoscan autoscan是用来扫描源代码目录生成configure.scan文件的。    configure.scan包含了系统配置的基本选项,里面都是一些宏定义。我们需要将它改名为configure.in
aclocal aclocal是一个perl脚本程序。aclocal根据configure.in文件的内容,自动生成aclocal.m4文件。aclocal的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。    生成的aclocal.m4是宏展开文件
autoconfautoconf是用来产生configure文件的     configure.in文件的内容是一些宏,这些宏经过autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本
autoheader 自动生成config.h.in    在configure生成config.h时候的in文件
automake我们使用automake --add-missing来产生Makefile.in     Makefile.am是用来生成Makefile.in的,需要你手工书写

 

3.autotools处理流程


Autotools使用流程:
1.目录树的最高层运行autoscan,生成configure.scan文件;
2.运行aclocal,生成aclocal.m4文件;
3.运行autoconf,生成configure配置脚本;
4.运行autoheader,生成config.h.in文件;
5.手工编写Makefile.am文件;
6.运行automake,生成Makefile.in;
7.运行配置脚本configure,生成Makefile。

 

4.autotools安装


[root@localhost ~]# yum install automake
安装包:

automake-1.13.4-3.el7.noarch.rpm
autoconf-2.69-11.el7.noarch.rpm
perl-Test-Harness-3.28-3.el7.noarch.rpm
m4-1.4.16-10.el7.x86_64.rpm
 

autotools实例

1.生成源码,并且确认代码可以编译执行

[root@localhost ~]#mkdir /home/mycode/auto_make_test/
[root@localhost ~]# cd /home/mycode/auto_make_test/
[root@localhost auto_make_test]# ll
total 0
[root@localhost auto_make_test]# vim auto_test.c
[root@localhost auto_make_test]# cat auto_test.c 
#include <stdio.h>
 
int main()
{
    printf("auto make test\n");
    return 0;
}
[root@localhost auto_make_test]# gcc auto_test.c 
[root@localhost auto_make_test]# ll
total 16
-rwxr-xr-x 1 root root 8560 Jan  5 17:01 a.out
-rw-r--r-- 1 root root   81 Jan  5 17:01 auto_test.c
[root@localhost auto_make_test]# ./a.out 
auto make test
[root@localhost auto_make_test]# rm a.out 
rm: remove regular file ‘a.out’? y

2.执行autoscan


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

[root@localhost auto_make_test]# autoscan 
[root@localhost auto_make_test]# ll
total 8
-rw-r--r-- 1 root root   0 Jan  5 17:35 autoscan.log
-rw-r--r-- 1 root root  81 Jan  5 17:01 auto_test.c
-rw-r--r-- 1 root root 471 Jan  5 17:35 configure.scan
[root@localhost auto_make_test]# mv configure.scan configure.ac

第二步:编辑上面得到的configure.ac文件。

[root@localhost auto_make_test]# cat 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([auto_test.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_OUTPUT
[root@localhost auto_make_test]# vim configure.ac 
[root@localhost auto_make_test]# cat configure.ac 
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
 
AC_PREREQ([2.69])
AC_INIT([auto_test], [1.0], [751773517@qq.com])
AC_CONFIG_SRCDIR([auto_test.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(auto_test,1.0)
 
# 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(Makefile)

对比前后的文件可以发现修改的项目为:AC_INIT,AC_OUTPUT
增加了:AM_INIT_AUTOMAKE(auto_test,1.0)
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 中。

[root@localhost auto_make_test]# aclocal
[root@localhost auto_make_test]# ll
total 52
-rw-r--r-- 1 root root 37794 Jan  5 17:43 aclocal.m4
drwxr-xr-x 2 root root  4096 Jan  5 17:43 autom4te.cache
-rw-r--r-- 1 root root     0 Jan  5 17:42 autoscan.log
-rw-r--r-- 1 root root    81 Jan  5 17:01 auto_test.c
-rw-r--r-- 1 root root   499 Jan  5 17:38 configure.ac


 4.执行autoconf


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

[root@localhost auto_make_test]# autoconf
[root@localhost auto_make_test]# ll
total 196
-rw-r--r-- 1 root root  37794 Jan  5 17:43 aclocal.m4
drwxr-xr-x 2 root root   4096 Jan  5 17:43 autom4te.cache
-rw-r--r-- 1 root root      0 Jan  5 17:42 autoscan.log
-rw-r--r-- 1 root root     81 Jan  5 17:01 auto_test.c
-rwxr-xr-x 1 root root 141850 Jan  5 17:43 configure
-rw-r--r-- 1 root root    499 Jan  5 17:38 configure.ac

5.执行autoheader


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

[root@localhost auto_make_test]# autoheader 
[root@localhost auto_make_test]# ll
total 200
-rw-r--r-- 1 root root  37794 Jan  5 17:43 aclocal.m4
drwxr-xr-x 2 root root   4096 Jan  5 17:43 autom4te.cache
-rw-r--r-- 1 root root      0 Jan  5 17:42 autoscan.log
-rw-r--r-- 1 root root     81 Jan  5 17:01 auto_test.c
-rw-r--r-- 1 root root    625 Jan  5 17:43 config.h.in
-rwxr-xr-x 1 root root 141850 Jan  5 17:43 configure
-rw-r--r-- 1 root root    499 Jan  5 17:38 configure.ac

6、创建Makefile.am文件:


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

[root@localhost auto_make_test]# vim Makefile.am
[root@localhost auto_make_test]# cat Makefile.am 
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=auto_test
auto_test_SOURCES=auto_test.c

1). AUTOMAKE_OPTIONS:由于GNU对自己发布的软件有严格的规范, 比如必须附带许可证声明文件COPYING等,否则automake执行时会报错. automake提供了3中软件等级:foreign, gnu和gnits, 供用户选择。默认级别是gnu。在本例中,使用了foreign等级, 它只检测必须的文件。
2). bin_PROGRAMS = auto_test:生成的可执行文件名称,生成多个可执行文件,可以用空格隔开。
3). auto_test_SOURCES:生成可执行文件auto_test需要依赖的源文件。其中auto_test_为可执行文件的名称。


7、执行automake

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

[root@localhost auto_make_test]# ll
total 204
-rw-r--r-- 1 root root  37794 Jan 15 15:30 aclocal.m4
drwxr-xr-x 2 root root   4096 Jan 15 15:30 autom4te.cache
-rw-r--r-- 1 root root      0 Jan 15 15:29 autoscan.log
-rw-r--r-- 1 root root     81 Jan  5 17:01 auto_test.c
-rw-r--r-- 1 root root    625 Jan 15 15:31 config.h.in
-rwxr-xr-x 1 root root 141850 Jan 15 15:30 configure
-rw-r--r-- 1 root root    499 Jan  5 17:38 configure.ac
-rw-r--r-- 1 root root     78 Jan  5 17:45 Makefile.am
[root@localhost auto_make_test]# automake --add-missing
configure.ac:8: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:8: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:8: installing './install-sh'
configure.ac:8: installing './missing'
Makefile.am: installing './depcomp'
[root@localhost auto_make_test]# ll
total 228
-rw-r--r-- 1 root root  37794 Jan 15 15:30 aclocal.m4
drwxr-xr-x 2 root root   4096 Jan 15 15:32 autom4te.cache
-rw-r--r-- 1 root root      0 Jan 15 15:29 autoscan.log
-rw-r--r-- 1 root root     81 Jan  5 17:01 auto_test.c
-rw-r--r-- 1 root root    625 Jan 15 15:31 config.h.in
-rwxr-xr-x 1 root root 141850 Jan 15 15:30 configure
-rw-r--r-- 1 root root    499 Jan  5 17:38 configure.ac
lrwxrwxrwx 1 root root     32 Jan 15 15:32 depcomp -> /usr/share/automake-1.13/depcomp
lrwxrwxrwx 1 root root     35 Jan 15 15:32 install-sh -> /usr/share/automake-1.13/install-sh
-rw-r--r-- 1 root root     78 Jan  5 17:45 Makefile.am
-rw-r--r-- 1 root root  23321 Jan 15 15:32 Makefile.in
lrwxrwxrwx 1 root root     32 Jan 15 15:32 missing -> /usr/share/automake-1.13/missing
[root@localhost auto_make_test]# 

8、执行configure:


执行configure生成Makefile文件,便可以运行make生成可执行文件了。

[root@localhost auto_make_test]# ./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
-rw-r--r-- 1 root root    625 Jan 15 15:31 config.h.in
-rw-r--r-- 1 root root   8546 Jan 15 15:34 config.log
-rwxr-xr-x 1 root root  32482 Jan 15 15:34 config.status
-rwxr-xr-x 1 root root 141850 Jan 15 15:30 configure
-rw-r--r-- 1 root root    499 Jan  5 17:38 configure.ac
lrwxrwxrwx 1 root root     32 Jan 15 15:32 depcomp -> /usr/share/automake-1.13/depcomp
lrwxrwxrwx 1 root root     35 Jan 15 15:32 install-sh -> /usr/share/automake-1.13/install-sh
-rw-r--r-- 1 root root  23092 Jan 15 15:34 Makefile
-rw-r--r-- 1 root root     78 Jan  5 17:45 Makefile.am
-rw-r--r-- 1 root root  23321 Jan 15 15:32 Makefile.in
lrwxrwxrwx 1 root root     32 Jan 15 15:32 missing -> /usr/share/automake-1.13/missing
-rw-r--r-- 1 root root     23 Jan 15 15:34 stamp-h1
[root@localhost auto_make_test]# make
make  all-am
make[1]: Entering directory `/home/mycode/auto_make_test'
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT auto_test.o -MD -MP -MF .deps/auto_test.Tpo -c -o auto_test.o auto_test.c
mv -f .deps/auto_test.Tpo .deps/auto_test.Po
gcc  -g -O2   -o auto_test auto_test.o  
make[1]: Leaving directory `/home/mycode/auto_make_test'
[root@localhost auto_make_test]# ll
total 324
-rw-r--r-- 1 root root  37794 Jan 15 15:30 aclocal.m4
drwxr-xr-x 2 root root   4096 Jan 15 15:32 autom4te.cache
-rw-r--r-- 1 root root      0 Jan 15 15:29 autoscan.log
-rwxr-xr-x 1 root root  11048 Jan 15 15:34 auto_test
-rw-r--r-- 1 root root     81 Jan  5 17:01 auto_test.c
-rw-r--r-- 1 root root   5984 Jan 15 15:34 auto_test.o
-rw-r--r-- 1 root root    778 Jan 15 15:34 config.h
-rw-r--r-- 1 root root    625 Jan 15 15:31 config.h.in
-rw-r--r-- 1 root root   8546 Jan 15 15:34 config.log
-rwxr-xr-x 1 root root  32482 Jan 15 15:34 config.status
-rwxr-xr-x 1 root root 141850 Jan 15 15:30 configure
-rw-r--r-- 1 root root    499 Jan  5 17:38 configure.ac
lrwxrwxrwx 1 root root     32 Jan 15 15:32 depcomp -> /usr/share/automake-1.13/depcomp
lrwxrwxrwx 1 root root     35 Jan 15 15:32 install-sh -> /usr/share/automake-1.13/install-sh
-rw-r--r-- 1 root root  23092 Jan 15 15:34 Makefile
-rw-r--r-- 1 root root     78 Jan  5 17:45 Makefile.am
-rw-r--r-- 1 root root  23321 Jan 15 15:32 Makefile.in
lrwxrwxrwx 1 root root     32 Jan 15 15:32 missing -> /usr/share/automake-1.13/missing
-rw-r--r-- 1 root root     23 Jan 15 15:34 stamp-h1
[root@localhost auto_make_test]# ./auto_test 
auto make test
[root@localhost auto_make_test]#

此外还可以执行make的其他操作比如打包、install、uninstall等:
 

[root@localhost auto_make_test]# make dist
make  dist-gzip am__post_remove_distdir='@:'
make[1]: Entering directory `/home/mycode/auto_make_test'
if test -d "auto_test-1.0"; then find "auto_test-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "auto_test-1.0" || { sleep 5 && rm -rf "auto_test-1.0"; }; else :; fi
test -d "auto_test-1.0" || mkdir "auto_test-1.0"
test -n "" \
|| find "auto_test-1.0" -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 /home/mycode/auto_make_test/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "auto_test-1.0"
tardir=auto_test-1.0 && ${TAR-tar} chof - "$tardir" | GZIP=--best gzip -c >auto_test-1.0.tar.gz
make[1]: Leaving directory `/home/mycode/auto_make_test'
if test -d "auto_test-1.0"; then find "auto_test-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "auto_test-1.0" || { sleep 5 && rm -rf "auto_test-1.0"; }; else :; fi
[root@localhost auto_make_test]# ll
total 400
-rw-r--r-- 1 root root  37794 Jan 15 15:30 aclocal.m4
drwxr-xr-x 2 root root   4096 Jan 15 15:32 autom4te.cache
-rw-r--r-- 1 root root      0 Jan 15 15:29 autoscan.log
-rwxr-xr-x 1 root root  11048 Jan 15 15:34 auto_test
-rw-r--r-- 1 root root  71596 Jan 15 15:37 auto_test-1.0.tar.gz
-rw-r--r-- 1 root root     81 Jan  5 17:01 auto_test.c
-rw-r--r-- 1 root root   5984 Jan 15 15:34 auto_test.o
-rw-r--r-- 1 root root    778 Jan 15 15:34 config.h
-rw-r--r-- 1 root root    625 Jan 15 15:31 config.h.in
-rw-r--r-- 1 root root   8546 Jan 15 15:34 config.log
-rwxr-xr-x 1 root root  32482 Jan 15 15:34 config.status

 

autogen.sh实例

之前介绍的automake貌似工序过于复杂,在这里其实是没有必要做这么复杂的工作的,完全可以将其抽象成一个模板性质的脚本将各个工序都集中到脚本里面,使用者只需要稍微修改自己的配置文件即可,直白点就是使用autogen.sh,相信这个脚本在很多的开源代码甚至网上的帖子讨论中都已经泛滥了。在此也使用这个脚本,然后再代码中只需要补充上configure.ac和Makefile.am文件即可。具体流程如下所示:

1.准备文件


[root@localhost ~]# mkdir autogen_test
[root@localhost ~]# cd autogen_test/
[root@localhost autogen_test]# mkdir src 
[root@localhost autogen_test]# cd src/


测试代码:

[root@localhost src]# vim auto_test.c
[root@localhost src]# cat auto_test.c
#include <stdio.h>
 
int main()
{
    printf("auto make test\n");
    return 0;
}


编写源码的Makefile.am

[root@localhost src]# vim Makefile.am
[root@localhost src]# cat Makefile.am 
bin_PROGRAMS=auto_test
auto_test_SOURCES=auto_test.c
auto_test_LDADD=
LIBS=-lm
INCLUDES=-I/usr/include


 编写外层目录Makefile.am

[root@localhost src]# cd ..
[root@localhost autogen_test]# vim Makefile.am 
[root@localhost autogen_test]# cat Makefile.am 
SUBDIRS = src
 
.PHONY: auto_clean
 
auto_clean: distclean
    find . -name Makefile.in -exec rm -f {} \;
    rm -rf autom4te.cache
    rm -f missing aclocal.m4 config.h.in config.guess config.sub ltmain.sh install-sh configure depcomp compile


 编写autogen.sh文件:

[root@localhost autogen_test]# vim autogen.sh 
[root@localhost autogen_test]# cat autogen.sh 
#!/bin/sh
 
echo
echo ... auto_test autogen ...
echo
 
## Check all dependencies are present
MISSING=""
 
# Check for aclocal
env aclocal --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
  ACLOCAL=aclocal
else
  MISSING="$MISSING aclocal"
fi
 
# Check for autoconf
env autoconf --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
  AUTOCONF=autoconf
else
  MISSING="$MISSING autoconf"
fi
 
# Check for autoheader
env autoheader --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
  AUTOHEADER=autoheader
else
  MISSING="$MISSING autoheader"
fi
 
# Check for automake
env automake --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
  AUTOMAKE=automake
else
  MISSING="$MISSING automake"
fi
 
# Check for libtoolize or glibtoolize
env libtoolize --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
  # libtoolize was found, so use it
  TOOL=libtoolize
else
  # libtoolize wasn't found, so check for glibtoolize
  env glibtoolize --version > /dev/null 2>&1
  if [ $? -eq 0 ]; then
    TOOL=glibtoolize
  else
    MISSING="$MISSING libtoolize/glibtoolize"
  fi
fi
 
# Check for tar
env tar -cf /dev/null /dev/null > /dev/null 2>&1
if [ $? -ne 0 ]; then
  MISSING="$MISSING tar"
fi
 
## If dependencies are missing, warn the user and abort
if [ "x$MISSING" != "x" ]; then
  echo "Aborting."
  echo
  echo "The following build tools are missing:"
  echo
  for pkg in $MISSING; do
    echo "  * $pkg"
  done
  echo
  echo "Please install them and try again."
  echo
  exit 1
fi
 
## Do the autogeneration
echo Running ${ACLOCAL}...
$ACLOCAL 
echo Running ${AUTOHEADER}...
$AUTOHEADER
echo Running ${TOOL}...
$TOOL --automake --copy --force
echo Running ${AUTOCONF}...
$AUTOCONF
echo Running ${AUTOMAKE}...
$AUTOMAKE --add-missing --force-missing --copy --foreign
 
# Run autogen in the argp-standalone sub-directory
#echo "Running autogen.sh in argp-standalone ..."
#( cd contrib/argp-standalone;./autogen.sh )
 
# Instruct user on next steps
echo
echo "Please proceed with configuring, compiling, and installing."


编写configure.ac文件

[root@localhost autogen_test]# vim configure.ac 
[root@localhost autogen_test]# cat configure.ac 
#                     -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
 
AC_PREREQ([2.69])
AC_INIT([auto_test],[1.0],[751772517@qq.com])
 
AC_SUBST([PACKAGE_RELEASE],[1.0],[751772517@qq.com])
 
AM_INIT_AUTOMAKE(auto_test,1.0)
AC_CONFIG_SRCDIR([src/auto_test.c])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile
               src/Makefile])
 
# Checks for programs.
AC_PROG_CC
AC_PROG_LIBTOOL
 
# Checks for libraries.
 
# Checks for header files.
AC_CHECK_HEADERS([stddef.h string.h])
 
# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_TYPE_SIZE_T
 
# Checks for library functions.
AC_FUNC_REALLOC
AC_CHECK_FUNCS([memset socket])
 
AC_OUTPUT


文件结构:

[root@localhost autogen_test]# tree
.
|-- autogen.sh
|-- configure.ac
|-- Makefile.am
`-- src
    |-- auto_test.c
    `-- Makefile.am
 
1 directory, 5 files
[root@localhost autogen_test]#

截止到这里所有的文件都已经创建好了,也就是说前期的准备就可以了,后续只需要运行脚本就可以生成Makefile文件了

2.生成Makefile

[root@localhost autogen_test]# ./autogen.sh 
 
... auto_test autogen ...
 
Running aclocal...
Running autoheader...
Running libtoolize...
Running autoconf...
Running automake...
configure.ac:9: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:9: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:17: installing './config.guess'
configure.ac:17: installing './config.sub'
configure.ac:9: installing './install-sh'
configure.ac:9: installing './missing'
src/Makefile.am:5: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
src/Makefile.am: installing './depcomp'
 
Please proceed with configuring, compiling, and installing.


查看文件:

[root@localhost autogen_test]# ll
total 1224
-rw-r--r-- 1 root root 346593 Jan 19 10:47 aclocal.m4
-rwxr-xr-x 1 root root   1986 Jan 18 15:22 autogen.sh
drwxr-xr-x 2 root root   4096 Jan 19 10:47 autom4te.cache
-rwxr-xr-x 1 root root  45297 Jan 19 10:47 config.guess
-rw-r--r-- 1 root root   2278 Jan 19 10:47 config.h.in
-rwxr-xr-x 1 root root  35533 Jan 19 10:47 config.sub
-rwxr-xr-x 1 root root 415594 Jan 19 10:47 configure
-rwxr-xr-x 1 root root    708 Jan 18 16:03 configure.ac
-rwxr-xr-x 1 root root  23566 Jan 19 10:47 depcomp
-rwxr-xr-x 1 root root  13997 Jan 19 10:47 install-sh
-rw-r--r-- 1 root root 283474 Jan 19 10:47 ltmain.sh
-rw-r--r-- 1 root root    229 Jan 19 10:45 Makefile.am
-rw-r--r-- 1 root root  24704 Jan 19 10:47 Makefile.in
-rwxr-xr-x 1 root root   6873 Jan 19 10:47 missing
drwxr-xr-x 2 root root   4096 Jan 19 10:47 src


 执行configure

[root@localhost autogen_test]# ./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 for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking how to print strings... printf
checking for a sed that does not truncate output... /usr/bin/sed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for fgrep... /usr/bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for mt... no
checking if : is a manifest tool... no
checking how to run the C preprocessor... gcc -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 dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... no
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking stddef.h usability... yes
checking stddef.h presence... yes
checking for stddef.h... yes
checking for string.h... (cached) yes
checking for stdbool.h that conforms to C99... yes
checking for _Bool... yes
checking for size_t... yes
checking for stdlib.h... (cached) yes
checking for GNU libc compatible realloc... yes
checking for memset... yes
checking for socket... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands


 查看生成的文件

[root@localhost autogen_test]# ll
total 1644
-rw-r--r-- 1 root root 346593 Jan 19 10:47 aclocal.m4
-rwxr-xr-x 1 root root   1986 Jan 18 15:22 autogen.sh
drwxr-xr-x 2 root root   4096 Jan 19 10:47 autom4te.cache
-rwxr-xr-x 1 root root  45297 Jan 19 10:47 config.guess
-rw-r--r-- 1 root root   2504 Jan 19 10:48 config.h
-rw-r--r-- 1 root root   2278 Jan 19 10:47 config.h.in
-rw-r--r-- 1 root root  25756 Jan 19 10:48 config.log
-rwxr-xr-x 1 root root  61429 Jan 19 10:48 config.status
-rwxr-xr-x 1 root root  35533 Jan 19 10:47 config.sub
-rwxr-xr-x 1 root root 415594 Jan 19 10:47 configure
-rwxr-xr-x 1 root root    708 Jan 18 16:03 configure.ac
-rwxr-xr-x 1 root root  23566 Jan 19 10:47 depcomp
-rwxr-xr-x 1 root root  13997 Jan 19 10:47 install-sh
-rwxr-xr-x 1 root root 292999 Jan 19 10:48 libtool
-rw-r--r-- 1 root root 283474 Jan 19 10:47 ltmain.sh
-rw-r--r-- 1 root root  24843 Jan 19 10:48 Makefile
-rw-r--r-- 1 root root    229 Jan 19 10:45 Makefile.am
-rw-r--r-- 1 root root  24704 Jan 19 10:47 Makefile.in
-rwxr-xr-x 1 root root   6873 Jan 19 10:47 missing
drwxr-xr-x 3 root root   4096 Jan 19 10:48 src
-rw-r--r-- 1 root root     23 Jan 19 10:48 stamp-h1
[root@localhost autogen_test]# 


到这里就生成了Makefile文件,所有的都是直接运行脚本自动生成的。

3.编译 测试

 

[root@localhost autogen_test]# make 
make  all-recursive
make[1]: Entering directory `/home/mycode/autogen_test'
Making all in src
make[2]: Entering directory `/home/mycode/autogen_test/src'
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include    -g -O2 -MT auto_test.o -MD -MP -MF .deps/auto_test.Tpo -c -o auto_test.o auto_test.c
mv -f .deps/auto_test.Tpo .deps/auto_test.Po
/bin/sh ../libtool  --tag=CC   --mode=link gcc  -g -O2   -o auto_test auto_test.o  -lm
libtool: link: gcc -g -O2 -o auto_test auto_test.o  -lm
make[2]: Leaving directory `/home/mycode/autogen_test/src'
make[2]: Entering directory `/home/mycode/autogen_test'
make[2]: Leaving directory `/home/mycode/autogen_test'
make[1]: Leaving directory `/home/mycode/autogen_test'


查看生成的可执行文件:

[root@localhost autogen_test]# ll src/
total 68
-rwxr-xr-x 1 root root 11048 Jan 19 10:49 auto_test
-rw-r--r-- 1 root root    81 Jan 18 15:11 auto_test.c
-rw-r--r-- 1 root root  5992 Jan 19 10:49 auto_test.o
-rw-r--r-- 1 root root 18522 Jan 19 10:48 Makefile
-rw-r--r-- 1 root root   103 Jan 18 15:16 Makefile.am
-rw-r--r-- 1 root root 18875 Jan 19 10:47 Makefile.in


 执行可执行程序:

[root@localhost autogen_test]# ./src/auto_test 
auto make test
执行make auto_clean

[root@localhost autogen_test]# make auto_clean
Making distclean in src
make[1]: Entering directory `/home/mycode/autogen_test/src'
 rm -f auto_test
rm -rf .libs _libs
rm -f *.o
rm -f *.lo
rm -f *.tab.c
test -z "" || rm -f 
test . = "." || test -z "" || rm -f 
rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
rm -rf ./.deps
rm -f Makefile
make[1]: Leaving directory `/home/mycode/autogen_test/src'
make[1]: Entering directory `/home/mycode/autogen_test'
rm -rf .libs _libs
rm -f *.lo
test -z "" || rm -f 
test . = "." || test -z "" || rm -f 
rm -f config.h stamp-h1
rm -f libtool config.lt
rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
rm -f cscope.out cscope.in.out cscope.po.out cscope.files
make[1]: Leaving directory `/home/mycode/autogen_test'
rm -f config.status config.cache config.log configure.lineno config.status.lineno
rm -f Makefile
find . -name Makefile.in -exec rm -f {} \;
rm -rf autom4te.cache
rm -f missing aclocal.m4 config.h.in config.guess config.sub ltmain.sh install-sh configure depcomp compile


 查看清理完毕后的文件:

[root@localhost autogen_test]# tree
.
|-- autogen.sh
|-- configure.ac
|-- Makefile.am
`-- src
    |-- auto_test.c
    `-- Makefile.am
 
1 directory, 5 files
[root@localhost autogen_test]# 


测试成功,makefi文件可以正常使用,这里就可以将其抽象成一个模板性质的东西,也就是说以后要用的时候只需要修改配置文件configure.ac以及src/Makefile.am即可,自己的代码可以任意的填充,当然多目录的也是如此只需要增加相应的目录以及对于的Makefile.am文件即可,在根目录下的Makefile.am把目录名填写进去就可以自动执行脚本扫描目录生成Makefile文件进行编译了,这种用法相对于前文来说工序是一样的只不过是将单个的命令封装到脚本里面了,这样用起来就方便多了。


————————————————
版权声明:本文为CSDN博主「kongslly」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kongshuai19900505/article/details/79104442

 
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值