[分享]autotools自动生成Makefile与打包

编译打包和其他
回复
wyylling
帖子: 51
注册时间: 2008-01-01 22:30

[分享]autotools自动生成Makefile与打包

#1

帖子 wyylling » 2008-08-27 15:52

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

autotools工具主要有:aclocal、autoscan、autoconf、autoheader、automake。使用autotools主要就是利用各个工具的脚本文件来生成最后的Makefile。下面结合实例来介绍具体的流程。
源文件如下:
#include <gtkmm-2.4/gtkmm.h>

using namespace std;

int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Main::run(window);
return 0;
}

第一步 autoscan

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

wyylling@wyylling-laptop:~/program/main$ tree
.
|-- Makefile.am
|-- main.cpp
`-- makefile_backup

0 directories, 3 files
wyylling@wyylling-laptop:~/program/main$ autoscan
wyylling@wyylling-laptop:~/program/main$ tree
.
|-- Makefile.am
|-- autoscan.log
|-- configure.scan
|-- main.cpp
`-- makefile_backup

0 directories, 5 files


其中,configure.scan是configure.in的原型文件。而configure.in是autoconf的脚本配置文件。所以下一步的工作就是要对configure.scan进行修改,将其转化为configure.in。
wyylling@wyylling-laptop:~/program/main$ mv configure.scan configure.in
wyylling@wyylling-laptop:~/program/main$ vi configure.in


第二步 autoconf

configure.in文件内容如下:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.61)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([main.cpp])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])
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(PACKAGE,VERSION),还要把AC_CONFIG_HEADER更改为AM_CONFIG_HEADER(该句有问题?ubuntu8.10不需要更改为AM)。具体如下:

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

AC_PREREQ(2.61)
AC_INIT(main, 1.0, wyylling@126.com)
AM_INIT_AUTOMAKE(main,1.0)
AC_CONFIG_SRCDIR([main.cpp])
#AC_CONFIG_HEADER([config.h])
AM_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CXX

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])
AC_OUTPUT


第三步 autoheader
wyylling@wyylling-laptop:~/program/main$ aclocal
wyylling@wyylling-laptop:~/program/main$ autoconf
wyylling@wyylling-laptop:~/program/main$ autoheader


第四步 automake
这是很重要的一步。automake需要的脚本配置文件是Makefile.am,这个文件需要自己建立。
其内容如下:
AUTOMAKE_OPTIONS = foreign
INCLUDES = `pkg-config --cflags gtkmm-2.4`
LIBS = `pkg-config --libs gtkmm-2.4`
bin_PROGRAMS = main
main_SOURCES = main.cpp

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

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

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

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

wyylling@wyylling-laptop:~/program/main$ automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'


生成Makefile.in文件

wyylling@wyylling-laptop:~/program/main$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking for g++... g++
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 g++ accepts -g... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

Makefile文件出现了。
wyylling@wyylling-laptop:~/program/main$ ls
aclocal.m4 config.log depcomp Makefile stamp-h1
autom4te.cache config.status install-sh Makefile.am
autoscan.log configure main makefile_backup
config.h configure.in main.cpp Makefile.in
config.h.in configure.in~ main.o missing

这样就完成了Makefile的制作。这是具有的功能:make、make install、make uninstall、make clean、make distclean、make dist。

1、键入make默认执行make all。其目标体为all。
wyylling@wyylling-laptop:~/program/main$ make
cd . && /bin/bash /home/wyylling/program/main/missing --run autoheader
rm -f stamp-h1
touch config.h.in
cd . && /bin/bash ./config.status config.h
config.status: creating config.h
config.status: config.h is unchanged
make all-am
make[1]: 正在进入目录 `/home/wyylling/program/main'
if g++ -DHAVE_CONFIG_H -I. -I. -I. `pkg-config --cflags gtkmm-2.4` -I /usr/include/gtkmm-2.4 -g -O2 -MT main.o -MD -MP -MF ".deps/main.Tpo" -c -o main.o main.cpp; \
then mv -f ".deps/main.Tpo" ".deps/main.Po"; else rm -f ".deps/main.Tpo"; exit 1; fi
g++ -g -O2 -L /usr/lib/gtkmm-2.4 -o main main.o `pkg-config --libs gtkmm-2.4`
make[1]:正在离开目录 `/home/wyylling/program/main'

2、make install(uninstall)

3、make clean (distclean)

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

4、make dist

将所有的程序和相关的文档打包为一个压缩文件以供发布。
上次由 wyylling 在 2008-11-25 9:21,总共编辑 28 次。
头像
aeonhuang
帖子: 115
注册时间: 2008-06-01 22:55
来自: 广东
联系:

#2

帖子 aeonhuang » 2008-08-27 16:36

学习了,多谢分享!
The word aeon, also spelled eon or æon, means "age", "forever" or "for eternity".
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

#3

帖子 HuntXu » 2008-08-27 17:03

mark~顺手转到编译版吧...
HUNT Unfortunately No Talent...
头像
dwl301
帖子: 1130
注册时间: 2007-04-14 11:17

Re: [分享]autotools自动生成Makefile与打包

#4

帖子 dwl301 » 2009-11-20 10:51

OK了&……谢谢分享
tianyukuan
帖子: 1
注册时间: 2011-01-17 12:30

Re: [分享]autotools自动生成Makefile与打包

#5

帖子 tianyukuan » 2011-01-17 13:05

有没有讲解这些宏、关键字的书籍阿,
或者从那里可以得到他们作用阿

AC_CONFIG_SRCDIR([main.cpp])

AUTOMAKE_OPTIONS = foreign
INCLUDES = `pkg-config --cflags gtkmm-2.4`
LIBS = `pkg-config --libs gtkmm-2.4`
bin_PROGRAMS = main
main_SOURCES = main.cpp
回复