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
[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
[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
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 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@localhost auto_make_test]# ll
total 304
-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 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]# 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
-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]#