autotools自动编译系列之三---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;
}
[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
[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
[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."

[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],[[email protected]])

AC_SUBST([PACKAGE_RELEASE],[1.0],[[email protected]])

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
[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
[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文件进行编译了,这种用法相对于前文来说工序是一样的只不过是将单个的命令封装到脚本里面了,这样用起来就方便多了。



猜你喜欢

转载自blog.csdn.net/kongshuai19900505/article/details/79104442