Linux工具之autoconf和automake
1 autoscan — 在 hello.c所在目录完成扫描
文件长生如下文件
┗ autoscan.log
┗ configure.scan
┗ mv configure.scan configure.ac
┗ edit configure,ac
修改 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])
# add it
AM_INIT_AUTOMAKE
#change it
AC_CONFIG_SRCDIR(hello.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.
# add it
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
2 makefile.am — 创建添加如下信息
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=hello.c
┗ binPROGRAMS 指定可执行输出文件
┗ helloworldSOURCES 制定输入的原文件
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
安装
./configure
make
make install
┗ /usr/bin/install -c helloworld '/usr/local/bin'
这样直接可以直接执行,不用带上绝对路径
autogen.sh 登场
#!/bin/sh
echo
echo ... helloworld autogen ...
echo
MISSING=""
env aclocal --version >/dev/null 2>&1
if [ $? -eq 0 ];then
ACLOCAL=aclocal
else
MISSING="$MISSING aclocal"
fi
env autoconf --version > /dev/null 2>&1
if [ $? -eq 0 ];then
AUTOCONF=autoconf
else
MISSING="$MISSING autoconf"
fi
env autoheader --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
AUTOHEADER=autoheader
else
MISSING="$MISSING autoconf"
fi
env automake --version > /dev/null 2>&1
if [ $? -eq 0 ];then
AUTOMAKE=automake
else
MISSING="$MISSING automake"
fi
env libtoolize --version > /dev/null 2>&1
if [ $? -eq 0 ];then
TOOL=libtoolize
else
env glibtoolize --version > /dev/null 2>&1
if [$? -eq 0 ];then
TOOL=glibtoolize
else
MISSING="$MISSING libtoolize/glibtoolize"
fi
fi
env tar -cf /dev/null /dev/null > /dev/null 2>&1
if [ $? -ne 0 ];then
MISSING="$MISSING tar"
fi
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
fi
echo Running $ACLOCAL
$ACLOCAL
echo Running $AUTOHEADER
$AUTOHEADER
echo Running $TOOL
$TOOL
echo Running $AUTOCONF
$AUTOCONF
echo Running $AUTOMAKE
$AUTOMAKE --add-missing --force-missing --copy --foreign
echo
echo "Please proceed with configuring, compiling and installing"
目录结构
├── autogen.sh
├── configure.ac
├── Makefile.am
└── src
├── hello.c
└── Makefile.am
根目录的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([src/hello.c])
AC_CONFIG_HEADERS([config.h])
# ● add
AM_INIT_AUTOMAKE(helloworld,1.0)
# ● add
AC_CONFIG_FILES([Makefile
src/Makefile])
# 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.am
SUBDIRS = src
#ACLOCAL_AMFLAGS = -I m4
.PHONY:clean
clean: distclean
find . -name Makefile.in -exec rm -rf {} \;
rm -rf autom4te.cache
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
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=hello.c
helloworld_LDADD=
LIBS=-lm
INCLUDE=-I/usr/include
:::warning
AC_CONFIG_FILES([Makefile
src/Makefile])
要让Makefile.am遍地开花
:::
参考文献
Linux工具之autoconf和automake
例解 autoconf 和 automake 生成 Makefile 文件