您的位置:首页 > 其它

使用automake自动生成makefile(上)

2013-08-29 18:31 357 查看

利用Linux下自动生成makefile的工具: automake, autoconf 生成makefile的一般过程

1. 创建工程目录和各个目录下的makefile.am。工程的名字一般和最终生成应用程序的名字相同。

简单的介绍如何使用automake自动生成Makefile文件



[cpp]
view plaincopy

wzb@embedded ~]$ mkdir workspace
[wzb@embedded ~]$ cd workspace/
[wzb@embedded workspace]$ ls
[wzb@embedded workspace]$ mkdir testAutoMake
[wzb@embedded workspace]$ ls
testAutoMake
[wzb@embedded workspace]$ cd testAutoMake/
[wzb@embedded testAutoMake]$ ls
[wzb@embedded testAutoMake]$ mkdir src
[wzb@embedded testAutoMake]$ ls
src
[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
src
[wzb@embedded testAutoMake]$ vi makefile.am
[wzb@embedded testAutoMake]$ cd src
[wzb@embedded src]$ ls
[wzb@embedded src]$ vi main.c
[wzb@embedded src]$ ls
main.c
[wzb@embedded src]$ vi main.c
[wzb@embedded src]$ clear
[wzb@embedded src]$ ls
main.c
[wzb@embedded src]$ vi makefile.am
[wzb@embedded src]$ clear
[wzb@embedded src]$ ls
main.c makefile.am
[wzb@embedded src]$ cd ..
[wzb@embedded testAutoMake]$ ls
makefile.am src
[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls -R
.:
makefile.am src

./src:
main.c makefile.am
[wzb@embedded testAutoMake]$

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
makefile.am src
[wzb@embedded testAutoMake]$ cat makefile.am
SUBDIRS=src
[wzb@embedded testAutoMake]$ vi makefile.am
[wzb@embedded testAutoMake]$ cd src
[wzb@embedded src]$ ls
main.c makefile.am
[wzb@embedded src]$ cat main.c
#include <stdio.h>
#include <stdarg.h>

int accumulate(int nr, ...);

int main(int argc, char *argv[]) {
int n = 5;

int result = accumulate(5, 5, 4, 3, 2, 1);
printf("%d,\n", result);


printf("test.......makefile ........ok\n");

return 0;
}


int accumulate(int nr, ...) {
int i = 0;
int result = 0;

va_list arg ;

va_start(arg, nr);
for(i=0; i<nr; i++) {
result += va_arg(arg, int);
}
va_end(arg);

return result;


}
[wzb@embedded src]$ pwd
/home/wzb/workspace/testAutoMake/src
[wzb@embedded src]$ cat makefile.am
bin_PROGRAMS=testAutoMake
testAutoMake_SOURCES=main.c
[wzb@embedded src]$



2.通过命令autoscan,创建autoconf的模板,生成configure.in 文件。
首先在工程目录中,通过执行autoscan命令,生成autoconf的模板文件configure.scan 文件,将其改名为configure.in。

需要两处修改:

(1). 将configure.in文件中的语句:AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)中的

FULL-PACKAGE-NAME: 替换为你指派的开发工程包的名字:如本工程的:test_AutoMake;

VERSION: 开发的版本号,一般格式:主版本号.从版本号。 如 0.1;

BUG-REPORT-ADDRESS: 提交bug的邮件地址: 如wzb12008@sina.com。

(2). 还要添加一句: AM_INIT_AUTOMAKE(testAutoMake, 0.1) 即AM_INIT_AUTOMAKE(package_name, version);

用来初始化automake。

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
makefile.am src
[wzb@embedded testAutoMake]$ autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[wzb@embedded testAutoMake]$ ls
autoscan.log configure.scan makefile.am src
[wzb@embedded testAutoMake]$ vi configure.scan
[wzb@embedded testAutoMake]$ vi configure.scan
[wzb@embedded testAutoMake]$ mv configure.scan configure.in
[wzb@embedded testAutoMake]$ ls
autoscan.log configure.in makefile.am src
[wzb@embedded testAutoMake]$ cat configure.in
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_HEADER_STDC

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([makefile
src/makefile])
AC_OUTPUT
[wzb@embedded testAutoMake]$ vi configure.in
[wzb@embedded testAutoMake]$ clear
[wzb@embedded testAutoMake]$ ls
autoscan.log configure.in makefile.am src
[wzb@embedded testAutoMake]$ cat configure.in
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(testAutoMake, 0.1, wzb12008@sina.com)
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADER([config.h])

AM_INIT_AUTOMAKE(testAutoMake, 0.1)

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_HEADER_STDC

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([makefile
src/makefile])
AC_OUTPUT
[wzb@embedded testAutoMake]$

3. 使用aclocal, 将configure.in中宏展开,生成alocal.m4 和临时加速文件autom4te.cache。

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
autoscan.log configure.in makefile.am src
[wzb@embedded testAutoMake]$ aclocal
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autom4te.cache autoscan.log configure.in makefile.am src
[wzb@embedded testAutoMake]$
[wzb@embedded testAutoMake]$

4. 使用autoheader 生成config.h.in 文件。

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autom4te.cache autoscan.log configure.in makefile.am src
[wzb@embedded testAutoMake]$ autoheader
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log configure.in src
autom4te.cache config.h.in makefile.am
[wzb@embedded testAutoMake]$

5. 创建文件 :README 、NEWS、AUTHORS、ChangeLog。touch README NEWS AUTHORS ChangeLog



[html]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log configure.in src
autom4te.cache config.h.in makefile.am
[wzb@embedded testAutoMake]$ touch NEWS README AUTHORS ChangeLog
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autom4te.cache ChangeLog configure.in NEWS src
AUTHORS autoscan.log config.h.in makefile.am README
[wzb@embedded testAutoMake]$ vi ChangeLog
[wzb@embedded testAutoMake]$ cat ChangeLog
2012-04-07 Earl wzb12008@sina.com
* Created
[wzb@embedded testAutoMake]$

6. 执行automake -a 命令生成makefile.in 文件以及其他相关文件 install-sh, missing, depcomp, INSTALL , COPYING等。

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autom4te.cache ChangeLog configure.in NEWS src
AUTHORS autoscan.log config.h.in makefile.am README
[wzb@embedded testAutoMake]$ automake -a
configure.in: installing `./install-sh'
configure.in: installing `./missing'
src/makefile.am: installing `./depcomp'
makefile.am: installing `./INSTALL'
makefile.am: installing `./COPYING'
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log configure.in INSTALL makefile.in README
AUTHORS ChangeLog COPYING install-sh missing src
autom4te.cache config.h.in depcomp makefile.am NEWS
[wzb@embedded testAutoMake]$

7. 执行 autoconf命令,生成configure脚本。

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log configure.in INSTALL makefile.in README
AUTHORS ChangeLog COPYING install-sh missing src
autom4te.cache config.h.in depcomp makefile.am NEWS
[wzb@embedded testAutoMake]$ autoconf
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log configure depcomp makefile.am NEWS
AUTHORS ChangeLog configure.in INSTALL makefile.in README
autom4te.cache config.h.in COPYING install-sh missing src
[wzb@embedded testAutoMake]$

8.执行./configure 生成 makefile 文件。

configure脚本的两个参数,-prefix 指定bin目录的父目录;另一个-host 指定处理器架构,交叉编译环境搭建时有用。

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log configure depcomp makefile.am NEWS
AUTHORS ChangeLog configure.in INSTALL makefile.in README
autom4te.cache config.h.in COPYING install-sh missing src
[wzb@embedded testAutoMake]$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
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 ANSI C... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for egrep... grep -E
checking for ANSI C header files... yes
configure: creating ./config.status
config.status: creating makefile
config.status: creating src/makefile
config.status: creating config.h
config.status: executing depfiles commands
[wzb@embedded testAutoMake]$ ls
aclocal.m4 ChangeLog config.status depcomp makefile.am README
AUTHORS config.h configure INSTALL makefile.in src
autom4te.cache config.h.in configure.in install-sh missing stamp-h1
autoscan.log config.log COPYING makefile NEWS
[wzb@embedded testAutoMake]$

9. 一下步骤基本与我们从源码编译安装应用的步骤相似。
首先,./configure -prefix bin目录所在的父目录(绝对路径)

接着 make ; 编译

最后 make install; 在bin目录中找到生成可执行程序测试。

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autom4te.cache ChangeLog config.h.in config.status configure.in depcomp install-sh makefile.am missing README stamp-h1
AUTHORS autoscan.log config.h config.log configure COPYING INSTALL makefile makefile.in NEWS src
[wzb@embedded testAutoMake]$ ./configure -prefix /home/wzb/w
workspace/ wzb/
[wzb@embedded testAutoMake]$ ./configure -prefix /home/wzb/workspace/testAutoMake/
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
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 ANSI C... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for egrep... grep -E
checking for ANSI C header files... yes
configure: creating ./config.status
config.status: creating makefile
config.status: creating src/makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
[wzb@embedded testAutoMake]$ make
make all-recursive
make[1]: Entering directory `/home/wzb/workspace/testAutoMake'
Making all in src
make[2]: Entering directory `/home/wzb/workspace/testAutoMake/src'
if gcc -DH***E_CONFIG_H -I. -I. -I.. -g -O2 -MT main.o -MD -MP -MF ".deps/main.Tpo" -c -o main.o main.c; \
then mv -f ".deps/main.Tpo" ".deps/main.Po"; else rm -f ".deps/main.Tpo"; exit 1; fi
gcc -g -O2 -o testAutoMake main.o
make[2]: Leaving directory `/home/wzb/workspace/testAutoMake/src'
make[2]: Entering directory `/home/wzb/workspace/testAutoMake'
make[2]: Leaving directory `/home/wzb/workspace/testAutoMake'
make[1]: Leaving directory `/home/wzb/workspace/testAutoMake'
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autom4te.cache ChangeLog config.h.in config.status configure.in depcomp install-sh makefile.am missing README stamp-h1
AUTHORS autoscan.log config.h config.log configure COPYING INSTALL makefile makefile.in NEWS src
[wzb@embedded testAutoMake]$ make install
Making install in src
make[1]: Entering directory `/home/wzb/workspace/testAutoMake/src'
make[2]: Entering directory `/home/wzb/workspace/testAutoMake/src'
test -z "/home/wzb/workspace/testAutoMake//bin" || mkdir -p -- "/home/wzb/workspace/testAutoMake//bin"
/usr/bin/install -c 'testAutoMake' '/home/wzb/workspace/testAutoMake//bin/testAutoMake'
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/home/wzb/workspace/testAutoMake/src'
make[1]: Leaving directory `/home/wzb/workspace/testAutoMake/src'
make[1]: Entering directory `/home/wzb/workspace/testAutoMake'
make[2]: Entering directory `/home/wzb/workspace/testAutoMake'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/home/wzb/workspace/testAutoMake'
make[1]: Leaving directory `/home/wzb/workspace/testAutoMake'
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log config.h config.status COPYING install-sh makefile.in README
AUTHORS bin config.h.in configure depcomp makefile missing src
autom4te.cache ChangeLog config.log configure.in INSTALL makefile.am NEWS stamp-h1
[wzb@embedded testAutoMake]$ ./bin/testAutoMake
15,
test.......makefile ........ok
[wzb@embedded testAutoMake]$

10. 当程序稳定了,可以通过命令 make dist , 生成工程的发布文件。(还有 make distcheck命令)

[cpp]
view plaincopy

[wzb@embedded testAutoMake]$ pwd
/home/wzb/workspace/testAutoMake
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log config.h config.status COPYING install-sh makefile.in README
AUTHORS bin config.h.in configure depcomp makefile missing src
autom4te.cache ChangeLog config.log configure.in INSTALL makefile.am NEWS stamp-h1
[wzb@embedded testAutoMake]$ make dist
{ test ! -d testAutoMake-0.1 || { find testAutoMake-0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr testAutoMake-0.1; }; }
mkdir testAutoMake-0.1
list='src'; for subdir in $list; do \
if test "$subdir" = .; then :; else \
test -d "testAutoMake-0.1/$subdir" \
|| mkdir -p -- "testAutoMake-0.1/$subdir" \
|| exit 1; \
distdir=`CDPATH="${ZSH_VERSION+.}:" && cd testAutoMake-0.1 && pwd`; \
top_distdir=`CDPATH="${ZSH_VERSION+.}:" && cd testAutoMake-0.1 && pwd`; \
(cd $subdir && \
make \
top_distdir="$top_distdir" \
distdir="$distdir/$subdir" \
distdir) \
|| exit 1; \
fi; \
done
make[1]: Entering directory `/home/wzb/workspace/testAutoMake/src'
make[1]: Leaving directory `/home/wzb/workspace/testAutoMake/src'
find testAutoMake-0.1 -type d ! -perm -755 -exec chmod a+rwx,go+rx {} \; -o \
! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
! -type d ! -perm -400 -exec chmod a+r {} \; -o \
! -type d ! -perm -444 -exec /bin/sh /home/wzb/workspace/testAutoMake/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r testAutoMake-0.1
tardir=testAutoMake-0.1 && /bin/sh /home/wzb/workspace/testAutoMake/missing --run tar chof - "$tardir" | GZIP=--best gzip -c >testAutoMake-0.1.tar.gz
{ test ! -d testAutoMake-0.1 || { find testAutoMake-0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr testAutoMake-0.1; }; }
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log config.h config.status COPYING install-sh makefile.in README testAutoMake-0.1.tar.gz
AUTHORS bin config.h.in configure depcomp makefile missing src
autom4te.cache ChangeLog config.log configure.in INSTALL makefile.am NEWS stamp-h1
[wzb@embedded testAutoMake]$ clear
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log config.h config.status COPYING install-sh makefile.in README testAutoMake-0.1.tar.gz
AUTHORS bin config.h.in configure depcomp makefile missing src
autom4te.cache ChangeLog config.log configure.in INSTALL makefile.am NEWS stamp-h1
[wzb@embedded testAutoMake]$ clear
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log config.h config.status COPYING install-sh makefile.in README testAutoMake-0.1.tar.gz
AUTHORS bin config.h.in configure depcomp makefile missing src
autom4te.cache ChangeLog config.log configure.in INSTALL makefile.am NEWS stamp-h1
[wzb@embedded testAutoMake]$ vi ChangeLog
[wzb@embedded testAutoMake]$ clear
[wzb@embedded testAutoMake]$ ls
aclocal.m4 autoscan.log config.h config.status COPYING install-sh makefile.in README testAutoMake-0.1.tar.gz
AUTHORS bin config.h.in configure depcomp makefile missing src
autom4te.cache ChangeLog config.log configure.in INSTALL makefile.am NEWS stamp-h1
[wzb@embedded testAutoMake]$ vi configure.in

以上是是使用automake生成makefile的全过程,

想到了为什么,安装源码文件时,要先 ./configure -prefix ... , 接着make, 接着make install 。很清楚了。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: