用automake工具自動生成Makefile的實例

個人一直覺得makefile比較難寫,而且每次做個項目是必不可少的,所以把自己用automake工具生成makefile的一個例子放在博客上,以便以後查閱

本博客參照了這個這篇博客的內容:http://www.ibm.com/developerworks/cn/linux/l-makefile/

環境:ubuntu10.10

文件夾內容:

example
    Makefile.am
    changelog
    src
        Makefile.am
        exmaple.c
    inc
        testlib_a.h
        testlib_la.h
    lib
        Makefile.am
        testlib_a.c
        testlib_la.c

一般的工程都是這樣的組織形式,其中Makefile.am生成Makefile 所必須的文件

1.在根目錄下執行命令:autoscan

可以看到根目錄下多了兩個文件:configure.scan、autoscan.log,我們需要將configure.scan重命名爲configure.in,然後編輯

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

AC_PREREQ([2.67])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/example.c])
AC_CONFIG_HEADERS([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
                 src/Makefile
                 src/lib/Makefile])
AC_OUTPUT
改爲:
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.67])
AC_INIT(automake_example, 0.1, [email protected])
AC_CONFIG_SRCDIR([src/example.c])
AC_CONFIG_HEADERS([config.h])

AM_INIT_AUTOMAKE()
# Checks for programs.
AC_PROG_CC

# Checks for libraries.
AC_PROG_RANLIB  #用了靜態庫
AC_PROG_LIBTOOL  #用了動態庫
# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile
                 src/Makefile
                 src/lib/Makefile])
AC_OUTPUT

2.執行autoconf,生成新文件configure

3.執行autoheader,生成config.h.in

4.執行automake --add-missing,出現錯誤 “required file `./ltmain.sh' not found”,執行一次 autoreconf -i就可以了,這個步驟生成了Makefile.in

5.執行./configure  它會將Makefile.in 和我們手寫的Makefile.am組合生成Makefile

6.現在已經生成了Makefile了,執行make就可以編譯整個工程了

下面把文件內容都貼出來,下面是一些文件的內容:

example/Makefile.am:

ROOT_DIR=$(shell /bin/pwd)
SUBDIRS=src/
AUTOMAKE_OPTIONS=foreign
INCLUDES=-I $(ROOT_DIR)/src/inc/
export INCLUDES


example/src/Makefile.am:

SUBDIRS=lib/
bin_PROGRAMS=example
example_SOURCES=example.c
AUTOMAKE_OPTIONS=foreign
#example_LDADD=-L$(top_srcdir)/src/lib/
example_LDADD=$(top_srcdir)/src/lib/libtestlib_a.a $(top_srcdir)/src/lib/libtestlib_la.la
#LIBS=$(top_srcdir)/src/lib/libtestlib_a.a


example/src/lib/Makefile.am:

noinst_LIBRARIES=libtestlib_a.a
libtestlib_a_a_SOURCES=testlib_a.c
AUTOMAKE_OPTIONS=foreign
noinst_LTLIBRARIES=libtestlib_la.la
libtestlib_la_la_SOURCES=testlib_la.c


example/src/example.c:

#include <stdio.h>
#include <testlib_a.h>
#include <testlib_la.h>
int main()
{
    printf("this is example\n");
    testlib_a();
    testlib_la();
    return 0;
}


example/src/lib/testlib_a.c:

#include <stdio.h>

void testlib_a()
{
    printf("this is testlib_a\n");
}


example/src/lib/testlib_la.c:

#include <stdio.h>

void testlib_la()
{
    printf("this is testlib_la\n");
}

 

example/src/inc/testlib_a.h:

void testlib_a();


example/src/inc/testlib_la.h:

void testlib_la();

實例已經上傳到csdn:http://download.csdn.net/download/liujian0616/4560225
 轉載請註明出處:http://blog.csdn.net/liujian0616/article/details/7955917

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章