歡迎您光臨本站 註冊首頁

用autoconf和automake生成Makefile文件

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

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>


#include <stdlib.h>
#include "include/stu.h"

int main(int argc, char* argv[])
{
unsigned int i;
struct student stus[] = {
{ 1, "aaa" },
{ 2, "bbb" }
};

for (i = 0; i < sizeof(stus)/sizeof(stus[0]); i ) {
printf("-- % 3d --> %sn", stus[i].no, stus[i].name);
}

exit(0);
}

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

下面是生成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


mv -f .deps/stu_list.Tpo .deps/stu_list.Po
gcc -g -O2 -o mytest stu_list.o
make[2]:正在離開目錄 `~/st/autoconf/project/src/main'
make[2]: 正在進入目錄 `~/st/autoconf/project/src'
make[2]: 沒有什麼可以做的為 `all-am'.
make[2]:正在離開目錄 `~/st/autoconf/project/src'
make[1]:正在離開目錄 `~/st/autoconf/project/src'

運行main目錄下的mytest,輸出:

-- 1 --> aaa
-- 2 --> bbb


[火星人 ] 用autoconf和automake生成Makefile文件已經有601次圍觀

http://coctec.com/docs/linux/show-post-48594.html