Linux工具之autoconf和automake


1 autoscan — 在 hello.c所在目录完成扫描

文件长生如下文件
autoscan.log
┗ configure.scan
┗ mv configure.scan configure.ac
┗ edit configure,ac

修改 configure.ac的内容

  1. # -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.
  3. AC_PREREQ([2.69])
  4. AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
  5. # add it
  6. AM_INIT_AUTOMAKE
  7. #change it
  8. AC_CONFIG_SRCDIR(hello.c)
  9. AC_CONFIG_HEADERS([config.h])
  10. # Checks for programs.
  11. AC_PROG_CC
  12. # Checks for libraries.
  13. # Checks for header files.
  14. # Checks for typedefs, structures, and compiler characteristics.
  15. # Checks for library functions.
  16. # add it
  17. AC_CONFIG_FILES([Makefile])
  18. AC_OUTPUT

2 makefile.am — 创建添加如下信息

  1. AUTOMAKE_OPTIONS=foreign
  2. bin_PROGRAMS=helloworld
  3. helloworld_SOURCES=hello.c

┗ binPROGRAMS 指定可执行输出文件
┗ helloworld
SOURCES 制定输入的原文件

3 执行aclocal

├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── requests
│ └── traces.0

4 执行autoheader | with sudo

├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── config.h.in

:::warning autom4te: cannot open autom4te.cache/requests: Permission denied
autoheader: ‘/usr/bin/autom4te’ failed with exit status: 1
————————————————————————————————————————————————
sudo XXX :::

5 执行autoconf | with sudo

├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├── config.h.in
├── configure
├── configure.ac
├── main.c
└── Makefile.am

执行automake —add-missing | with sudo

├── aclocal.m4
├── autom4te.cache
│ ├── output.0
│ ├── output.1
│ ├── requests
│ ├── traces.0
│ └── traces.1
├── autoscan.log
├──
compile -> /usr/share/automake-1.16/compile
├── config.h.in
├── configure
├── configure.ac
├──
depcomp -> /usr/share/automake-1.16/depcomp
├──
install-sh -> /usr/share/automake-1.16/install-sh
├── main.c
├── Makefile.am
├──
Makefile.in
└──
missing -> /usr/share/automake-1.16/missing**


经过以上操作我们就可以像一般安装程序一config —> make —> make install

C   | autogen工作流程梳理 - 图1

安装


  1. ./configure
  2. make
  3. make install
  4. /usr/bin/install -c helloworld '/usr/local/bin'
  5. 这样直接可以直接执行,不用带上绝对路径


autogen.sh 登场


  1. #!/bin/sh
  2. echo
  3. echo ... helloworld autogen ...
  4. echo
  5. MISSING=""
  6. env aclocal --version >/dev/null 2>&1
  7. if [ $? -eq 0 ];then
  8. ACLOCAL=aclocal
  9. else
  10. MISSING="$MISSING aclocal"
  11. fi
  12. env autoconf --version > /dev/null 2>&1
  13. if [ $? -eq 0 ];then
  14. AUTOCONF=autoconf
  15. else
  16. MISSING="$MISSING autoconf"
  17. fi
  18. env autoheader --version > /dev/null 2>&1
  19. if [ $? -eq 0 ]; then
  20. AUTOHEADER=autoheader
  21. else
  22. MISSING="$MISSING autoconf"
  23. fi
  24. env automake --version > /dev/null 2>&1
  25. if [ $? -eq 0 ];then
  26. AUTOMAKE=automake
  27. else
  28. MISSING="$MISSING automake"
  29. fi
  30. env libtoolize --version > /dev/null 2>&1
  31. if [ $? -eq 0 ];then
  32. TOOL=libtoolize
  33. else
  34. env glibtoolize --version > /dev/null 2>&1
  35. if [$? -eq 0 ];then
  36. TOOL=glibtoolize
  37. else
  38. MISSING="$MISSING libtoolize/glibtoolize"
  39. fi
  40. fi
  41. env tar -cf /dev/null /dev/null > /dev/null 2>&1
  42. if [ $? -ne 0 ];then
  43. MISSING="$MISSING tar"
  44. fi
  45. if [ "x$MISSING" != "x" ];then
  46. echo "ABORTING."
  47. echo
  48. echo "The following build tools are missing:"
  49. echo
  50. for pkg in $MISSING;do
  51. echo " * $pkg"
  52. done
  53. echo
  54. echo "please install them and try again."
  55. echo
  56. fi
  57. echo Running $ACLOCAL
  58. $ACLOCAL
  59. echo Running $AUTOHEADER
  60. $AUTOHEADER
  61. echo Running $TOOL
  62. $TOOL
  63. echo Running $AUTOCONF
  64. $AUTOCONF
  65. echo Running $AUTOMAKE
  66. $AUTOMAKE --add-missing --force-missing --copy --foreign
  67. echo
  68. echo "Please proceed with configuring, compiling and installing"

目录结构

├── autogen.sh
├── configure.ac
├── Makefile.am
└── src
├── hello.c
└── Makefile.am
根目录的configure ac

  1. # -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.
  3. AC_PREREQ([2.69])
  4. AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
  5. AC_CONFIG_SRCDIR([src/hello.c])
  6. AC_CONFIG_HEADERS([config.h])
  7. # ● add
  8. AM_INIT_AUTOMAKE(helloworld,1.0)
  9. # ● add
  10. AC_CONFIG_FILES([Makefile
  11. src/Makefile])
  12. # Checks for programs.
  13. AC_PROG_CC
  14. # Checks for libraries.
  15. # Checks for header files.
  16. # Checks for typedefs, structures, and compiler characteristics.
  17. # Checks for library functions.
  18. AC_OUTPUT


根目录的Makefile.am

  1. SUBDIRS = src
  2. #ACLOCAL_AMFLAGS = -I m4
  3. .PHONY:clean
  4. clean: distclean
  5. find . -name Makefile.in -exec rm -rf {} \;
  6. rm -rf autom4te.cache
  7. rm -f aclocal.m4 autoscan.log config.h config.log config.sub configure depcomp Makefile compile config.h.in config.status install-sh missing stamp-h1 config.guess config.h.in~ ltmain.sh

src目录下的Makefile.am

  1. AUTOMAKE_OPTIONS=foreign
  2. bin_PROGRAMS=helloworld
  3. helloworld_SOURCES=hello.c
  4. helloworld_LDADD=
  5. LIBS=-lm
  6. INCLUDE=-I/usr/include

:::warning AC_CONFIG_FILES([Makefile
src/Makefile])
要让Makefile.am遍地开花 :::

参考文献


Linux工具之autoconf和automake
例解 autoconf 和 automake 生成 Makefile 文件