免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7365 | 回复: 4
打印 上一主题 下一主题

用autotools制作Makefile 和configure文件。 [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-29 19:24 |只看该作者 |倒序浏览
目的 从复杂的工作中简化出来。

网上有一些制作Makfile的文章,只停留在Makefile而已。用autotools的工具相对来说要简单的多,其它一些介绍autotools文章又有很多漏洞,而且步骤烦琐。

制作一个最简单的helloworld程序:

现有目录test

mkdir src 建立src目录存放 源代码
在src下。
编辑hello.c文件

  1. #include <stdio.h>

  2. int main()
  3. {
  4.         printf("hello world\n");
  5.         return 0;
  6. }
复制代码

在src目录下建立Makefile.am文件 (src/Makefile.am)

  1. AUTOMAKE_OPTIONS=foreign
  2. bin_PROGRAMS = hello
  3. hello_SOURCES = hello.c
  4. hello_LDADD = -lpthread (只是测试,实际不需要连接该库)
复制代码

保存退出

退到test目录

编辑Makefile.am文件 (Makefile.am)

SUBDIRS = src

退出保存

然后
执行
autoscan
生成configure.scan文件

按此编辑此文件

  1. #                                               -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.

  3. AC_PREREQ(2.59)
  4. AC_INIT(hello,1.0, [miaoquan@nou.com.cn])
  5. AM_INIT_AUTOMAKE
  6. AC_CONFIG_SRCDIR([src/hello.c])
  7. AC_CONFIG_HEADER([config.h])

  8. # Checks for programs.
  9. AC_PROG_CC

  10. # Checks for libraries.
  11. # FIXME: Replace `main' with a function in `-lpthread':
  12. AC_CHECK_LIB([pthread], [main])

  13. # Checks for header files.

  14. # Checks for typedefs, structures, and compiler characteristics.

  15. # Checks for library functions.

  16. #AC_CONFIG_FILES([Makefile
  17. #                 src/Makefile])
  18. AC_OUTPUT(Makefile src/Makefile)
复制代码


退出保存
将此文件更名 mv configure.scan configure.in
然后执行
touch NEWS README AUTHORS ChangeLog

然后执行
autoreconf -fvi

至此生成configure文件
执行configure文件

生成Makefile文件

make
make install
make uninstall
make dist
试验一下吧。

[ 本帖最后由 mq110 于 2006-6-29 19:29 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-06-29 21:15 |只看该作者

多谢

论坛徽章:
0
3 [报告]
发表于 2006-06-30 00:38 |只看该作者
基于文本替换的代码生成工具……

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2006-07-02 23:16 |只看该作者
继续完善这个例子,论坛里有人问,如何生成静态库,并连接.

完善hello.c这个例子


当前目录
     |-  src 目录
            |-  hello.c 文件
     |-  include 目录
            |-  hello.h文件
     |-  lib 目录
            |-  test.c文件 此文件用来生成 libhello.a

在当前目录 编写Makefile.am

  1. SUBDIRS = lib src
复制代码


在include目录下 编写hello.h


  1. extern void print(char *);
复制代码


在lib目录下编写test.c


  1. #include <stdio.h>

  2. void print(char *msg)
  3. {
  4.         printf("%s\n",msg);
  5. }
复制代码


在lib目录下编写Makefile.am


  1. noinst_LIBRARIES=libhello.a
  2. libhello_a_SOURCES=test.c
复制代码


这里noinst_LIBRARIES 的意思是生成的静态库 ,不会被make install 安装
然后指定libhello.a的源文件test.c


在src目录下编写hello.c


  1. #include "hello.h"

  2. int main()
  3. {
  4.         print("haha");  //这里是静态库里的print函数
  5.         return 0;
  6. }

复制代码


在src目录下编写Makefile.am

  1. INCLUDES= -I../include

  2. bin_PROGRAMS=hello
  3. hello_SOURCES=hello.c
  4. hello_LDADD=../lib/libhello.a
复制代码


首先指定头文件的位置 ../include
然后指定要生成执行文件 hello
然后指定源代码文件 hello.c
最后添加静态库的位置 ../lib/libhello.a


按照我首篇帖子的方式.
执行autoscan 生成configure.scan

修改该文件

按照首篇帖子修改.

然后不同之处
需要添加一行:AC_PROG_RANLIB



  1. #                                               -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.

  3. AC_PREREQ(2.59)
  4. AC_INIT(hello,1.1,[miaoquan@nou.com.cn])
  5. AM_INIT_AUTOMAKE
  6. AC_CONFIG_SRCDIR([src/hello.c])
  7. AC_CONFIG_HEADER([config.h])

  8. # Checks for programs.
  9. AC_PROG_CC


  10. # Checks for libraries.
  11. AC_PROG_RANLIB
  12. # Checks for header files.

  13. # Checks for typedefs, structures, and compiler characteristics.

  14. # Checks for library functions.

  15. #AC_CONFIG_FILES([Makefile
  16. #                lib/Makefile
  17.   #               src/Makefile])
  18. AC_OUTPUT([Makefile
  19.                         lib/Makefile
  20.                         src/Makefile])
复制代码


mv configure.scan configure.in


touch NEWS README AUTHORS ChangeLog


执行autoreconf -fvi

生成configure.执行configure生成Makefile..


后面同上...

[ 本帖最后由 mq110 于 2006-7-2 23:25 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-10-04 16:11 |只看该作者
这种方法根本不具有实用性,只能处理“hello world”这样的小问题。
首先,这个方式就不能自动建立多文件之间的依赖关系。
不过,用这样的小例子到是能清晰说明autotools的使用流程。

plus: mq110, 好久不见你来CU了,不知道现在还好吗?过节送去一份祝福吧

[ 本帖最后由 stackmm 于 2009-10-4 16:14 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP