Makefile 是用于自动编译和链接的,一个工程文件可以由很多文件组成,每一个文件的改变都会导致工程的重新链接,但是不是所有的文件都需要重新编译,Makefile 中记录有文件的信息,在make时会决定在链接的时候需要重新编译哪些文件。Makefile 的宗旨就是:让编译器知道如果要编译一个文件,那么需要依赖其他的哪些文件。
使用automake,程序员只需要写一些简单的含有预定义宏的文件,由autoconf根据一个宏文件生成configure,由automake根据另一个宏文件生成Makefile.in,再使用configure,依据Makefile.in生成一个符合惯例的Makefile。接下来介绍automake生成Makefile的简单例子。
1、建目录
[root@localhost jetway]# mkdir fuckworld
[root@localhost jetway]# cd fuckworld/
2、编写程序fuckworld.c
[root@localhost fuckworld]# vi fuckworld.c
#include<stdio.h>
int main(int argc,char ** argv)
{
printf("fuck the pool world!!!/n");
return 0;
}
3、生成configure
[root@localhost fuckworld]# autoscan
[root@localhost fuckworld]# ls
autoscan.log configure.scan fuckworld.c
将configure.scan作为configure.in的蓝本,重命名:
mv configure.scan configure.in
修改configure.in里面的内容:
[root@localhost fuckworld]# vim configure.in
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_INIT(fuckworld.c)
AM_INIT_AUTOMAKE(fuckworld,1.0)
# 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)
然后执行命令aclocal和autoconf
[root@localhost fuckworld]# aclocal
[root@localhost fuckworld]# ls
aclocal.m4 autom4te.cache autoscan.log configure.in fuckworld.c
[root@localhost fuckworld]# autoconf
[root@localhost fuckworld]# ls
aclocal.m4 autom4te.cache autoscan.log configure configure.in fuckworld.c
4、新建Makefile.am
[root@localhost fuckworld]# vi Makefile.am
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = fuckworld
fuckworld_SOURCES = fuckworld.c
5、运行automake
[root@localhost fuckworld]# automake --add-missing
6、执行configure生成Makefile
[root@localhost fuckworld]# ./configure
7、使用Makefile编译代码
[root@localhost fuckworld]# make
运行程序
[root@localhost fuckworld]# ./fuckworld
fuck the pool world!!!