目錄
project
|---base
| |--fun.c
| |--fun.h
| |--hello.c
| |--hello.h
|---src
|--main.c
主要使用automake,libtools進行編譯輔助,base目錄下是調用庫,src目錄是應用程序
文件內容如下:
fun.h
- ##fun.h
- #ifndef __FUN__H__
- #define __FUN__H__
- int add(int a, int b);
- #endif
- int add(int a, int b)
- {
- return a+b;
- }
- #ifndef __HELLO__H__
- #define __HELLO__H__
- void output(char *ss);
- #endif
- #include <stdio.h>
- void output(char *ss)
- {
- printf("HELLO %s/n",ss);
- return;
- }
- #include <stdio.h>
- #include "hello.h"
- #include "fun.h"
- int main()
- {
- output("world");
- printf("Test Value:%d/n",add(1,2));
- return 0;
- }
1。在project目錄下建立Makefile.am文件
- SUBDIRS = base src
在base目錄下新建Makefile.am文件
- lib_LTLIBRARIES = libtest.la
- libtest_la_SOURCES = fun.c fun.h hello.c hello.h
- INCLUDES= -I$(top_srcdir)/base
- bin_PROGRAMS = test
- test_SOURCES = main.c
- test_LDADD = $(top_builddir)/base/libtest.la
運行autoscan檢測生成configure.scan..(出錯不用理會),命名爲configure.in文件
$autoscan
$mv configure.scan configure.in
查看生成文件configure.in如下
#
- #
- # 利用libtool自動生成動態庫
- #
- 1. autoscan命令在當前目錄生成configure.scan文件, 內容爲:
- # -*- Autoconf -*-
- # Process this file with autoconf to produce a configure script.
- AC_PREREQ(2.59)
- AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
- AC_CONFIG_SRCDIR([src/main.c])
- AC_CONFIG_HEADER([config.h])
- # Checks for programs.
- AC_PROG_CC
- # Checks for libraries.
- # Checks for header files.
- # Checks for typedefs, structures, and compiler characteristics.
- # Checks for library functions.
- AC_CONFIG_FILES([Makefile
- base/Makefile
- src/Makefile])
- AC_OUTPUT
- #
- # 利用libtool自動生成動態庫
- #
- 1. autoscan命令在當前目錄生成configure.scan文件, 內容爲:
- # -*- Autoconf -*-
- # Process this file with autoconf to produce a configure script.
- AC_PREREQ(2.59)
- AC_INIT(test, 1.0, [email protected]) ###
- AC_CONFIG_SRCDIR([src/main.c])
- AM_CONFIG_HEADER([config.h]) ###
- AM_INIT_AUTOMAKE(test, 1.0) ###
- # Checks for programs.
- AC_PROG_CC
- AM_PROG_LIBTOOL ###
- # Checks for libraries.
- # Checks for header files.
- # Checks for typedefs, structures, and compiler characteristics.
- # Checks for library functions.
- AC_CONFIG_FILES([Makefile
- base/Makefile
- src/Makefile])
- AC_OUTPUT
- #! /bin/sh
- #Regenerate the files autoconf /automake
- libtoolize --force --automake
- rm -f config.cache
- rm -f config.log
- aclocal
- autoheader
- autoconf
- automake -ac --foreign --add-missing --copy
給腳本執行權限
4.運行腳本
$./autogen.sh
5.運行生成的configure文件
$./configure
6.$make
後面就可以像我們平時編譯開源的源碼包一樣,進行configure,make ,make clean等操作
PS:本文只是使用step by step,具體爲什麼這樣做,請查看相應automake幫助.