`

使用autoconf和automake自动生成Makefile详细例解--研究

 
阅读更多
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT(test, 1.0.0, d@1.com)  
AC_CONFIG_SRCDIR([src/main/main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(test, 1.0.0)  
AC_PROG_LIBTOOL 

AC_PROG_CC

AC_OUTPUT(Makefile
           include/Makefile
           src/main/Makefile                                
           src/swap/Makefile
           src/myadd/Makefile
          )

 

test 是应用名称,main.c是应用入口,config,h是后来生成的

 

基本步骤已在上一篇讲过,这里结合一个稍微复杂的例子来进一步降解。

建立一个程序目录:test,然后按如下目录结构建立相关文件

test
|
|---- src
|     |---- main
|     |     |---- main.c
|     |
|     |---- myadd
|     |     |---- myadd.c
|     |
|     |---- swap
|           |---- swap.c
|
|---- include
       |---- swap.h
       |---- myadd.h

各文件内容如下:

/* ### src/main/main.c ### */
#include <stdio.h>
#include "swap.h"
#include "myadd.h"
int main()
{
    int a = 10;
    int b = 20;
    int sum = 0;
    printf("a is: %d, b is: %d\n",a,b);
    swap(&a,&b);
    printf("after swap,a is: %d, b is: %d\n",a,b);
    printf("start to run myadd(a,b)\n");
    sum = myadd(a,b);
    printf("a+b=%d\n",sum);
}

/* ### include/myadd.h ### */

#include <stdio.h>
int myadd(int a, int b)
{
    return (a+b);
}

/* ### include/swap.h ### */

#include <stdio.h>

void swap(int *a, int *b)
{
    int temp = 0;
    temp = *a;
    *a = *b;
    *b = temp;
}

/* ### include/myadd.h ### */
#include <stdio.h>
void myadd( int,int ) ;

/* ### include/swap.h ### */
#include <stdio.h>
void swap( int*,int* ) ;

生成Makefile要求:将swap链接为静态库,将myadd链接为动态库

>autoscan

修改 configure.scan为 configure.in,编辑内容(基本和前面相同,只需添加两句)

 

上述makefile没有生成顺序

接着执行下面的命令

>aclocal
>libtoolize
>autoheader
>autoconf
注:只有用到动态库时,才需要
>libtoolize

然后我们写 Makefile.am

/* ### Makefile.am ### */
SUBDIRS = include src/swap src/myadd src/main //这个有先后顺序

/* ### include/Makefile.am ### */
helloincludedir =$( includedir )
helloinclude_HEADERS =swap.h myadd.h

/* ### src/main/Makefile.am ### */
bin_PROGRAMS =test
test_SOURCES =main.c
test_LDADD =$( top_srcdir ) /src/swap/libswap.a
LIBS =-lmyadd
INCLUDES =-I$( top_srcdir ) /include
test_LDFLAGS =-L$( top_srcdir ) /src/myadd

/* ### src/swap/Makefile.am ### */
noinst_LIBRARIES =libswap.a
libswap_a_SOURCES =swap.c
INCLUDES =-I$( top_srcdir ) /include  

/* ### src/myadd/Makefile.am ### */
lib_LTLIBRARIES =libmyadd.la
libmyadd_la_SOURCES =myadd.c
INCLUDES =-I$( top_srcdir ) /include 

接着 automake 的话,无法通过的,还需要下面几个文件,我们暂时 touch 一下即可

>touch README NEWS AUTHORS ChangeLog

注意是在 $( top_srcdir ) 目录下面

然后
>automake --add-missing
>./configure

至此,所有 Makefile 文件应该全部生成
我们可以执行
>make
>make clean
>make install
>make uninstall
>make dist
>make ...

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics