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

本文详细介绍了一个使用Autoconf和Automake构建复杂项目的实例过程。包括如何组织源代码目录结构,编写配置文件configure.in,以及Makefile.am的具体内容。此外还讲解了如何生成静态库和动态库,并提供了完整的构建步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#                                               -*- 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 ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值