Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47205
  • 博文数量: 14
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 160
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-05 22:28
文章分类
文章存档

2011年(1)

2009年(13)

我的朋友

分类: LINUX

2009-08-11 16:40:00

原来做个Makefile这么麻烦。按照这文章的说法,需要7步:
1〉建立编译目录和源代码目录及源代码文件(即要编译的源文件)
2〉利用autoscan工具产生一个configure.in文件的模板文件configure.scan文件:
3〉把configure.scan文件更名为configure.in文件,并编译其内容如下:
4〉执行aclocal,会产生aclocal.m4文件
5〉执行autoconf,会产生confiure文件
6〉创建文件Makefile.am并编辑其内容
7〉执行automake程序,automake程序会根据Makefile.am产生一些文件,其中最重要的是Makefile.in文件
(以下部分内容被修改。)

  编写:Leaf Zhou
  EMAIL:leaf_zhou_8@hotmail.com

  无论对于一个初学者还是一个资深的Linux程序员,编写Makefile文件都是一件很麻烦的事;再者,开发人员应该把主要的精力放在程序代码的编写上,而在Makefile文件花费太多的精力显然是不明智的;还有,对于不同的处理器架构,往往编译器不同,环境不同,特别是一些嵌入式系统中的各种程序的编译,于是移植问题也使Makefile文件编写趋于复杂,也显得这个问题很重要。对于这些问题Linux的高手们早已想到了,所以他们开发了一些能够自动生成Makefile文件的工具。他们就是下面这些工具:

  〉GNU Automake
  〉GNU Autoconf
  〉GNU m4
  〉perl
  〉GNU Libtool

  因此您的操作系统必须安装以上软件,否则就不能够自动生成Makefile文件或者在生成的过程中出现各种各样的问题。用 autoconf/automake/autoheader工具来处理各种移植性的问题,用这些工具完成系统配置信息的收集,制作Makefile文件。然后在打算编译源码时只需要通过 "./configure; make"这样简单的命令就可以得到干净利落的编译。

制作Makefile文件需要以下几步:

1〉建立编译目录和源代码目录及源代码文件(即要编译的源文件)

[dwang@dwang study]$ mkdir testmk
[dwang@dwang study]$ cd testmk/
[dwang@dwang testmk]$ ls
[dwang@dwang testmk]$ vim hello.c
  编辑hello.c文件如下内容
#include
int main(void)
{
    printf("%s\n", "hello, world!");
    return 0;
}

2〉利用autoscan工具产生一个configure.in文件的模板文件configure.scan文件:
[dwang@dwang testmk]$ autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1

3〉把configure.scan文件更名为configure.in文件,并编译其内容如下:
[dwang@dwang testmk]$ mv configure.scan configure.in
[dwang@dwang testmk]$ vim configure.in
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(hello.c)

AM_INIT_AUTOMAKE(hello, 0.1)

# 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)

4〉执行aclocal,会产生aclocal.m4文件
[dwang@dwang testmk]$ aclocal

5〉执行autoconf,会产生confiure文件
[dwang@dwang testmk]$ autoconf

6〉创建文件Makefile.am并编辑其内容如下:
  AUTOMAKE_OPTIONS=foreign
  bin_PROGRAMS=hello
  hello_SOURCES=hello.c
其中,hello为编译后产生的可执行文件名称,而第三行等号后面为源文件列表

7〉执行automake程序,automake程序会根据Makefile.am产生一些文件,其中最重要的是Makefile.in文件:
[dwang@dwang testmk]$ automake --add-missing
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

这样就完成了Makefile文件的制作,执行的结果如下:
[dwang@dwang 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 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 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
[dwang@dwang testmk]$ make
if gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"hello\" -DVERSION=\"0.1\"  -I. -I.     -g -O2 -MT hello.o -MD -MP -MF ".deps/hello.Tpo" -c -o hello.o hello.c; \
        then mv -f ".deps/hello.Tpo" ".deps/hello.Po"; else rm -f ".deps/hello.Tpo"; exit 1; fi
gcc  -g -O2   -o hello  hello.o 
[dwang@dwang testmk]$./hello
hello World!
阅读(2008) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~