Makefile自动生成 autoconf 和 automake

从helloworld 入手


sudo apt-get install autoconf


我们从大家最常使用的例子程序helloworld 开始。

下面的过程如果简单地说来就是:

新建三个文件:

helloworld.c
configure.in
Makefile.am

然后执行:

aclocal; autoconf; automake --add-missing; ./configure; make; ./helloworld

就可以看到Makefile 被产生出来,而且可以将helloworld.c 编译通过。

很简单吧,几条命令就可以做出一个符合惯例的Makefile ,感觉如何呀。

现在开始介绍详细的过程:

1 、建目录

在你的工作目录下建一个helloworld 目录,我们用它来存放helloworld 程序及相关文件,如在/home/my/build 下:

$ mkdir hello
$ cd hello

2 、 hello.c

然后用你自己最喜欢的编辑器写一个hello.c 文件,如命令:vi hello.c 。使用下面的代码作为hello.c 的内容。

int main(int argc, char** argv)
{
printf("Hello, Linux World!\n");
return 0;
}

完成后保存退出。

现在在hello 目录下就应该有一个你自己写的hello.c 了。

3 、生成configure

我们使用autoscan 命令来帮助我们根据目录下的源代码生成一个configure.in 的模板文件。

命令:

$ autoscan
$ ls
configure.scan helloworld.c

执行后在hellowrold 目录下会生成一个文件:configure.scan ,我们可以拿它作为configure.in 的蓝本。

现在将configure.scan 改名为configure.in ,并且编辑它,按下面的内容修改,去掉无关的语句:

============================configure.in 内容开始=========================================
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT(helloworld.c)
AM_INIT_AUTOMAKE(hello, 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)
============================configure.in 内容结束=========================================

然后执行命令aclocal 和autoconf ,分别会产生aclocal.m4 及configure 两个文件:

$ aclocal
$ls
aclocal.m4 configure.in helloworld.c
$ autoconf
$ ls
aclocal.m4 autom4te.cache configure configure.in helloworld.c


大家可以看到configure.in 内容是一些宏定义,这些宏经autoconf 处理后会变成检查系统特性、环境变量、软件必须的参数的shell 脚本。

autoconf 是用来生成自动配置软件源代码脚本(configure )的工具。configure 脚本能独立于autoconf 运行,且在运行的过程中,不需要用户的干预。

要生成configure 文件,你必须告诉autoconf 如何找到你所用的宏。方式是使用aclocal 程序来生成你的aclocal.m4 。

aclocal 根据configure.in 文件的内容,自动生成aclocal.m4 文件。aclocal 是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac” 。

autoconf 从configure.in 这个列举编译软件时所需要各种参数的模板文件中创建configure 。

autoconf 需要GNU m4 宏处理器来处理aclocal.m4 ,生成configure 脚本。

m4 是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏,m4 还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,循环等。m4 既可以作为编译器的前端,也可以单独作为一个宏处理器。

4  新建Makefile.am

新建Makefile.am 文件,命令:


$ vi Makefile.am


内容如下:


AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
ee_SOURCES=hello.c


automake 会根据你写的Makefile.am 来自动生成Makefile.in 。

Makefile.am 中定义的宏和目标, 会指导automake 生成指定的代码。例如,宏bin_PROGRAMS 将导致编译和连接的目标被生成。
 
5 执行执行autoheader,生成config.h.in
 

6 、运行automake --add-missing


命令:


$ automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./mkinstalldirs'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'

error;
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
这些对结果影响不大,如果不想产生这些让人误解的信息,可以手动建立这些文件:
>NEWS   
>README   
>AUTHORS   
>ChangeLog  

automake 会根据Makefile.am 文件产生一些文件,包含最重要的Makefile.in 。

7 、执行./configure 生成Makefile


$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output... 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 ANSI C... 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
$ ls -l Makefile
-rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile


你可以看到,此时Makefile 已经产生出来了。

8 、使用Makefile 编译代码



$ make
if gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -

DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="helloworld" -DVERSION="1.0"

-I. -I. -g -O2 -MT helloworld.o -MD -MP -MF ".deps/helloworld.Tpo" \
-c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c; \
then mv -f ".deps/helloworld.Tpo" ".deps/helloworld.Po"; \
else rm -f ".deps/helloworld.Tpo"; exit 1; \
fi
gcc -g -O2 -o helloworld helloworld.o


运行helloworld



$ ./helloworld
Hello, Linux World!


这样helloworld 就编译出来了,你如果按上面的步骤来做的话,应该也会很容易地编译出正确的helloworld 文件。你还可以试着使用一些其 他的make 命令,如make clean ,make install ,make dist ,看看它们会给你什么样的效果。感觉如何?自己也能写出这么专业的Makefile ,老板一定会对你刮目相看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值