先按照一个简单的步骤实现一个automake的helloworld例子:
1, 创建一个实验路径
mkdir amhelloworld && cd amhelloworld
2, 创建一个src目录
mkdir -p src
3, 创建一个简单的helloworld.c程序源文件
vim amhelloworld.c
#include <stdio.h>
#ifdef WITH_LOG4C
#include <log4c.h>
#endif
int main(int argc, char * argv [])
{
#ifdef ENABLE_LOG4C
PutLog("");
#else
printf("Hello Automake world.\n");
#endif
return 0;
}
注意:这里故意设置了一个宏,WITH_LOG4C,决定是否使用log4c库来打印日志。
这样的话,可以在配置的时候,检测用户是否指定了log4c外部库。
如果指定了,可以通过configure.ac设置cflags和ldflags等,指定-DWITH_LOG4C以及 -I./log4c/ -L./log4c/ -llog4c等等。
4, 回到amhelloworld目录,
cd .. && autoscan
5, 修改configure.scan
mv configure.scan configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.68])
AC_INIT([amhelloworld], [1.0.0.1], [author@amhelloworld.com])
AC_CONFIG_SRCDIR([src/amhelloworld.c])
# AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE()
# 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_CONFIG_FILES(Makefile
src/Makefile)
AC_OUTPUT
6, 运行aclocal
aclocal是一个工具,用于扫描configure.ac里面的am宏,生成aclocal.m4宏文件
http://www.gnu.org/software/m4/
m4是一个宏处理程序,autoconf工具集使用m4作为宏处理系统。
宏的工作方式是替换,学过c语言都知道。一个简短单词,可以处理一堆的繁琐预编译操作。
m4在autoconf工具集里面的角色就是把m4宏替换成bash脚本,用于检查,设置,输出等操作。
m4的核心是AC_DEFUN()宏,用于定义新的函数。
7, 运行autoconf生成configure脚本
autoconf
8, 编写Makefile.am
vim Makefile.am
SUBDIRS=src
vim src/Makefile.am
bin_PROGRAMS=amhelloworld
amhelloworld_SOURCES=amhelloworld.c
9, 运行automake生成Makefile.in
automake -ac
可能需要补足一些辅助文件
touch README AUTHORS INSTALL COPYING NEWS ChangeLog
10, 运行configure生成Makefile
./configure --prefix=`pwd`/distdir
11, make
make && make install && make dist
以上就是基本步骤。