Linux Automake工具生成Makefile软件实现步骤

运维 系统运维
在本文中,将给大家介绍如何使用Linux Automake和automake两个工具来帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常见的GNU程序一样,只要使用“./configure”,“make”,“make instal”就可以把程序安装到Linux系统中去了。

使用的环境,本文所提到的程序是基于Linux发行版本:Fedora Core release 1,它包含了我们要用到的Linux Automake,automake。 从helloworld入手,我们从大家最常使用的例子程序helloworld开始。 下面的过程如果简单地说来就是:新建三个文件:
helloworld.c
configure.in
Makefile.am

然后执行:
aclocal; autoconf; automake --add-missing; ./configure; make; ./helloworld
就可以看到Makefile被产生出来,而且可以将helloworld.c编译通过。
很简单吧,几条命令就可以做出一个符合惯例的Makefile,感觉如何呀。

现在开始介绍Linux Automake工具生成Makefile软件实现步骤详细的过程:

1、建目录
在你的工作目录下建一个helloworld目录,我们用它来存放helloworld程序及相关文件,如在/home/my/build下:
$ mkdir helloword
$ cd helloworld

2、 helloworld.c
然后用你自己最喜欢的编辑器写一个hellowrold.c文件,如命令:vi helloworld.c。使用下面的代码作为helloworld.c的内容。
 

  1. int main(int argc, char** argv)  
  2. {  
  3. printf("Hello, Linux World!\n");  
  4. return 0;  
  5. }  

完成后保存退出。
现在在helloworld目录下就应该有一个你自己写的helloworld.c了。

3、生成configure
我们使用autoscan命令来帮助我们根据目录下的源代码生成一个configure.in的模板文件。
命令:
$ autoscan
$ ls
configure.scan helloworld.c
执行后在hellowrold目录下会生成一个文件:configure.scan,我们可以拿它作为configure.in的蓝本。
现在将configure.scan改名为configure.in,并且编辑它,按下面的内容修改,去掉无关的语句:
configure.in内容开始
 

  1. # -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.   
  3. AC_INIT(helloworld.c)  
  4. AM_INIT_AUTOMAKE(helloworld, 1.0)   
  5. # Checks for programs.  
  6. AC_PROG_CC   
  7. # Checks for libraries.   
  8. # Checks for header files.   
  9. # Checks for typedefs, structures, and compiler characteristics.   
  10. # Checks for library functions.  
  11. AC_OUTPUT(Makefile)  

configure.in内容结束
然后执行命令aclocal和autoconf,分别会产生aclocal.m4及configure两个文件:
 

  1. $ aclocal   
  2. $ls   
  3. aclocal.m4 configure.in helloworld.c   
  4. $ autoconf   
  5. $ ls   
  6. aclocal.m4 autom4te.cache configure configure.in helloworld.c   

 

大家可以看到configure.in内容是一些宏定义,这些宏经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。 autoconf 是用来生成自动配置软件源代码脚本(configure)的工具。configure脚本能独立于autoconf运行,且在运行的过程中,不需要用户的干预。 要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4。
 
aclocal根据configure.in文件的内容,自动生成aclocal.m4文件。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。 autoconf从configure.in这个列举编译软件时所需要各种参数的模板文件中创建configure。 autoconf需要GNU m4宏处理器来处理aclocal.m4,生成configure脚本。 m4是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,循环等。m4既可以作为编译器的前端,也可以单独作为一个宏处理器。

4、新建Makefile.am
新建Makefile.am文件,命令:
$ vi Makefile.am
内容如下:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c
automake会根据你写的Makefile.am来自动生成Makefile.in。
Makefile.am中定义的宏和目标,会指导automake生成指定的代码。例如,宏bin_PROGRAMS将导致编译和连接的目标被生成。

5、运行automake
命令:
 

  1. $ automake --add-missing  
  2. configure.in: installing `./install-sh'  
  3. configure.in: installing `./mkinstalldirs'  
  4. configure.in: installing `./missing'  
  5. Makefile.am: installing `./depcomp'  


automake会根据Makefile.am文件产生一些文件,包含最重要的Makefile.in。

6、执行configure生成Makefile
 

  1. $ ./configure   
  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... 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: executing depfiles commands  
  20. $ ls -l Makefile  
  21. -rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile   

你可以看到,此时Makefile已经产生出来了。

7、使用Makefile编译代码
 

  1. $ make  
  2. if gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -   
  3. DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="helloworld" -DVERSION="1.0"   
  4. -I. -I. -g -O2 -MT helloworld.o -MD -MP -MF ".deps/helloworld.Tpo" \  
  5. -c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c; \  
  6. then mv -f ".deps/helloworld.Tpo" ".deps/helloworld.Po"; \  
  7. else rm -f ".deps/helloworld.Tpo"; exit 1; \  
  8. fi  
  9. gcc -g -O2 -o helloworld helloworld.o  
  10. 运行helloworld   
  11. $ ./helloworld   
  12. Hello, Linux World!  


这样helloworld就编译出来了,你如果按上面的步骤来做的话,应该也会很容易地编译出正确的helloworld文件。你还可以试着使用一些其他的make命令,如make clean,make install,make dist,看看它们会给你什么样的效果。感觉如何?自己也能写出这么专业的Makefile,老板一定会对你刮目相看。

【编辑推荐】

  1. Autoconf使用关于autoconf安装条件介绍 
  2. Autoconf使用生成Makefile的方法及其规则
  3. autoconf安装关于可移植的源代码详解
  4. Autoconf教程关于UNIX文件系统概述
  5. Autoconf教程关于安全管理介绍

 

责任编辑:chenqingxiang 来源: 互联网
相关推荐

2017-09-12 09:22:51

LinuxMakefileautoconf工具

2010-06-22 17:09:52

Linux Autom

2010-06-22 23:52:42

Automake使用

2010-03-01 09:57:55

Linux Makef

2010-06-22 15:45:06

Autoconf使用

2010-06-22 17:45:34

Linux Autom

2010-06-22 22:50:40

Automake使用

2009-10-26 13:45:39

linux Makef

2009-10-26 11:34:42

linux makef

2009-12-14 10:47:34

Linux makef

2010-02-25 17:49:36

Linux Makef

2017-06-06 14:44:37

2016-10-18 11:16:42

评估选择软件

2010-06-22 15:40:54

Autoconf使用

2010-06-13 15:26:42

2010-05-27 12:52:42

Linux流量监控软件

2010-06-18 16:35:54

Linux amidi

2010-05-28 14:34:17

Linux开发工具

2010-01-05 15:26:04

Linux软件

2009-08-10 11:12:06

点赞
收藏

51CTO技术栈公众号