使用autotools进行项目管理简单应用

学习记录:
目录
project
  |---base
  |   |--fun.c
  |   |--fun.h       
  |   |--hello.c
  |   |--hello.h    
  |---src        
       |--main.c

主要使用automake,libtools进行编译辅助,base目录下是调用库,src目录是应用程序
文件内容如下:
fun.h
  1. ##fun.h
  2. #ifndef __FUN__H__
  3. #define __FUN__H__
  4. int add(int a, int b);
  5. #endif
 fun.c
  1. int add(int a, int b)
  2. {
  3.     return a+b;
  4. }
hello.h
  1. #ifndef __HELLO__H__
  2. #define __HELLO__H__
  3. void output(char *ss);
  4. #endif
hello.c
  1. #include <stdio.h>
  2. void output(char *ss)
  3. {
  4.     printf("HELLO %s/n",ss);
  5.     return;
  6. }
main.c
  1. #include <stdio.h>
  2. #include "hello.h"
  3. #include "fun.h"
  4. int main()
  5. {
  6.     output("world");
  7.     printf("Test Value:%d/n",add(1,2));
  8.     return 0;
  9. }
使用如下:
1。在project目录下建立Makefile.am文件
  1. SUBDIRS = base src
注意: 依赖文件库文件在前面
  在base目录下新建Makefile.am文件
  
  1. lib_LTLIBRARIES = libtest.la
  2. libtest_la_SOURCES = fun.c fun.h hello.c hello.h
在src目录下新建Makefile.am文件
  1. INCLUDES= -I$(top_srcdir)/base
  2. bin_PROGRAMS = test
  3. test_SOURCES = main.c
  4. test_LDADD = $(top_builddir)/base/libtest.la
2.在project目录下
  运行autoscan检测生成configure.scan..(出错不用理会),命名为configure.in文件
  $autoscan
  $mv configure.scan configure.in
  查看生成文件configure.in如下
  #    
  1. #
  2. # 利用libtool自动生成动态库
  3. #
  4. 1. autoscan命令在当前目录生成configure.scan文件, 内容为:
  5. # -*- Autoconf -*-
  6. # Process this file with autoconf to produce a configure script.
  7. AC_PREREQ(2.59)
  8. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
  9. AC_CONFIG_SRCDIR([src/main.c])
  10. AC_CONFIG_HEADER([config.h])
  11. # Checks for programs.
  12. AC_PROG_CC
  13. # Checks for libraries.
  14. # Checks for header files.
  15. # Checks for typedefs, structures, and compiler characteristics.
  16. # Checks for library functions.
  17. AC_CONFIG_FILES([Makefile
  18.                  base/Makefile
  19.                  src/Makefile])
  20. AC_OUTPUT
  修改为
  1. #
  2. # 利用libtool自动生成动态库
  3. #
  4. 1. autoscan命令在当前目录生成configure.scan文件, 内容为:
  5. # -*- Autoconf -*-
  6. # Process this file with autoconf to produce a configure script.
  7. AC_PREREQ(2.59)
  8. AC_INIT(test, 1.0, [email protected]) ###
  9. AC_CONFIG_SRCDIR([src/main.c])
  10. AM_CONFIG_HEADER([config.h])               ###
  11. AM_INIT_AUTOMAKE(test, 1.0)                ###
  12. # Checks for programs.
  13. AC_PROG_CC
  14. AM_PROG_LIBTOOL                            ###
  15. # Checks for libraries.
  16. # Checks for header files.
  17. # Checks for typedefs, structures, and compiler characteristics.
  18. # Checks for library functions.
  19. AC_CONFIG_FILES([Makefile
  20.                  base/Makefile
  21.                  src/Makefile])
  22. AC_OUTPUT
3.在目录下新建autogen.sh脚本
  1. #! /bin/sh
  2. #Regenerate the files autoconf /automake
  3. libtoolize --force --automake
  4. rm -f config.cache
  5. rm -f config.log
  6. aclocal
  7. autoheader
  8. autoconf
  9. automake -ac --foreign --add-missing --copy
 $chmod a+x autogen.sh
给脚本执行权限
4.运行脚本
$./autogen.sh
5.运行生成的configure文件
$./configure
6.$make

后面就可以像我们平时编译开源的源码包一样,进行configure,make ,make clean等操作

PS:本文只是使用step by step,具体为什么这样做,请查看相应automake帮助.
发布了31 篇原创文章 · 获赞 5 · 访问量 23万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章