autoconf和automake可以掃描源代碼文件,自動生成configure和Makefile文件,可以減少一些重複勞動.設想做個簡單測試,在源代碼目錄下有2個目錄,include和main,分別是頭文件和源代碼,預想達到的目標是用autoconf工具集生成Makefile,運行make命令后能夠生成可執行目標文件.
目錄結構如下:
.
|-- include
| `-- stu.h
`-- main
`-- stu_list.c
include/stu.h:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#ifndef __STUDENT_H_
#define __STUDENT_H_
struct student {
int no;
char name[32];
};
#endif
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
main/stu_list.c:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <stdio.h>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
下面是生成Makefile的步驟:
1) 運行autoscan命令
2) 將configure.scan 文件重命名為configure.in,並修改configure.in文件
3) 在當前目錄下新建Makefile.am文件,並在main目錄下也新建makefile.am文件 4) 在當前目錄下新建NEWS、 README、 ChangeLog 、AUTHORS文件
5) 將/usr/share/automake-1.X/目錄下的depcomp和complie文件拷貝到本目錄下
6) 運行aclocal命令
7) 運行autoconf命令
8) 運行autoheader命令
9) 運行automake -a命令
10) 運行./confiugre腳本
configure.in要修改的內容包括:
將AC_CONFIG_HEADER([config.h])修改為:AM_CONFIG_HEADER(config.h), 並加入AM_INIT_AUTOMAKE(mytest,1.0).在AC_OUTPUT輸入要創建的Makefile文件名.
修改後的configure.in:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65])
AC_INIT(mytest, 1.0, xxxx@hotmail.com)
AC_CONFIG_SRCDIR([include/stu.h])
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(mytest, 1.0)
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT(Makefile main/Makefile)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Makefile.am:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
SUBDIRS = main
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
main/Makefile.am:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
noinst_PROGRAMS = mytest
mytest_SOURCES = stu_list.c
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
運行make命令,輸出:
make all-recursive
make[1]: 正在進入目錄 `~/st/autoconf/project/src'
Making all in main
make[2]: 正在進入目錄 `~/st/autoconf/project/src/main'
gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT stu_list.o -MD -MP -MF .deps/stu_list.Tpo -c -o stu_list.o stu_list.c
運行main目錄下的mytest,輸出:
-- 1 --> aaa
-- 2 --> bbb
[火星人 ] 用autoconf和automake生成Makefile文件已經有601次圍觀