automake和autoconf自动生成Makefile

开发软件项目的源代码往往放在不同的文件里,使用make来编译节省了大量的时间,提高了效率。因为源码有改动的地方可以只对改动的地方重新编译,重新连接。make要调用一个Makefile文件来实现,因此 Makefile的编写是使用make的关键问题。当工程里面包含很多源文件,且源文件放在不同的子目录时,手动书写Makefile文件十分不方便且容易出错。这种情况下,我们通常用automake和autoconf工具来自动生成Makefile文件。

一、利用工具生成Makefile文件的操作步骤如下:
1. 运行autoscan,生成文件autoscan.log (没用) 和 configure.scan。
2. 修改configure.scan文件,改成configure.ac (或configure.in也可以,是老板本autoconf支持的)。
3. 运行autoheader,生成文件config.h.in。(configure.ac中有宏AC_CONFIG_HEADERS()时才可以用)
4. 运行aclocal,生成aclocal.m4文件。
5. 运行autoconf,生成configure(该步需要aclocal.m4和configure.ac文件,或config.h.in)。
6. 编写Makefile.am文件,每一个有源代码的目录或子目录都需要有一个Makefile.am文件。
7. 运行automake,生成Makefile.in文件(该步骤需要Makefile.am和configure.ac,或config.h.in)
8. 运行./configure,生成Makefile文件。
9. 运行make,生成中间文件,库文件,可执行文件。
10. 运行make install,相应的可执行文件,库文件,头文件会拷贝好相应的位置。
11. 运行make uninstall,把目标文件中系统中卸载。
12. 运行make dist,将程序和相关文档打包成一个压缩文件来发布。
13. 运行make distcheck,生成发布软件包并对其进行测试检查,以确定发布包的正确性。
ps:如果configure.ac中有宏AC_CONFIG_HEADERS([config.h])时,那么在用到configure.ac文件时,configure会调用config.h.in。

二、针对上述步骤中的命令,再做一下详细的介绍:
1. autoscan
autoscan是用来扫描源代码目录生成configure.scan文件的。autoscan可以用目录名做为参数,如果不使用参数的话,autoscan认为使用的是当前目录。autoscan扫描你所指定目录中的源文件,并创建configure.scan文件。
2. configure.scan
configure.scan包含了系统配置的基本选项里面都是 一些宏定义.我们需要将它改名为configure.ac。
3. autoheader
autoheader工具根据系统给定的文件“accconfig.h” 生成config.h.in 文件。
4. aclocal
aclocal是一个perl 脚本程序aclocal根据configure.ac 文件的内容自动生成aclocal.m4文件
5. autoconf
autoconf 是用来生成自动配置软件源代码脚本(configure)的工具。要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4。autoconf从configure.in这个列举编译软件时所需要各种参数的模板文件中创建configure。autoconf需要GNU m4宏处理器来处理aclocal.m4生成configure脚本。 m4是一个宏处理器,将输入拷贝到输出同时将宏展开。宏可以是内嵌的也可以是用户定义的。除了可以展开宏,m4还有一些内建的函数用来引用文件整数运算文本操作循环等。m4既可以作为编译器的前端也可以单独作为一个宏处理器。
6. Makefile.am
Makefile.am是用来生成Makefile.in的需要你手工书写.Makefile.am中定义了一些内容:
a) AUTOMAKE_OPTIONS
   automake的选项在执行automake时它会检查目录下是否存在标准GNU软件包中应具备的各种文件例如AUTHORSChangeLogNEWS等文件我们将其设置成foreign时automake会改用一般软件包的标准来检查
b) bin_PROGRAMS
  指定所要产生的可执行文件的文件名如果要产生多个可执行文件那么在各个名字间用空格隔开
c)hello _SOURCES
  指定产生"hello"时所需要的源代码如果它用到了多个源文件那么使用空格符号将它们隔开如需要hello.h,hello.c那么写成hello_SOURCES= hello.h hello.c   如果在 bin_PROGRAMS定义了多个可执行文件,则对应每个可执行文件都要定义相对的filename_SOURCES
Makefile.am的一般格式:

 Makefile.am的变量:

 Makefile.am的路径变量:

 
7. automake
使用automake --add-missing来产生Makefile.in选项--add-missing的定义是 "add missing standard files to package"它会让automake加入一个标准的软件包所必须的 一些文件用automake产生出来的 Makefile.in文件是符合GNU Makefile惯例的接下来只要执行configure这个shell 脚本就可以产生合适的 Makefile 文件

三、下面给一个生成Makefile的简单例子:
1、编译环境在Ubuntu10.04上,安装autoconf工具
输入命令:sudo apt-get install autoconf
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会安装下列额外的软件包:
  automake autotools-dev m4
建议安装的软件包:
  autoconf2.13 autoconf-archive gnu-standards autoconf-doc libtool gettext
下列【新】软件包将被安装:
   autoconf automake autotools-dev m4
升级了 0 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 243 个软件包未被升级。
需要下载 1,730kB 的软件包。
解压缩后会消耗掉 5,005kB 的额外空间。
您希望继续执行吗?[Y/n]
输入Y后,就安装了autoconf和automake工具。
2、建立一个源文件,命令如下:
new@new-desktop:~/linux$ mkdir renwen
new@new-desktop:~/linux$ cd renwen
new@new-desktop:~/linux/renwen$ vi renwen.c, 内容:
#include<stdio.h>
int main(int argc, char* argv [])
{
printf("Hello,Renwen0524!!!\n");
return 0;
}
3、使用autoscan工具,产生configure.ac
new@new-desktop:~/linux/renwen$ autoscan
new@new-desktop:~/linux/renwen$ ls
autoscan.log  configure.scan  renwen.c
new@new-desktop:~/linux/renwen$ mv configure.scan configure.ac
new@new-desktop:~/linux/renwen$ vi configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65]) //autoconf版本号
AC_INIT([renwen], [1.0], [Renwen0524@163.com]) //软件名称,软件版本,bug报告地址
AM_INIT_AUTOMAKE(renwen,1.0)//automake使用的宏,自己必须添加
AC_CONFIG_SRCDIR([renwen.c]) //查看源码文件
AC_CONFIG_HEADERS([config.h]) //产生config.h文件,以便autoheader使用
# 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) //设定configure所产生的文件名
4、使用autoheader工具生成config.h.in
new@new-desktop:~/linux/renwen$ autoheader
new@new-desktop:~/linux/renwen$ ls
autom4te.cache  autoscan.log   config.h.in  configure.ac  renwen.c
5、使用aclocal工具来产生aclocal.m4
new@new-desktop:~/linux/renwen$ aclocal
new@new-desktop:~/linux/renwen$ ls
aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure.ac  renwen.c
6、使用autoconf工具生成configure
new@new-desktop:~/linux/renwen$ autoconf
new@new-desktop:~/linux/renwen$ ls
aclocal.m4      autoscan.log   configure   renwen.c autom4te.cache  config.h.in   configure.ac
7、建立Makefile.am文件
new@new-desktop:~/linux/renwen$ vi Makefile.am
AUTOMAKE_OPTIONS = foreign //软件等级
bin_PROGRAMS = renwen //可执行文件名字
renwen_SOURCES = renwen.c //产生可执行文件的源文件
8、使用automake工具生成Makfile.in
new@new-desktop:~/linux/renwen$ automake --add-missing
new@new-desktop:~/linux/renwen$ ls
aclocal.m4      autoscan.log  configure     Makefile.am  renwen.c
autom4te.cache  config.h.in   configure.ac   Makefile.in
9、进行配置
new@new-desktop:~/linux/renwen$ ./configure
new@new-desktop:~/linux/renwen$ ls
aclocal.m4       config.h     config.status  Makefile     renwen.c
autom4te.cache  config.h.in  configure      Makefile.am   stamp-h1
autoscan.log     config.log   configure.ac   Makefile.in
10、生成可执行文件,并运行
new@new-desktop:~/linux/renwen$ make
new@new-desktop:~/linux/renwen$ ./renwen
Hello,Renwen0524!!!
11、把目标文件安装到系统中,并运行
new@new-desktop:~/linux/renwen$ sudo make install
new@new-desktop:~/linux/renwen$ renwen
Hello,Renwen0524!!!
11、把目标文件充系统中卸载
new@new-desktop:~/linux/renwen$ sudomake uninstall
new@new-desktop:~/linux/renwen$ renwen
renwen:找不到命令
12、将程序和相关文件打包成一个压缩文档以供发布
new@new-desktop:~/linux/renwen$ make dist
new@new-desktop:~/linux/renwen$ ls
aclocal.m4      config.h.in~    depcomp      missing            stamp-h1
autom4te.cache  config.log      install-sh   renwen
autoscan.log    config.status  Makefile      renwen-1.0.tar.gz
config.h        configure      Makefile.am  renwen.c
config.h.in     configure.ac   Makefile.in  renwen.o
13、linux下编译renwen-1.0.tar.gz的步骤
new@new-desktop:~/linux/renwen$ tar xzvf renwen-1.0.tar.gz
new@new-desktop:~/linux/renwen$ cd renwen-1.0
new@new-desktop:~/linux/renwen/renwen-1.0$ ./configure 
[new@localhostrenwen]#make
[new@localhostrenwen]# sudomake install
[new@localhostrenwen]#renwen
Hello,Renwen0524!!!
[new@localhostrenwen]# sudomake unstall
[new@localhostrenwen]#make clean
ps: ./configure时不指定--prefix可执行文件默认放在/usr /local/bin库文件默认放在/usr/local/lib配置文件默认放在/usr/local/etc其它的资源文件放在/usr /local/share要卸载这个程序要么在原来的make目录下用一次make uninstall(前提是make文件指定过uninstall)要么去上述目录里面把相关的文件一个个手工删掉指定prefix直接删掉一个文件夹就够了

四、下面给一个生成Makefile的复杂例子(带有多级目录和配置文件):
1、工程说明:
renwen是工程的顶级目录,conf内是配置文件,include内是头文件,src内有三个目录,main是主程序,first做静态链接库,second做动态链接库。
renwen
|— — — — conf
                | — — librenwen.conf
|— — — — include
                |— — first.h
                |— — second.h
|— — — — src
                |— — main
|— — main.c
                |— — first
|— — first.c
                |— — second
|— — second.c
2、源文件清单:
*********renwen/conf/librenwen.conf************
/usr/local/lib
********* renwen/ include/first.h************
#include <stdio.h>
void first(void);
********* renwen/ include/second.h************
#include <stdio.h>
void second(void);
********* renwen/ src/main/main.c************
#include <stdio.h>
#include "first.h"
#include "second.h"
int main(int argc, char* argv[] )
{
printf("Hello, boys(:>!!!\n");
first();
second();
return 0;
}
********* renwen/ src/first/first.c************
#include "first.h"
void first(void)
{
printf("Renwen01,first!!!\n");
}
*********renwen/src/second/second.c************
#include "second.h"
void second(void)
{
printf("Renwen02,second!!!\n");
}
3、configure.ac文件清单
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65])
AC_INIT([david], [1.0], [Renwen0524@163.com])
AM_INIT_AUTOMAKE(david,1.0.0,Renwen0524@163.com)
AC_CONFIG_SRCDIR([include/first.h])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

#静态连接库
#AC_PROG_RANLIB
#动态链接库
AC_PROG_LIBTOOL

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT( [Makefile
                 conf/Makefile
                 include/Makefile
                 src/first/Makefile
                 src/main/Makefile
                 src/second/Makefile])
3、Makefile.am文件清单
********* renwen/ Makefile.am************
AUTOMAKE_OPTIONS = foreign
SUBDIRS = include src/first src/second src/main conf
********* renwen/ conf/Makefile.am************
configdir = /etc/ld.so.conf.d                 //这个数据文件将要移动到的目录
config_DATA = libdavid.conf               //数据文件名,如有多个文件则这样写config_DATA = test1.dat test2.dat
********* renwen/ include/ Makefile.am ************
helloincludedir=$(includedir)
helloinclude_HEADERS=first.h second.h
********* renwen/ src/main/ Makefile.am ************
bin_PROGRAMS=renwen
renwen_SOURCES=main.c
renwen_LDADD=$(top_srcdir)/src/first/libfirst.a
LIBS=-lsecond
INCLUDES=-I$(top_srcdir)/include
renwen_LDFLAGS=-L$(top_srcdir)/src/second
********* renwen/ src/first/ Makefile.am ************
noinst_LIBRARIES=libfirst.a
libfirst_a_SOURCES=first.c
INCLUDES=-I$(top_srcdir)/include
********* renwen/ src/second/ Makefile.am ************
lib_LTLIBRARIES=libsecond.la
libsecond_la_SOURCES=second.c
INCLUDES=-I$(top_srcdir)/include
4、安装新的工具
由于使用了动态链接库,所以需要安装libtool:
new@new-desktop:~/linux/renwen$ sudo apt-get install libtool
有时在执行autoconf时,会出现:

configure.ac:13: error: possibly undefined macro: AC_PROG_LIBTOOL

      If this token and others are legitimate, please use m4_pattern_allow.

      See the Autoconf documentation.

需要安装 libsysfs-dev,之后问题解决。
new@new-desktop:~/linux/renwen$ sudo apt-get install libsysfs-dev
5、生成Makefile的步骤:
new@new-desktop:~/linux/renwen$ autoheader
new@new-desktop:~/linux/renwen$ aclocal
new@new-desktop:~/linux/renwen$ libtoolize (如果没有动态链接库,该步可以省略)
new@new-desktop:~/linux/renwen$ autoconf
new@new-desktop:~/linux/renwen$ automake --add-missing
new@new-desktop:~/linux/renwen$ ./configure
6、运行生成的程序
new@new-desktop:~/linux/renwen$ make
new@new-desktop:~/linux/renwen$ ./src/main/renwen
Hello, boys(:>!!!
Renwen01,first!!!
Renwen02,second!!!
6、把目标文件安装到系统中
new@new-desktop:~/linux/renwen$ sudo make install
有时提示找不到动态链接文件,按照以下方式去修改  
new@new-desktop:~/linux/renwen$ sudo vi /etc/ld.so.conf.d/libc.conf
在此文件中写入你需要用的动态链接库的位置 例如mysql就是/usr/local/mysql/lib/mysql 然后,执行命令就解决了找不到动态链接库的问题。
new@new-desktop:~/linux/renwen$ sudo ldconfig
6、构建deb二进制软件包,使用checkinstall工具
new@new-desktop:~/linux/renwen$ sudo apt-get install checkinstall
new@new-desktop:~/linux/renwen$ sudo checkinstall -D make install
这样目录下就生成了david_1.0.0-1_amd64.deb包
7、安装deb二进制软件包
new@new-desktop:~/linux/renwen$ sudo dpkg -i david_1.0.0-1_amd64.deb
new@new-desktop:~/linux/renwen$ renwen
Hello, boys(:>!!!
Renwen01,first!!!
Renwen02,second!!!
8、卸载deb二进制软件包
new@new-desktop:~/linux/renwen$ sudo dpkg -i david
new@new-desktop:~/linux/renwen$ renwen
bash: /usr/local/bin/hello: 没有那个文件或目录



原文地址:

1、http://renwen0524.blog.163.com/blog/static/73019455201162109367/

2、http://renwen0524.blog.163.com/blog/static/7301945520116373647540/

3、http://renwen0524.blog.163.com/blog/static/7301945520116424235732/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值