Makefile介绍
通过源码编译和安装软件时,只需要简单的进行./configure;make;make install;就可以,但是如果按照makefile的格式要求,完全写出一个自己的makefile往往很难,幸好gnu提供了一系列的工具来帮我们快速的生成Makefile,帮我们在大项目中管理各个子模块,它们就是autoconf和automake。
autoscan是用来扫描源代码目录生成configure.scan文件的。autoscan可以用目录名做为参数,但如果你不使用参数的话,那么autoscan将认为使用的是当前目录。autoscan将扫描你所指定目录中的源文件,并创建configure.scan文件。
aclocal是一个perl 脚本程序。aclocal根据configure.in文件的内容,自动生成aclocal.m4文件。aclocal的定义是:“aclocal – create aclocal.m4 by scanning configure.ac”。
autoconf是用来产生configure文件的。configure是一个脚本,它能设置源程序来适应各种不同的操作系统平台,并且根据不同的系统来产生合适的Makefile,从而可以使你的源代码能在不同的操作系统平台上被编译出来。
我们使用automake –add-missing来产生Makefile.in。选项–add-missing的定义是“add missing standard files to package”,它会让automake加入一个标准的软件包所必须的一些文件。我们用automake产生出来的Makefile.in文件是符合GNU Makefile惯例的,接下来我们只要执行configure这个shell 脚本就可以产生合适的 Makefile 文件了。
- configure.scan:通过autoscan命令来帮助我们根据目录下的源代码生成一个configure.scan的模板文件
- configure.in:修改scan文件,内容是一些宏定义,配合autoconf能够生成configure文件
- aclocal.m4:要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4
- configure:经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本,用来检查环境并生成Makefile
- Makefile.am:是一种比Makefile更高层次的规则。只需指定要生成什么目标,它由什么源文件生成,要安装到什么目录等构成,用来生成Makefile.in
- Makefile.in:由Makefile.am生成的一些基本的定义,符合GNU的规范,方便configure根据平台不同产生不同的Makefile文件
- Makefile:该文件用来描述源程序之间的相互关系并自动维护编译工作和后续的安装卸载等
因此整个makefile生成过程如下:
首先我们有一个helloworld的c程序:
#include <stdio.h> int main(int argc, char** argv){ printf("%s", "Hello,World!\n"); return 0; }
1.使用autoscan命令来帮助我们根据目录下的源代码生成一个configure.scan的 模板文件
2.将configure.scan 文件重命名为configure.in,并修改configure.in文件内容:
# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_INIT(helloworld.c) AM_INIT_AUTOMAKE(helloworld, 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)
3.然后执行命令aclocal和autoconf,分别会产生aclocal.m4及configure两个文件
[kongjian@v132172.sqa.cm4 helloworld]$ ls configure.in helloworld.c [kongjian@v132172.sqa.cm4 helloworld]$ aclocal [kongjian@v132172.sqa.cm4 helloworld]$ ls aclocal.m4 autom4te.cache configure.in helloworld.c [kongjian@v132172.sqa.cm4 helloworld]$ autoconf [kongjian@v132172.sqa.cm4 helloworld]$ ls aclocal.m4 autom4te.cache configure configure.in helloworld.c
4.新建Makefile.am文件,内容如下:
AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=helloworld helloworld_SOURCES=helloworld.c
automake会根据你写的 Makefile.am来自动生成Makefile.in,Makefile.am中定义的宏和目标,会指导automake生成指定的代码.例如,宏bin_PROGRAMS指定编译和连接的目标
5.automake会根据Makefile.am文件产生一些文件,包含最重要的 Makefile.in
6.至此,configure已经生成,可以按照./configure;make;make install;的顺序编译安装了。
[kongjian@v132172.sqa.cm4 helloworld]$ ./configure checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /bin/mkdir -p checking for gawk... gawk checking whether make sets $(MAKE)... yes checking for gcc... gcc 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 gcc accepts -g... yes checking for gcc option to accept ISO C89... 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: executing depfiles commands [kongjian@v132172.sqa.cm4 helloworld]$ make gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"helloworld\" -DVERSION=\"1.0\" -I. -g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo -c -o helloworld.o helloworld.c mv -f .deps/helloworld.Tpo .deps/helloworld.Po gcc -g -O2 -o helloworld helloworld.o [kongjian@v132172.sqa.cm4 helloworld]$ ./helloworld Hello,World!