使用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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章