ubuntu10.04下,利用automake工具生成Makefile

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

一、利用工具生成Makefile文件的操作步骤如下:
1. 运行autoscan,生成文件autoscan.log (没用) 和 configure.scan。
2. 修改configure.scan文件,改成configure.ac (或configure.in也可以)。
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
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、工程说明:
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$ sudovi /etc/ld.so.conf.d/libc.conf
在此文件中写入你需要用的动态链接库的位置 例如mysql就是/usr/local/mysql/lib/mysql 然后,执行命令就解决了找不到动态链接库的问题。
new@new-desktop:~/linux/renwen$ sudoldconfig
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 -idavid_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 -idavid
new@new-desktop:~/linux/renwen$ renwen
bash: /usr/local/bin/hello: 没有那个文件或目录
至此,关于automake自动生成Makefile已经介绍完了,如果想了解更高级的应用,清参考automake官方文档!!!

原创粉丝点击