Linux 环境下 Makefile 文件制作浅谈(一)

Linux 环境下 Makefile 文件制作浅谈(一)
编写:Leaf Zhou
EMAIL:leaf_zhou_8@hotmail.com
可自由复制但禁止删改
2003-10-12




无论对于一个初学者还是一个资深的Linux程序员,编写Makefile文件都是一件很麻烦的事;再者,开发人员应该把主要的精力放在程序代码的编写 上,而在Makefile文件花费太多的精力显然是不明智的;还有,对于不同的处理器架构,往往编译器不同,环境不同,特别是一些嵌入式系统中的各种程序 的编译,于是移植问题也使Makefile文件编写趋于复杂,也显得这个问题很重要。对于这些问题Linux的高手们早已想到了,所以他们开发了一些能够 自动生成Makefile文件的工具。他们就是下面这些工具:
〉GNU Automake
〉GNU Autoconf
〉GNU m4
〉perl
〉GNU Libtool
因此您的操作系统必须安装以上软件,否则就不能够自动生成Makefile文件或者在生成的过程中出现各种各样的问题。用 autoconf/automake/autoheader工具来处理各种移植性的问题,用这些工具完成系统配置信息的收集,制作Makefile文件。 然后在打算编译源码时只需要通过 "./configure; make"这样简单的命令就可以得到干净利落的编译。

制作Makefile文件需要以下几步:
1〉建立编译目录和源代码目录及源代码文件(即要编译的源文件)
[root@localhost leaf]#mkdir testmk
[root@localhost leaf]#cd testmk
[root@localhost testmk]#vi hello.c
编辑hello.c文件如下内容:
/*filename:hello.c*/
#include <stdio.h>

int main(int argc,char **argv)
{
printf("%s/n","Hello, World!")
return 0;
}
2〉利用autoscan工具产生一个configure.in文件的模板文件configure.scan文件:
[root@localhost testmk]#autoscan
[root@localhost testmk]#ls
configure.scan hello.c
3〉把configure.scan文件更名为configure.in文件,并编译其内容如下:
[root@localhost testmk]#mv configure.scan configure.in
[root@localhost testmk]#vi configure.in
dnl Process this file with autoconf to produce a configure script.
AC_INIT(hello.c)

dnl Add the file by leaf
AM_INIT_AUTOMAKE(hello,1.0)

dnl Checks for programs.
AC_PROG_CC

dnl Checks for libraries.

dnl Checks for header files.

dnl Checks for typedefs, structures, and compiler characteristics.

dnl Checks for library functions.

AC_OUTPUT(Makefile)

4〉执行aclocal,会产生aclocal.m4文件
[root@localhost testmk]#aclocal
[root@localhost testmk]#ls
aclocal.m4 configure.in hello.c
5〉执行autoconf,会产生confiure文件
[root@localhost testmk]#autoconf
[root@localhost testmk]#ls
aclocal.m4 [autom4te.cache] configure configure.in hello.c
6〉创建文件Makefile.am并编辑其内容如下:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCES=hello.c
其中,hello为编译后产生的可执行文件名称,而第三行等号后面为源文件列表
7〉执行automake程序,automake程序会根据Makefile.am产生一些文件,其中最重要的是Makefile.in文件:
[root@localhost testmk]#automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./mkinstalldirs'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'
[root@localhost testmk]#ls
aclocal.m4 [autom4te.cache] configure configure.in depcomp
hello.c install-sh Makefile.am Makefile.in missing
mkinstalldirs
8〉执行configure脚本,生成我们需要的Makefile文件。
[root@localhost testmk]#./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
9〉最后只执行make就大功告成了:
[root@localhost testmk]#make
source='hello.c' object='hello.o' libtool=no /
depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo' /
depmode=gcc3 /bin/sh ./depcomp /
gcc -DPACKAGE_NAME=/"/" -DPACKAGE_TARNAME=/"/" -DPACKAGE_VERSION=/"/" -DPACKAGE_STRING=/"/" -DPACKAGE_BUGREPORT=/"/" -DPACKAGE=/"hello/" -DVERSION=/"1.0/" -I. -I. -g -O2 -c `test -f 'hello.c' || echo './'`hello.c
gcc -g -O2 -o hello hello.o

备注:
1.以上内容均在RedHat Linux 9.0环境下测试通过。
2.参考书目《Linux 程序设计权威指南》于明俭、陈向阳、方汉编著
3.其它国内外网站资料
4.RedHat 9.0下带的程序文件及版本
autoconf-2.57-3.noarch.rpm
automake-1.6.3-5.noarch.rpm
gcc-3.2.2-5.i386.rpm




Re: Linux 环境下 Makefile 文件制作浅谈(一)

对于比较简单的工程,我采用:

gcc -MM > Makefile

然后再稍微手工编辑一下,高效方便



Re: Linux 环境下 Makefile 文件制作浅谈(一)

多文件的工程怎么做?

Re: Linux 环境下 Makefile 文件制作浅谈(一)

生成动态连接库 .so 文件怎么做?
如果我多个文件放在多个目录中呢?

Re: Linux 环境下 Makefile 文件制作浅谈(一)

哪里有中文的automake,autoconfig HOW-TO,呵呵,毕竟是中文的东西看的轻松亚。。 *^_^*

Re: Linux 环境下 Makefile 文件制作浅谈(一)

关注,不是很清楚。第3补是把内容添加到configure.in.
添加到什么地方呢?文件末尾吗?

Re: Linux 环境下 Makefile 文件制作浅谈(一)

在执行了autoscan之后,产生的文件是configure.scan,它是configure.in的蓝本文件。我们要对它进行修改。原始内容大致如下:
dnl Process this file with autoconf to produce a configure script.
AC_INIT(hello.c)

dnl Checks for programs.
AC_PROG_CC

dnl Checks for libraries.

dnl Checks for header files.

dnl Checks for typedefs, structures, and compiler characteristics.

dnl Checks for library functions.

AC_OUTPUT()
下面是修改过文件内容:
01:dnl Process this file with autoconf to produce a configure script.
02:AC_INIT(hello.c)
03:
04:dnl Add the file by leaf
05:AM_INIT_AUTOMAKE(hello,1.0)
06:
07:dnl Checks for programs.
08:AC_PROG_CC
09:
10:dnl Checks for libraries.
11:
12:dnl Checks for header files.
13:
14:dnl Checks for typedefs, structures, and compiler characteristics.
15:
16:dnl Checks for library functions.
17:
18:AC_OUTPUT(Makefile)
内容大致修改了两个地方,首先添加了第3、4、5行,确定生成文件的名称和软件的版本;然后修改第18行,添加了要生的Makefile文件的名称。记住,可不是在后面直接添加呀。
========================================================
对于多个Makefile文件的生成和编译库文件Makefile文件的生成,正在编写中,由于时间有限,所以可能需要几天;另外,非常感谢朋友们的回应,也希望大家多提问题,这样我们才能共同进步。

个人认为可以算做一点补充

at first, i m just a newbie.

不错,测试了一下
结果如下:

1. 把AC_INIT(hello.c)改为
AC_CONFIG_SRCDIR([hello.c])
AM_CONFIG_HEADER([config.h])

2. 在第6步和第7步之间加上一行命令
touch config.h.in

3. automake的参数最好为 --copy 或 -c
这样就不会是link了

4.autoconf 和 automake 之间还可以加上autoheader

------------------------------------------------
OS: Red Hat Linux 9.0
Kernel: linux 2.4.20-20.9
compiler: gcc 3.2.2 20030222
-------------------------------------------------

推荐:
http://sources.redhat.com/autobook/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值