分享

!!!使用autotools建立Makefile

 quasiceo 2018-05-07
2013年07月22日 16:47:34
阅读数:621

!!!使用autotools建立Makefile

对于一个较大的项目而言,完全手动建立Makefile是一件费力而又容易出错的工作。autotools系列工具只需用户输入简单的目标文件、依赖文件、文件目录等就可以比较轻松地生成Makefile了。现在Linux上的软件开发一般都是用autotools来制作Makefile

autotools工具主要有:aclocalautoscanautoconfautoheaderautomake。使用autotools主要就是利用各个工具的脚本文件来生成最后的Makefile。下面结合实例来介绍具体的流程。

第一步 autoscan

使用autoscan在给定目录及其子目录树中检查源文件,如果没有给出目录,就在当前目录及其子目录树中进行检查。最终生成两个文件:configure.scanautoscan.log

[armlinux@lqm autotools-easy]$ tree
.
|-- main.c
|-- mytool1.c
|-- mytool1.h
|-- mytool2.c
`-- mytool2.h

0 directories, 5 files

执行命令:
# autoscan

[armlinux@lqm autotools-easy]$ tree
.
|-- autoscan.log
|-- configure.scan
|-- main.c
|-- mytool1.c
|-- mytool1.h
|-- mytool2.c
`-- mytool2.h

0 directories, 7 files

 其中,configure.scanconfigure.in的原型文件。而configure.inautoconf的脚本配置文件。所以下一步的工作就是要对configure.scan进行修改,将其转化为configure.in

第二步   autoconf

configure.scan文件内容如下:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.57)
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

说明:

1、以号开始的是行为注释。
2
AC_PREREQ宏声明本文件要求的autoconf版本。
3
AC_INIT宏用来定义软件的名称和版本等信息,这里的BUG-REPORT-ADDRESS可以省略。
4
AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。这个参数一般不需要修改。
5
AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

修改时需要增加一个宏AM_INIT_AUTOMAKE(PACKAGEVERSION),还要把AC_CONFIG_HEADER更改为AM_CONFIG_HEADER。具体如下:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.57)
#AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_INIT(main,1.0)                 #
添加
AM_INIT_AUTOMAKE(main,1.0)    #添加
AC_CONFIG_SRCDIR([main.c])
AM_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(Makefile)    
#添加(Makefile)

执行命令:

#mv configure.scan configure.in

#aclocal

#autoconf

第三步  autoheader

执行命令:

#autoheader

 

首先把configure.scan更改名称为configure.in。然后执行aclocalautoconfautoheader

[armlinux@lqm autotools-easy]$ mv configure.scan configure.in
[armlinux@lqm autotools-easy]$ ls
autoscan.log  configure.
in  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
[armlinux@lqm autotools-easy]$ aclocal
[armlinux@lqm autotools-easy]$ ls
aclocal.m4  autoscan.log  configure.
in  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
[armlinux@lqm autotools-easy]$ autoconf
[armlinux@lqm autotools-easy]$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.
in  main.c  mytool1.c  mytool1.h  mytool2.c  mytool2.h
[armlinux@lqm autotools-easy]$ autoheader

第四步  automake

这是很重要的一步。automake需要的脚本配置文件是Makefile.am,这个文件需要自己建立。

执行命令:

#vi Makefile.am        #编辑添加如下内容:

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=main
main_SOURCES=main.c mytool1.c mytool1.h mytool2.c mytool2.h

[add by lcf:   如果需要添加链接库 添加 一行

如:  LIBS += -lm -lz -lmysqlclient_r]

#automake -–add-missing

 

 AUTOMAKE_OPTIONS为设置automake的选项。automake提供了3种软件等级:foreigngnugnits,让用户选择使用,默认等级是gnu。现在使用的foreign只是检测必要的文件。

bin_PROGRAMS定义了要产生的执行文件名。如果产生多个可执行文件,每个文件名用空格隔开。

file_SOURCES定义file这个执行程序的依赖文件。同样的,对于多个执行文件,那就要定义相应的file_SOURCES

接下来就是使用automake对其生成configure.in文件。这里可以使用选项--adding-missingautomake自动添加一些必要的脚本文件。

第五步  运行configure

执行命令:

#./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... 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
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands

这样就完成了Makefile的制作。

这是具有的功能:makemake installmake uninstallmake cleanmake distcleanmake dist

1、键入make默认执行make all。其目标体为all

[armlinux@lqm autotools-easy]$ make
make  all-am
make[1]: Entering directory `/home/armlinux/program/autotools-easy'
source='main.c' object='main.o' libtool=no 
depfile='.deps/main.Po' tmpdepfile='.deps/main.TPo' 
depmode=gcc3 /bin/sh ./depcomp 
gcc -DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -c `test -f 'main.c' || echo './'`main.c
source='mytool1.c' object='mytool1.o' libtool=no 
depfile='.deps/mytool1.Po' tmpdepfile='.deps/mytool1.TPo' 
depmode=gcc3 /bin/sh ./depcomp 
gcc -DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -c `test -f 'mytool1.c' || echo './'`mytool1.c
source='mytool2.c' object='mytool2.o' libtool=no 
depfile='.deps/mytool2.Po' tmpdepfile='.deps/mytool2.TPo' 
depmode=gcc3 /bin/sh ./depcomp 
gcc -DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -c `test -f 'mytool2.c' || echo './'`mytool2.c
gcc  -g -O2   -o main  main.o mytool1.o mytool2.o  
make[1]: Leaving directory `/home/armlinux/program/autotools-easy'

2make installuninstall

3make clean distclean

make clean仅仅是清除之前编译的可执行文件及配置文件。而make distclean要清除所有生成的文件。

[armlinux@lqm autotools-easy]$ make clean
test -z "main" || rm -f main
rm -f *.o core *.core
[armlinux@lqm autotools-easy]$ make distclean
test -z "main" || rm -f main
rm -f *.o core *.core
rm -f *.tab.c
rm -rf ./.deps
rm -f Makefile 
rm -f config.h stamp-h1
rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH
rm -f config.status config.cache config.log configure.lineno

4make dist

将所有的程序和相关的文档打包为一个压缩文件以供发布。

[armlinux@lqm autotools-easy]$ make dist
{ test ! -d main-1.0 || { find main-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr main-1.0; }; }
mkdir main-1.0
find main-1.0 -type d ! -perm -777 -exec chmod a+rwx {} ; -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/armlinux/program/autotools-easy/install-sh -c -m a+r {} {} ; 
|| chmod -R a+r main-1.0
/bin/sh /home/armlinux/program/autotools-easy/missing --run tar chof - main-1.0 | GZIP=--best gzip -c >main-1.0.tar.gz
{ test ! -d main-1.0 || { find main-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr main-1.0; }; }
[armlinux@lqm autotools-easy]$ ls
aclocal.m4      config.h     config.status  depcomp     main-1.0.tar.gz  Makefile     missing        mytool1.h  mytool2.h
autom4te.cache  config.h.in  configure      install-sh  main.c           Makefile.am  mkinstalldirs  mytool1.o  mytool2.o
autoscan.log    config.log   configure.in   main        main.o           Makefile.in  mytool1.c      mytool2.c  stamp-h1

文章标签: make automake makefile
个人分类: 库 && makefile

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多