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

   
   在介绍了简单的如何产生Makefile文件之后,相信已经能够编写Makefile文件了,但那还远

  远不够,因为编写那么简单的Makefile文件还要如此繁琐和兴师动众,那未免小题大做了。因此,

  我们有必要进一步了解如何制作Makefile文件。在制作Makefile文件的过程中,编写configure.in

  文件是关键,因此这一部分将重点介绍configure.in文件的编写的相关问题。对于Makefile.am文

  件的编写在编译多文件和多目录中要用到,所以下一篇将会介绍。

   在打开生成的Makefile文件,才发现能够编译的目标(target)有很多,因此有必要介绍一下

  其中比较重要而且常用的目标(target)的含义,也就是说,如何使用这个Makefile文件。

   1〉make 或 make all

   开始执行编译过程,产生我们设定的目标。这时会开始编译必要的源代码文件,然后进

  行连结,并且最终生成可执行文件或我们想要生成库文件。 具体命令行如下:

   [root@localhost testmk]#make

   或

   [root@localhost testmk]#make all

   2〉make dist

   将程序的源代码和相关文档打包成一个压缩文件,以用来备份源代码文件。命令完成后

  会在目录下会产生一个以 PACKAGE-VERSION.tar.gz 为名称的打包文件。PACKAGE 和 VERSION

  这两个变量是在configure.in文件中定义的。在我们的例子中定义如下:

   AM_INIT_AUTOMAKE(hello,1.0)

   因此会在目录中生成一个名为hello-1.0.tar.gz的文件。具体命令行如下:

   [root@localhost testmk]#make dist

   3〉make install

   将正确编译生成的可执行文件或库文件安装到系统中,通常是/usr/local/bin这个目录。

  如果没有需要安装的可执行文件或库文件,将会自动执行make命令进行编译,然后再进行安装操

  作。具体命令行如下:

   [root@localhost testmk]#make install

   4〉make clean

   清除之前所编译的可执行文件以及目标文件(Object Files, *.o)。 具体命令行如下:

   [root@localhost testmk]#make clean

   5〉make distclean

   清除之前所编译的可执行文件、目标文件(Object Files, *.o)以及由执行./configure

  所产生的 Makefile文件。 具体命令行如下:

   [root@localhost testmk]#make distclean

   在弄清Makefile文件如何使用之后,我们来进一步了解生成Makefile文件的有关问题。先看

  一下源文件的结构和内容:

   /hello-1.0

   /hello-1.0/pubfun.h

   /hello-1.0/pubfun.c

   /hello-1.0/hello.c

   -------------------------------------------------

   /*filename:pubfun.h */

   #include <stdio.h>

  

   void *printA(void *pdata);

   -------------------------------------------------

   /*filename:pubfun.c */

  

   #include <stdio.h>

   #include <math.h>

   #include <pubfun.h>

  

   void *printA(void *pdata)

   {

   printf("%f\n",sin(2));

   printf("Hello,World -->%d\n",getpid());

   }

   -------------------------------------------------

   /*filename:hello.c */

  

   #include <stdio.h>

   #include <pthread.h>

   #include "pubfun.h"

  

   int main(int argc,char **argv)

   {

   int ret;

   pthread_t ptid;

   int index;

  

   // create a thread

   ret = pthread_create(&ptid,NULL,printA,(void *)&index);

   if(ret)

   return -1;

   printA(NULL);

  

   return 0;

   }

   ---------------------------------------------------

   对于我们的源文件中用到了数学库和线程库,还有我们自己写的头文件,我们修改configure.in 文件如下:

   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 检查C编译器.如果在环境中没有设定CC,就查找gcc,如果没有找到,就使用cc.

   08:AC_PROG_CC

   09:

   10: dnl 为C编译器提供的调试和优化选项.

   11: CFLAGS=" -O2"

   12:

   13: dnl 为C预处理器和编译器提供头文件搜索目录选项('-Idir')以及其他各种选项.

   14: CPPFLAGS=" -I."

   15:

   16: dnl 自定义输出的检查信息

   17:AC_MSG_CHECKING([for architecture type])

   18:

   19:dnl 输出检查结果

   20: AC_MSG_RESULT([ok])

   21:

   22: dnl 传递给连接器的'-l'和'-L'选项.

   23: LIBS=" -L."

   24:

   25:dnl Checks for libraries,Might abort.

   26:AC_CHECK_LIB(m,sin,[LIBS="$LIBS -lm"],exit 1)

   27: AC_CHECK_LIB(pthread,pthread_create,[LIBS="$LIBS -pthread"],exit 1)

   28: dnl AC_CHECK_LIB(socket, connect)

   29:

   30:dnl Checks for header files.(检查头文件是否存在)

   31:AC_CHECK_HEADERS(sys/socket.h)

   32:

   33:dnl Checks for typedefs, structures, and compiler characteristics.

   34:

   35:dnl Checks for library functions.

   36:

   37:AC_OUTPUT(Makefile)

  

   这个configure.in文件中使用了几个常用的宏,还有一些如AC_ARG_ENABLE宏、AC_MSG_ERROR

  宏、AC_MSG_RESULT宏、AM_PATH_GTK宏等有用的宏,你可以在附带的附件文件里查找,以便使用。

  这里我要重点提一下第26到28行,对于程序中需要用到一些特定的库,需要在编译时进行指定,

  否则会出现连结错误。例如第26行就对数学库进行了检查,如果没有,将退出Makefile文件的生

  成,因为找不到数学库生成也编译不过去;同理第27行对线程库进行了检查,第28行对socket库

  进行了检查(被注释掉的原因是引用的例子中没用到此库)。这里面用到了AC_CHECK_LIB宏。具体

  用法如下:

   AC_CHECK_LIB(库名称,需要库中的函数,[如果找到,[如果没找到]])

  在这个宏中的[库名称]实在编译时 -l 选项后面的名称,如数学库 -lm 就用 m 就行了。在例子

  中,如果找到了库,就在编译选项中添加 -lm 选项。

   还是如上一篇中所述的那样,执行aclocal和autoconf命令,然后再创建并编辑Makefile.am

  文件内容如下:

   AUTOMAKE_OPTIONS=foreign

   bin_PROGRAMS=hello

   hello_SOURCES=hello.c pubfun.c pubfun.h

   Makefile.am文件和上一篇中相比就增加了源代码的数量,其他都没有改变。

   然后再执行automake --add-missing即可。信息如下:

   [root@leaf hello-1.0]# ./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

   checking for sin in -lm... yes

   checking for pthread_create in -lpthread... yes

   configure: creating ./config.status

   config.status: creating Makefile

   config.status: executing depfiles commands

   [root@leaf hello-1.0]# 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. -I. -O2 -c `test -f 'hello.c' || echo './'`hello.c

   source='pubfun.c' object='pubfun.o' libtool=no \

   depfile='.deps/pubfun.Po' tmpdepfile='.deps/pubfun.TPo' \

   depmode=gcc3 /bin/sh ./depcomp \

   gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING=""

  -DPACKAGE_BUGREPORT="" -DPACKAGE="hello" -DVERSION="1.0" -I.

   -I. -I. -O2 -c `test -f 'pubfun.c' || echo './'`pubfun.c

   gcc -O2 -o hello hello.o pubfun.o -L. -lm -pthread

  

  

  备注:

   1.以上内容均在RedHat Linux 9.0环境下测试通过。

   2.详细的选项可参考由王立翻译的文档http://www.cngnu.org/technology/1657/297.html。

   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

   5.附件源文件用如下命令打开即可:

   tar xvzf hello-1.0.tar.gz
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值