Chinaunix首页 | 论坛 | 博客
  • 博客访问: 292943
  • 博文数量: 111
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 672
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-05 23:41
文章分类

全部博文(111)

文章存档

2017年(111)

我的朋友

分类: LINUX

2017-06-19 17:47:29

作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便。一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Makefile就不那么容易了。 

  在本文中,将给大家介绍如何使用 autoconfautomake两个工具来帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常见的GNU程序一样,只要使用“./configure”“make”“make install”就可以把程序安装到Linux系统中去了。这将特别适合想做开放源代码软件的程序开发人员,又或如果你只是自己写些小的Toy程序,那么这个文章对你也会有很大的帮助。

  1.准备好程序 main.c

   main.c
   #include

   int main()

  {

      print("hello!");

      return 0;

   }

  2.准备好脚本 autogen.sh  

  为了autogen.sh运行时,输出提醒信息。autogen.sh如下:
#!/bin/sh
#
# Copyright (C) 2003-2010 Sebastien Helleu 
# Copyright (C) 2005 Julien Louis 
# Copyright (C) 2005-2006 Emmanuel Bouthenot 
#
# This file is part of WeeChat, the extensible chat client.
#
# WeeChat is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# WeeChat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with WeeChat.  If not, see .
#  
###
### common stuff
###
OK="\\033[70G[\\033[1;32mOK\\033[1;00m]"
FAIL="\\033[70G[\\033[1;31mFAILED\\033[1;00m]"  
AUTOGEN_LOG=autogen.log
  err ()
{
echo "-------"
echo "Error :"
echo "---8<-----------------------------------"
cat $AUTOGEN_LOG
echo "----------------------------------->8---"
exit 1
}
  run ()
{
echo -n "Running "$@""
eval $@ >$AUTOGEN_LOG 2>&1
if [ $? = 0 ] ; then
echo -e $OK
else
echo -e $FAIL
err
fi
}  
###
### cleanning part
###
#  remove autotools stuff
run "rm -rf config"
run "rm -f config.h.in"
run "rm -f aclocal.m4 configure config.log config.status"
run "rm -rf autom4te*.cache"
# remove libtool stuff
run "rm -f libtool"
# remove gettext stuff
run "rm -f ABOUT-NLS"
run "rm -rf intl"  
###
### configuration part
###
# create the config directory
run "mkdir -p config/m4"
run "mkdir intl"  
# execute autotools cmds
run "autopoint -f"
run "libtoolize --automake --force --copy"
run "aclocal --force -I config/m4"
run "autoheader"
run "autoconf"
run "automake --add-missing --copy --gnu"  
# ending
rm -f $AUTOGEN_LOG

  3.运行autoscan 生成configure.scan

  4.mv configure.scan configure.ac

  5.configure.ac 中修改

  AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)

   AC_INIT(hello, 0.0.1, ) 
  
并添加 AM_INIT_AUTOMAKE 、AM_GNU_GETTEXT_VERSION(0.18.1)、
AC_CONFIG_FILES([Makefile])

 configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.68])
AC_INIT(hello, 0.0.1, xuelei_51@126.com)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE]
AM_GNU_GETTEXT_VERSION(0.18.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_FUNC_MALLOC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

 6.准备好Makefile.am

 Makefile.am

  AUTOMAKE_OPTIONS = foreign

  bin_PROGRAMS = hello

  hello_SOURCES = main.c

  #noinst_HEADERS =

 7.运行autogen.sh    哼哼自动生成了makefile ! 

以上文件放在同一目录下运行./autogen.sh && ./configure && make && ./test

利用 configure 所产生的 Makefile文件有几个预先设定的目标可供使用,这里只用几个简述如下:

  make all

  产生设定的目标,既范例中的可执行文件。只敲入make 也可以,此时会开始编译源代码,然后连接并产生执行文件。

  make clean

  清除之前所编译的可执行文件及目标文件(object file, *.o)

  make distclean

  除了清除可执行文件和目标文件以外,也把 configure 所产生的 Makefile 清除掉。 通常在发布软件前执行该命令。

  make install

  将程序安装到系统中,若源码编译成功,且执行结果正确,便可以把程序安装到系统预先设定的执行文件存放路径中,若用 bin_PROGRAMS 宏的话,程序会被安装到 /usr/local/bin下。

  make dist

  将程序和相关的文档包装为一个压缩文档以供发布 (distribution) 。执行完在目录下会产生一个以 PACKAGE-VERSION.tar.gz 为名称的文件!

  make distcheck

   make dist 类似,但是加入检查包装以后的压缩文件是否正常,这个目标除了把程序和相关文档包装成 tar.gz 文件外,还会自动把这个压缩文件解开,执行 configure,并执行 make all ,确认编译无错误以后,方显示这个 tar.gz 文件已经准备好并可以发布了。

阅读(1190) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~