相信每个学习Linux的人都知道Makefile,这是一个很有用的东西,但是编写它是比较复杂,今天介绍一个它的自动生成工具,autotools的使用。很多GNULinux的的软件都是用它生成Makefile的,包括我们非常熟悉的Linux内核源代码。

1、准备:

需要工具

autoscan

aclocal

autoheader

automake

autoconf

auto make

在终端敲入命令,哪个没有安装哪个,一般是第一个autoscan没有,其它的我用的Ubuntu10.04下全部都有

2、测试程序编写:
     建立目录:mkdir include src

编写程序:include/str.h

  1. #include <stdio.h>
  2. int str(char *string);

编写程序:src/str.c

  1. #include "str.h"
  2. //print string
  3. int str(char *string){
  4. printf("\n----PRINT STRING----\n\"%s\"\n",string);
  5. return 0;
  6. }
  7. //interface of this program
  8. int main(int argc , char **argv){
  9. char str_read[1024];
  10. printf("Please INPUT something end by [ENTER]\n");
  11. scanf("%s",str_read);
  12. return str(str_read );
  13. }

3、生成configure.ac

configure.ac是automake的输入文件,所以必须先生成该文件。
    执行命令:

  1. [root@localhost str]# ls
  2. include  src
  3. [root@localhost str]# autoscan
  4. autom4te: configure.ac: no such file or directory
  5. autoscan: /usr/bin/autom4te failed with exit status: 1
  6. [root@localhost str]# ls
  7. autoscan.log  configure.scan  include  src
  8. [root@localhost str]# cp configure.scan configure.ac

修改 configure.ac

  1. #                                               -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.
  3. AC_PREREQ(2.59)
  4. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
  5. AC_CONFIG_SRCDIR([include/str.h])
  6. AC_CONFIG_HEADER([config.h])
  7. # Checks for programs.
  8. AC_PROG_CC
  9. # Checks for libraries.
  10. # Checks for header files.
  11. # Checks for typedefs, structures, and compiler characteristics.
  12. # Checks for library functions.
  13. AC_OUTPUT

修改

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

改为:

  1. AC_INIT(str,0.0.1, [bug@sounos.org])

其中:FULL-PACKAGE-NAME 为程序名称,VERSION为当前版本, BUG-REPORT-ADDRESS为bug汇报地址

然后添加两句话:

AM_INIT_AUTOMAKE
    AC_CONFIG_FILES([Makefile])

结果如下:(两句话不是在一起的)

  1. #                                               -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.
  3. AC_PREREQ(2.59)
  4. #AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
  5. AC_INIT(str, 0.0.1, [bug@sounos.org])
  6. AM_INIT_AUTOMAKE
  7. AC_CONFIG_SRCDIR([include/str.h])
  8. AC_CONFIG_HEADER([config.h])
  9. # Checks for programs.
  10. AC_PROG_CC
  11. # Checks for libraries.
  12. # Checks for header files.
  13. # Checks for typedefs, structures, and compiler characteristics.
  14. # Checks for library functions.
  15. AC_CONFIG_FILES([Makefile])
  16. AC_OUTPUT

4、执行aclocal

  1. [root@localhost str]# aclocal
  2. /usr/share/aclocal/libfame.m4:6: warning: underquoted definition of AM_PATH_LIBFAME
  3. run info '(automake)Extending aclocal'
  4. or see http://sources.redhat.com/automake/automake.html#Extending-aclocal

5、制作Makefile.am

  1. [root@localhost str]# vi Makefile.am
  2. #Makefile.am
  3. bin_PROGRAMS    = str
  4. str_SOURCES     = include/str.h src/str.c
  5. str_CPPFLAGS    = -I include/

automake 这个命令需要用到这个配置文件。各个选项意思比较直观,不多说。

6、autoheader

  1. [root@localhost str]# autoheader

7、automake必须文件:

  1. *  install-sh
  2. * missing
  3. * INSTALL
  4. * NEWS
  5. * README
  6. * AUTHORS
  7. * ChangeLog
  8. * COPYING
  9. * depcomp

其中,以下文件在执行automake -a的时候会自动生成

  1. * install-sh
  2. * missing
  3. * INSTALL
  4. * COPYING
  5. * depcomp

所以,接下来手动生成剩下的文件

  1. [root@localhost str]# touch NEWS README AUTHORS ChangeLog

8、执行automake -a

  1. [root@localhost str]# automake -a
  2. configure.ac: installing `./install-sh'
  3. configure.ac: installing `./missing'
  4. Makefile.am: installing `./INSTALL'
  5. Makefile.am: installing `./COPYING'
  6. Makefile.am: installing `./compile'
  7. Makefile.am: installing `./depcomp'

9、autoconf

  1. [root@localhost str]# autoconf
  2. [root@localhost str]# ls
  3. aclocal.m4      autoscan.log  config.h.in   configure.scan  include     Makefile.am  NEWS
  4. AUTHORS         ChangeLog     configure     COPYING         INSTALL     Makefile.in  README
  5. autom4te.cache  compile       configure.ac  depcomp         install-sh  missing      src

10、执行测试:
      执行./configure

  1. [root@localhost str]# ./configure --prefix=/u
  2. checking for a BSD-compatible install... /usr/bin/install -c
  3. checking whether build environment is sane... yes
  4. checking for gawk... gawk
  5. checking whether make sets $(MAKE)... yes
  6. checking for gcc... gcc
  7. checking for C compiler default output file name... a.out
  8. checking whether the C compiler works... yes
  9. checking whether we are cross compiling... no
  10. checking for suffix of executables...
  11. checking for suffix of object files... o
  12. checking whether we are using the GNU C compiler... yes
  13. checking whether gcc accepts -g... yes
  14. checking for gcc option to accept ANSI C... none needed
  15. checking for style of include used by make... GNU
  16. checking dependency style of gcc... gcc3
  17. configure: creating ./config.status
  18. config.status: creating Makefile
  19. config.status: creating config.h
  20. config.status: config.h is unchanged
  21. config.status: executing depfiles commands

执行 make

  1. [root@localhost str]# make
  2. make  all-am
  3. make[1]: Entering directory `/data/devel/c/str'
  4. if gcc -DHAVE_CONFIG_H -I. -I. -I.  -I include/   -g -O2 -MT str-str.o -MD -MP -MF ".deps/str-str.Tpo" -c -o str-str.o `test -f 'src/str.c' || echo './'`src/str.c; \
  5. then mv -f ".deps/str-str.Tpo" ".deps/str-str.Po"; else rm -f ".deps/str-str.Tpo"; exit 1; fi
  6. gcc  -g -O2   -o str  str-str.o
  7. make[1]: Leaving directory `/data/devel/c/str'

此时已经生成了 str(可执行文件名字在前面设置Makefile.am的参数时候去顶)这个,可以通过./str直接看到运行结果

  1. [root@localhost str]# ./str
  2. Please INPUT something end by [ENTER]
  3. abcksdhfklsdklfdjlkfd
  4. ----PRINT STRING----
  5. "abcksdhfklsdklfdjlkfd"

不过这里我们都做一步,把它安装到系统里面,这样我们只要在终端输入str就可以运行程序了。

执行 make install:

  1. [root@localhost str]# make install
  2. make[1]: Entering directory `/data/devel/c/str'
  3. test -z "/u/bin" || mkdir -p -- "/u/bin"
  4. /usr/bin/install -c 'str' '/u/bin/str'
  5. make[1]: Nothing to be done for `install-data-am'.
  6. make[1]: Leaving directory `/data/devel/c/str'

接下来你可以make clean 清除安装的那些.o 文件了。

这样生成了一个自动的Makefile。

Makefile自动生成工具-----autotools的使用(详细)的更多相关文章

  1. C/C++ makefile自动生成工具(comake2,autotools,linux),希望能为开源做点微薄的贡献!

      序     在linux下C或C++项目开发,Makefile是必备的力气,但是发现手写很麻烦. 在百度有个comake2工具,用于自动生成Makefile工具,而在外边本想找一个同类工具,但发现 ...

  2. Makefile自动生成头文件依赖

    前言 Makefile自动生成头文件依赖是很常用的功能,本文的目的是想尽量详细说明其中的原理和过程. Makefile模板 首先给出一个本人在小项目中常用的Makefile模板,支持自动生成头文件依赖 ...

  3. h5自动生成工具

    一.前言 写了很多h5之后,对于写手写html和css已经麻木的我决定动手写个工具自动生成h5结构和样式.其实这个想法由来已久,但总是觉得自己技术不够,所以一直没实行.直到某天我真的写够了,我决定动手 ...

  4. 代码自动生成工具MyGeneration之一(程序员必备工具)

    代码自动生成工具MyGeneration之一(程序员必备工具) 转 分类: C#2008-08-06 18:12 16064人阅读 评论(12) 收藏 举报 工具数据库相关数据库stringbrows ...

  5. Linux Makefile自动生成--config.h

    Linux Makefile自动生成--config.h http://blog.csdn.net/spch2008/article/details/12510805

  6. Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展

    Asp.net mvc 5 CRUD代码自动生成工具 -Visual Studio.net2013 Saffolding功能扩展 上次做过一个<Asp.net webform scaffoldi ...

  7. springboot成神之——swagger文档自动生成工具

    本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...

  8. 基于数据库的代码自动生成工具,生成JavaBean、生成数据库文档、生成前后端代码等(v6.0.0版)

    TableGo v6.0.0 版震撼发布,此次版本更新如下: 1.UI界面大改版,组件大调整,提升界面功能的可扩展性. 2.新增BeautyEye主题,界面更加清新美观,也可以通过配置切换到原生Jav ...

  9. C# 代码自动生成工具

    开源:C# 代码自动生成工具,支持站点前后台   前言 写这个项目有很长一段时间了,期间也修修改改,写到最后,自己也没咋用(研究方向变化了). 正文 具体项目开源了:https://github.co ...

随机推荐

  1. C# 中根据datetime的值来计算属于本年的第几周,类似delphi中的weekoftheyear功能

    /// <summary> /// 获得今天是今年的第几周 /// </summary> /// <param name="year">< ...

  2. 如何使用notepad++搭配MinGW配置编译C/C++

    最经开始学C++了,平时学习不喜欢用IDE,一直以来都喜欢使用sublimetext写代码.所以在网上找了一下如何配置sublimetext编译C/C++.不过简单配置之后,只有输出,要想进行输出操作 ...

  3. BZOJ 1965: [Ahoi2005]SHUFFLE 洗牌( 数论 )

    对于第x个数, 下一轮它会到位置p. 当x<=N/2, p = x*2 当x>N/2, p = x*2%(N+1) 所以p = x*2%(N+1) 设一开始的位置为t, 那么t*2M%(N ...

  4. HTML文档类型声明的坑...

    如果发现js莫名其妙的报错(比如demo不报错,自己写的就报错),或者样式显示不正常,一定记得检查HTML页面里面加没加如下文档声明: <!DOCTYPE HTML PUBLIC "- ...

  5. a='1,2,3,4,5'如何转换为['1','2','3','4','5']

    a='1,2,3,4,5'如何转换为['1','2','3','4','5'] b=a.split(',') split函数自动将分隔后的元素以逗号为分隔符存放到列表中 用处:有时需要用户输入多个数字 ...

  6. LGPL与闭源程序

    最近一直在学习 Qt.Qt 有两个许可证:LGPL 和商业协议.这两个协议在现在的 Qt 版本中的代码是完全一致的(潜在含义是,Qt 的早期版本,商业版的 Qt 通常包含有一些开源版本所没有的库,比如 ...

  7. CPU满格的元凶,这回是由于QTimer引起的(默认interval是0,太猛)

    timer_space = new QTimer(); qDebug() << SystemGlobal::m_app->SpaceUse; qDebug() << ti ...

  8. python模块 mysql-python安装(在ubuntu系统下)

    直接运行如下命令 sudo pip install MySQL-python 报如下错误 xxx@ubuntu:~$ sudo pip install MySQL-python Downloading ...

  9. 错误解决--oracle中出现ORA-01791: 不是 SELECTed 表达式 错误

    Oracle数据库,执行下面语句出现错误“ORA-01791: 不是 SELECTed 表达式”: select distinct t.name from auth_employee t order ...

  10. 第五章:输入输出(IO)管理

     I/O设备概念: 指计算机内部除中央处理器和内存之外的全部设备,通常也称为外部设备.  I/O设备分类:   ·按交互对象分类: ·人机交互设备 ·与计算机或其它电子设备交互的设备 ·计算机间的同信 ...