Linux开发的同学都有类似的经验,随着项目的推进,文件越来越多,每次编译都很繁琐,简单的还可以通过Makefile搞定,在复杂一点就很不爽了,在最近的项目开发的过程中遇到了这个问题,为了方便以后查询,也方便其他同学,先记录下automake的配置过程,以及中途遇到的问题。我的平台是:Centos 6.2
原创:http://blog.youkuaiyun.com/kevin_aixin/article/details/7564563
整个过程很简单,只需要autoscan、aclocal、autoconf、autoheader、automaker --add-missing、最后configure,make,make install
这个过程成中需要配置的文件很少,因此只要简单的配置就可以生成一个非常专业的安装程序。下面一步步介绍需要注意的事项
准备工作:
在/root/project/main目录下创建一个文件main.c,其内容如下:
------------------------------------------------
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Hello, Auto Makefile!\n");
return 0;
}
------------------------------------------------
此时状态如下:
[root@localhost main]# pwd
/root/project/main
[root@localhost main]# ls
main.c
[root@localhost main]#
1、autoscan
这一步之前一定要清理工程中的多余文件,特别是带有main函数的测试文件
运行 autoscan , 自动创建两个文件: autoscan.log configure.scan
此时状态如下:
[root@localhost main]# autoscan
[root@localhost main]# ls
autoscan.log configure.scan main.c
[root@localhost main]#
2、aclocal这一步之前需要对上一步生成的configure.scan进行修改,首先重命名为configure.in,然后做如下修改,
查看configure.in的内容:
------------------------------------------------
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([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
------------------------------------------------
解读以上的文件:
------------------------------------------------
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
# AC_PREREQ:
# 确保使用的是足够新的Autoconf版本。如果用于创建configure的Autoconf的版
# 本比version 要早,就在标准错误输出打印一条错误消息并不会创建configure。
AC_PREREQ(2.61)
#
# 初始化,定义软件的基本信息,包括设置包的全称,版本号以及报告BUG时需要用的邮箱地址
#
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
#
# 用来侦测所指定的源码文件是否存在,来确定源码目录的有效性
#
AC_CONFIG_SRCDIR([main.c])
#
# 用于生成config.h文件,以便autoheader使用
#
AC_CONFIG_HEADER([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.
#
# 创建输出文件。在`configure.in'的末尾调用本宏一次。
#
AC_OUTPUT
------------------------------------------------
修改动作:
1.修改AC_INIT里面的参数: AC_INIT(main,1.0, pgpxc@163.com)
2.添加宏AM_INIT_AUTOMAKE, 它是automake所必备的宏,也同前面一样,PACKAGE是所要产生软件套件的名称,VERSION是版本编号。
3.在AC_OUTPUT后添加输出文件Makefile
修改后的结果:
------------------------------------------------
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT(main, 1.0, pgpxc@163.com)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(main,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])
------------------------------------------------
万事具备只欠东风,运行 aclocal, 生成一个“aclocal.m4”文件和一个缓冲文件夹autom4te.cache,该文件主要处理本地的宏定义。
此时的状态是:
[root@localhost main]# aclocal
[root@localhost main]# ls
aclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ main.c
[root@localhost main]#
3、autoconf
这一步没有什么号修改的,直接运行即可
运行 autoconf, 目的是生成 configure
此时的状态是:
[root@localhost main]# autoconf
[root@localhost main]# ls
aclocal.m4 autoscan.log configure.in main.c
autom4te.cache configure configure.in~
[root@localhost main]#
4、autoheader
运行 autoheader,它负责生成config.h.in文件。该工具通常会从“acconfig.h”文件中复制用户附加的符号定义,因此此处没有附加符号定义,所以不需要创建“acconfig.h”文件。
此时的状态是:
[root@localhost main]# autoheader
[root@localhost main]# ls
aclocal.m4 autoscan.log configure configure.in~
autom4te.cache config.h.in configure.in main.c
[root@localhost main]#
5、automake
在运行automake之前需要创建一个 Makefile.am.这一步是创建Makefile很重要的一步,automake要用的脚本配置文件是Makefile.am,用户需要自己创建相应的文件。之后,automake工具转换成Makefile.in。
这个Makefile.am的内容如下:
------------------------------------------------
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=main.c
------------------------------------------------
下面对该脚本文件的对应项进行解释。
其中的AUTOMAKE_OPTIONS为设置automake的选项。由于GNU(在第1章中已经有所介绍)对自己发布的软件有严格的规范,比如必须附 带许可证声明文件COPYING等,否则automake执行时会报错。automake提供了三种软件等级:foreign、gnu和gnits,让用 户选择采用,默认等级为gnu。在本例使用foreign等级,它只检测必须的文件。
bin_PROGRAMS定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。
main_SOURCES定义“main”这个执行程序所需要的原始文件。如果”main”这个程序是由多个原始文件所产生的,则必须把它所用到的所有原 始文件都列出来,并用空格隔开。例如:若目标体“main”需要“main.c”、“sunq.c”、“main.h”三个依赖文件,则定义 main_SOURCES=main.c sunq.c main.h。要注意的是,如果要定义多个执行文件,则对每个执行程序都要定义相应的file_SOURCES。
其次,使用automake对其生成“configure.in”文件,在这里使用选项“—adding-missing”可以让automake自动添加有一些必需的脚本文件。
------------------------------------------------
[root@localhost main]# automake --add-missing
configure.in:8: installing `./missing'
configure.in:8: installing `./install-sh'
Makefile.am: installing `./depcomp'
[root@localhost main]# ls
aclocal.m4 config.h.in configure.in~ main.c Makefile.in
autom4te.cache configure depcomp Makefile.am missing
autoscan.log configure.in install-sh Makefile.am~
[root@localhost main]#
------------------------------------------------
这一步之后就离成功不远了
6、configure
运行configure,在这一步中,通过运行自动配置设置文件configure,把Makefile.in变成了最终的Makefile。
运行的结果如下:
------------------------------------------------
[root@localhost main]# ./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... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
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
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
[root@localhost main]# ls
aclocal.m4 config.h.in configure.in main.c Makefile.in
autom4te.cache config.log configure.in~ Makefile missing
autoscan.log config.status depcomp Makefile.am stamp-h1
config.h configure install-sh Makefile.am~
[root@localhost main]#
------------------------------------------------
7、make
运行 make,对配置文件Makefile进行测试一下
------------------------------------------------
[root@localhost main]# make
cd . && /bin/sh /root/project/main/missing --run aclocal-1.10
cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign
cd . && /bin/sh /root/project/main/missing --run autoconf
/bin/sh ./config.status --recheck
running CONFIG_SHELL=/bin/sh /bin/sh ./configure --no-create --no-recursion
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
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
configure: creating ./config.status
/bin/sh ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
cd . && /bin/sh /root/project/main/missing --run autoheader
rm -f stamp-h1
touch config.h.in
make all-am
make[1]: Entering directory `/root/project/main'
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -g -O2 -o main main.o
cd . && /bin/sh ./config.status config.h
config.status: creating config.h
config.status: config.h is unchanged
make[1]: Leaving directory `/root/project/main'
[root@localhost main]# ls
aclocal.m4 autoscan.log config.h.in config.status configure.in depcomp main main.o Makefile.am Makefile.in stamp-h1
autom4te.cache config.h config.log configure configure.in~ install-sh main.c Makefile Makefile.am~ missing
[root@localhost main]#
------------------------------------------------
8、make install
最后,执行make install,将程序注册到系统,以后运行程序就可以直接main了。
9、注意事项:
学习的过程成中遇到了很多问题,项目中加入mysql以后,只采用如此简单的默认配置是不够的,研究了很多文档以后,学到了一点简单的配置方式,可以应对多数的库加载问题,在configure过程中可以很酷的加入类似--with-mysql=/usr/local/mysql的配置信息,要实现这个功能只需要配置两个文件,configure.in和Makefile.am,如下是我项目中的一个配置文件:
configure.in文件;
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.63])
AC_INIT([drawguess], [1.0], [aixin_86@126.com])
AC_CONFIG_SRCDIR([drawguess.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(drawguess, 1.0)
default_find_path=/usr
AC_ARG_WITH([libev], [AS_HELP_STRING([--with-libev=DIR],[Where to find libev])],, [withval=$default_find_path])
if test "$withval" != "no" ; then
AC_MSG_CHECKING([for libev installation])
for dir in "." $withval/include ; do
if test -f "$dir/ev.h" ; then
CFLAGS="$CFLAGS -I$dir"
ev_include="ok"
fi
done
for dir in "." $withval/lib ; do
if test -f "$dir/libev.so" ; then
EV_LIBS="$dir/libev.so"
ev_lib="ok"
fi
done
if test "$ev_include" == "ok" -a "$ev_lib" == "ok" ; then
AC_MSG_RESULT([yes])
else
AC_MSG_RESULT([no])
AC_MSG_ERROR([libev not found])
fi
fi
AC_SUBST(EV_LIBS)
AC_ARG_WITH([mysql], [AS_HELP_STRING([--with-mysql=DIR],[Where to find mysql])],, [withval=$default_find_path])
if test "$withval" != "no" ; then
AC_MSG_CHECKING([for mysql installation])
for dir in "." $withval/include ; do
if test -f "$dir/mysql.h" ; then
CFLAGS="$CFLAGS -I$dir"
mysql_include="ok"
fi
done
for dir in "." $withval/lib ; do
if test -f "$dir/libmysqlclient.so" ; then
MYSQL_LIBS="$dir/libmysqlclient.so"
mysql_lib="ok"
fi
done
if test "$mysql_include" == "ok" -a "$mysql_lib" == "ok" ; then
AC_MSG_RESULT([yes])
else
AC_MSG_RESULT([no])
AC_MSG_ERROR([mysql not found])
fi
fi
AC_SUBST(MYSQL_LIBS)
AC_ARG_WITH([log4c], [AS_HELP_STRING([--with-log4c=DIR],[Where to find log4c])],, [withval=$default_find_path])
if test "$withval" != "no" ; then
AC_MSG_CHECKING([for log4c installation])
for dir in "." $withval/include ; do
if test -f "$dir/log4c.h" ; then
CFLAGS="$CFLAGS -I$dir"
log4c_include="ok"
fi
done
for dir in "." $withval/lib ; do
if test -f "$dir/liblog4c.so" ; then
LOG4C_LIBS="$dir/liblog4c.so"
log4c_lib="ok"
fi
done
if test "$log4c_include" == "ok" -a "$log4c_lib" == "ok" ; then
AC_MSG_RESULT([yes])
else
AC_MSG_RESULT([no])
AC_MSG_ERROR([log4c not found])
fi
fi
AC_SUBST(LOG4C_LIBS)
AC_SUBST(CFLAGS)
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# FIXME: Replace `main' with a function in `-lev':
AC_CHECK_LIB([ev], [main])
# FIXME: Replace `main' with a function in `-lmysqlclient':
AC_CHECK_LIB([mysqlclient], [main])
# FIXME: Replace `main' with a function in `-lpthread':
AC_CHECK_LIB([pthread], [main])
# FIXME: Replace `main' with a function in `-llog4c':
AC_CHECK_LIB([log4c], [main])
# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h fcntl.h malloc.h netinet/in.h stdlib.h string.h sys/socket.h unistd.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_FUNC_MALLOC
AC_CHECK_FUNCS([bzero inet_ntoa memset mkdir socket strerror])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
Makefile.am文件:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=drawguess
drawguess_SOURCES=configs.h hash_room.c mysql_k.c room_state.h conn_queue.c drawguess.c hash_room.h mysql_k.h conn_queue.h drawguess.h room_state.c log4c_kevin.c log4c_kevin.h
drawguess_LDADD= $(MYSQL_LIBS) $(EV_LIBS) $(LOG4C_LIBS)
drawguess_LDFLAGS=$(CFLAGS)
注意:
这两个文件是相互关联的,在configure.in中定义了MYSQL_LIBS、EV_LIBS、LOG4C_LIBS和CFLAGS,在Makefile.am中调用