使用autotools自动生成makefile

本文通过一个简单的项目,详细介绍了如何利用GNU Autotools(包括aclocal、autoscan、autoconf、autoheader和automake)生成Makefile。首先,通过autoscan创建configure.scan并改名为configure.ac,然后逐个使用aclocal、autoconf和automake生成aclocal.m4、configure和Makefile.in。最后,通过configure生成Makefile并进行编译、安装和卸载操作。文章强调了理解Makefile规则的重要性,并指出对于复杂项目,Autotools的完整应用还包括处理文件关系、动态库和第三方库的依赖等。
摘要由CSDN通过智能技术生成

转自:http://blog.csdn.net/scucj/article/details/6079052

手工写Makefile是一件很有趣的事情,对于比较大型的项目,如果有工具可以代劳,自然是一件好事。在Linux系统开发环境中,GNU Autotools 无疑就充当了这个重要角色。(在Windows系统的开发环境中,IDE工具,诸如Visual Studio,来管理项目也很方便。)

      本文以一个简单项目为例子,来讲述GNU Autotools的一列工具及其命令的用法。

autotools是系列工具, 它主要由autoconf、automake、perl语言环境和m4等组成;所包含的命令有五个:
    (1)aclocal
    (2)autoscan
    (3)autoconf
    (4)autoheader
    (5)automake

 

一、准备源代码

(1)目录project包含一个main.c的文件和两个子目录lib与include;lib目录中包含一个test.c,include目录中包含一个test.h。在系统中,显示如下:

[c-sharp]  view plain  copy
  1. [root@localhost project]# ls  
  2. include  lib  main.c  
  3. [root@localhost project]#  
  4. [root@localhost project]# ls include/  
  5. test.h  
  6. [root@localhost project]# ls lib/  
  7. test.c  
  8. [root@localhost project]#  

 

(2)源代码如下:

 

[cpp]  view plain  copy
  1. /* project/main.c */  
  2. #include <stdio.h>  
  3. #include "include/test.h"  
  4. int main()  
  5. {  
  6.     printf("main entrance./n");  
  7.     test_method();  
  8.     return 0;  
  9. }  

 

[c-sharp]  view plain  copy
  1. /* project/lib/test.c */  
  2. #include <stdio.h>  
  3. #include "../include/test.h"  
  4. void test_method()  
  5. {  
  6.     printf("test method./n");  
  7. }  

[cpp]  view plain  copy
  1. /* project/include/test.h*/  
  2. void test_method();  

 

二、autotools 使用步骤

     2.1 使用autoscan命令,它将扫描工作目录,生成 configure.scan 文件。    

[c-sharp]  view plain  copy
  1. [root@localhost project]# autoscan  
  2. autom4te: configure.ac: no such file or directory  
  3. autoscan: /usr/bin/autom4te failed with exit status: 1  
  4. [root@localhost project]# ls  
  5. autoscan.log  configure.scan  include  lib  main.c  
  6. [root@localhost project]#  

      2.2 将configure.scan 文件重命名为configure.ac,并做适当的修改。在 configure.ac 中,# 号开始的行是注释,其他都是m4 宏命令;configure.ac里面的宏的主要作用是侦测系统。

 

[c-sharp]  view plain  copy
  1. [root@localhost project]mv configure.scan configure.ac  
  2. [root@localhost project]# ls  
  3. autoscan.log  configure.ac include  lib  main.c  
  4. [root@localhost project]#  
  5. [root@localhost project]# cat configure.ac  
  6. #                                               -*- Autoconf -*-  
  7. # Process this file with autoconf to produce a configure script.  
  8. AC_PREREQ(2.59)  
  9. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)  
  10. AC_CONFIG_SRCDIR([main.c])  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值