Makefile自动生成工具-----autotools的使用(详细)

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

 

  1、准备:

  需要工具

  autoscan

  aclocal

  autoheader 

  automake

  autoconf

  auto make 

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

 

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

     编写程序:include/str.h

 

[cpp] view plaincopy

 

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

    编写程序:src/str.c

[cpp] view plaincopy

 

  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.   
  8. //interface of this program  
  9. int main(int argc , char **argv){  
  10.         char str_read[1024];  
  11.         printf("Please INPUT something end by [ENTER]\n");  
  12.         scanf("%s",str_read);  
  13.         return str(str_read );  
  14. }  
  15.   
  16.        

  3、生成configure.ac

 

 

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

 

[cpp] view plaincopy

 

  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 

 

 

 

[cpp] view plaincopy

 

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

修改

[cpp] view plaincopy

 

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

改为:

 

 

 

[cpp] view plaincopy

 

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

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

[cpp] view plaincopy

 

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

4、执行aclocal

[cpp] view plaincopy

 

  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

[cpp] view plaincopy

 

  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

 

[cpp] view plaincopy

 

  1. [root@localhost str]# autoheader  

7、automake必须文件:

[cpp] view plaincopy

 

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

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

 

 

 

[cpp] view plaincopy

 

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

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

 

 

 

[cpp] view plaincopy

 

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

8、执行automake -a

 

 

 

[cpp] view plaincopy

 

  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

[cpp] view plaincopy

 

  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

[cpp] view plaincopy

 

  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

[cpp] view plaincopy

 

  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直接看到运行结果

[cpp] view plaincopy

 

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

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

 

 

 执行 make install:

 

[cpp] view plaincopy

 

  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。

时间: 2024-08-04 05:44:31

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

编程-代码自动生成工具的具体讲解

问题描述 代码自动生成工具的具体讲解 求问大神有哪些代码自动生成工具?以及代码自动生成工具利用的局限性或者利弊? 请大神能不能详细讲解一下!

Makefile自动生成头文件依赖

前言 Makefile自动生成头文件依赖是很常用的功能,本文的目的是想尽量详细说明其中的原理和过程. Makefile模板 首先给出一个本人在小项目中常用的Makefile模板,支持自动生成头文件依赖. CC = gcc CFLAGS = -Wall -O INCLUDEFLAGS = LDFLAGS = OBJS = seq.o TARGETS = test_seq .PHONY:all all : $(TARGETS) test_seq:test_seq.o $(OBJS) $(CC) -o

基于JSP网页自动生成工具的设计与实现

js|设计|网页 摘 要:Web开发技术是Internet应用的一个重要方面,而JSP又是Web开发的最先进的技术,是当前Web开发人员的首选技术.但是由于JSP对Web开发人员要求较高,所以许多一般的Web开发人员还不能够使用这一项先进的技术.讨论基于模板和标签库的JSP网页自动生成工具的设计和实现,提出具体的设计思想和实现方法. 关键词:JSP:自动生成:Web开发:标签:标签库:模板 目录: 引言-------------------------..2 1          系统设计目标和

10款经典快速设计网页的自动生成工具

随着许多优秀的网页http://www.aliyun.com/zixun/aggregation/6993.html">设计工具和大量资源的出现,今天网页设计比起十年前要简便得多.本文收集了28款可以帮助你快速设计网页的自动生成工具,包括了logo,banner生成器,域名推荐. 1. Lorem Ipsum Generator 文本排版生成器 2. Stripe Generator 条纹背景 3. Mycoolbutton 按钮生成器

Makefile 自动生成依赖

虽然以前对Makefile有个基本概念,但是真正到自己去写一个哪怕是简单的Makefile时也会遇到不少的麻烦.    现在我有如下文件 dList.h dList.c memory.c debug.c debug.h test.c aaron.h 其中包含关系如下:    aaron.h-->dList.h debug.h    dList.c-->aaron.h    debug.c-->aaron.h    test.c-->aaron.h    memory.c-->

makefile自动生成依赖关系

手工编写依赖关系不仅工作量大而且极易出现遗漏,更新也很难及时,修改源或头文件后makefile可能忘记修改.为了解决这个问题,可以用gcc的-M选项自动生成目标文件和源文件的依赖关系.-M选项会把包含的系统头文件以及其所包含的其他系统头文件也找出来了,如果我们不需要输出系统头文件的依赖关系时,可以用-MM选项. 下面我们以一个简单的例子来说明如何自动生成依赖关系: exm/      main.c      s.c      s.h makefile文件内容如下: all:a src=$(wil

AutoThrCode 三层结构业务层代码自动生成工具

请看下面示例:数据表结构如下: 生成的 属性成员 和 添加,更新的代码: 'AutoThrCode自动生成三层结构业务逻辑层代码'IsDotNet 版权所有'作者:梦虫'Msn:IsDotNet@MsN.CoM'Http://www.IsDotNet.com'本代码引用 IsDotNet.Data.SqlDbHelper 类,请将 AdoHelper.dll 拷贝到WEB程序的BIN目录下'请在WEB程序的web.config文件的节配置数据库连接字符串,"ConnectionString&qu

英文站自动生成工具:NiceWords英文版

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 NiceWords英文版(www.nicewords.org )于2008年3月4日正式发布,以后做英文站就容易多了. 传统做英文站的方式:1.找一套国外的cms系统,如joomla.drupal.wordpress等. 2.用采集工具找一个国外的网站,乱采一通.或者干脆自己去复制粘贴. 3.找国外的论坛发广告,等待google收录.如果你的

Robots.txt的写法和利用百度站长平台工具自动生成

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 百度站长平台提供了一个很好的robots.txt自动生成工具,利用它我们可以很方便的写出robots.txt,通过robots.txt阻止搜索引擎收录你不想要被收录的页面及目录,还可以阻止及引导蜘蛛爬行更多的页面,减少蜘蛛的爬行量,提高蜘蛛的爬行效率,有效的提升更多的页面被收录. 前面我们介绍了通过IIS日志分析让你更好的了解网站运行情况,可