打印本文 打印本文 关闭窗口 关闭窗口
linux下的Makefile的自动生成
作者:武汉SEO闵涛  文章来源:敏韬网  点击数640  更新时间:2009/4/22 23:08:09  文章录入:mintao  责任编辑:mintao
  初学linux,看到linux程序的install有./configure,make,make install等命令感到奇怪便找了
 一些文章看.
   GNU autoconf和GNU automake是GNU用来协助自动产生makefile文档的,能方便地安装你开发的软件
   
   make根据makefile编译、联结程序,手写makefile很困难因此有了autoconf,automake.

 请先确认你的系统已经安装以下的软件
 1.GNU Automake
 2.GNU Autoconf
 3.GNU m4
 4.perl
 5.GNU Libtool(如果你需要产生sharedlibrary)

 假设有一个hello.c文件 在该文件的目录下

 a ./autoscan 产生 configure.scan 文件

 b 编辑configure.scan为configure.in

 configure.in档的内容是一连串GNU m4的巨集
 dnl Process this file with autoconf to produce  a con figure script.  //dnl是一个注解标志
 AC_INIT(hello.c) // 用来检查原码路径
 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 ch aracteristics.  
 dnl Checks for library functions.  
 AC_OUTPUT(Makefile) //configure产生的文件

 c 执行aclocal,autoconf 产生  aclocal.m4 configure configure.in
 aclocal产生的aclocal.m4是一个巨集同configure.in供autoconf使用
 Autoconf会读取configure.in档然後产生''''''''configure''''''''这个 shell script
 ''''''''configure''''''''他使程序能在各种LINUX平台产生合适的Makefile或是C的头文
 件(header file),让原程序可以很方便地在这些不同的平台上被编 译出来。

 d 编辑Makefile.am

 AUTOMAKE_OPTIONS=foreign//必须
 bin_PROGRAMS=hello//
 hello_SOURCES=hello.c//指定原文件hello_SOURCES=hello.c main.c hello.h,
                         filename_SOURCES
 e automake - -add-missing automake根据makefile.am产生makefile.in

 - -add-missing选项是告诉automake顺便帮我们加 入包装一个软体套件所必备的档案

 f 执行./configure 产生makefile文件然后使用 make编译hello.c为执行文 。

 注意:Automake只需用到一些已经定义好的巨集(am),我们把巨集及目标(target)写在Makefile.am档内,Automake读入Makefile.am档後会把这一串已经定义好的巨集展开并且产生对应的Ma
 kefile.in档,然後再由configure这个shellscript根据Makefile.in产生适合的Makefile。

 d 使用makefile
 make all 编译程序可写为make
 make clean,make disclean,
 make install 程式会被安装至/usr/local/bin这个目录
 make dist 执行完在目录下会产生一个以PACKAGE-VERSION.tar.gz为名称的档案
 make distcheck  比前一个多了检查相关套件

打印本文 打印本文 关闭窗口 关闭窗口