autoconf automake 工具 管理项目

本文介绍了一个涉及静态库编译的示例项目结构及配置过程,包括如何使用autoconf和automake工具生成Makefile,以及如何编译链接生成可执行文件。

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

 

这是一个涉及到静态库编译的文件.lib中的test.c文件将生成libhello.a hello.c 调用这个库文件,生成hello可执行文件。

 

//文件夹的树形结构如下图

delev/

|-- configure.in

|-- Makefile.am

|-- include

|   |-- hello.h

|-- lib

|   |-- Makefile.am

|   |-- test.c

`-- src

    |-- hello.c

    |-- Makefile.am

 

//下面是各个源文件的内容

delev/hello.c

 

#include "hello.h"

 

int main()

{

  print("hello/n");

  return 0;

}

 

// delev/lib/test.c

 

#include<stdio.h>

 

void print(char * msg)

{

   printf("message:%s/n",msg);

   return;

}

 

//delev/include/hello.h

 

extern void print(char *);

 

//以下时生成Makefile需要编辑的文件

---------------------------------------------------------------------

//delev/Makefile.am

SUBDIRS = lib src 

---------------------------------------------------------------------

SUBDIRS 是子目录

 

//delev/configure.in

#                                               -*- Autoconf -*-

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

 

AC_PREREQ([2.65])

AC_INIT(hello, 1.0, [yangtingjun1@gmail.com])

AM_INIT_AUTOMAKE

AC_CONFIG_SRCDIR([src/hello.c])

#AC_CONFIG_HEADERS([config.h])

 

# Checks for programs.

AC_PROG_CC

AC_PROG_RANLIB

# Checks for libraries.

 

# Checks for header files.

 

# Checks for typedefs, structures, and compiler characteristics.

 

# Checks for library functions.

 

#AC_CONFIG_FILES([Makefile

#                 lib/Makefile

#                src/Makefile])

AC_OUTPUT(Makefile lib/Makefile src/Makefile)

Confiure.in是一些宏的文件。他能设置源程序来适应各种不同的操作系统

根据不同的操作系统生成相应的Makefile文件。Configue.in的宏在被

autoconf处理后变成检查系统特性,环境变量,软件必须的参数

的脚步文件 configure

 

Confiure.in的结构必须是:AC_INIT开始,AC_OUTPUT结束

中间的顺序没有什么要求。

AC_INIT

xxxxxx

xxxxxx

AC_OUTPUT

AC_INIT(hello,1.0,[yangtingjun1@gmail.om])

初始化包的全称,指名打包的版本,打包的文件名,报告bug的联系方式等等。

当用make dist时候会生成一个hell_1.0.tar.gz文件

AC_CONFIG_SRCDIR()用于检查源文件是否存在

AC_INIT_AUTOMAKE

AC_PROG_CC 检查系统所用的编译其

AC_PROG_RANLIB 当要使用静态库的时候使用

AC_OUTPUT(file)这个宏说明要生成的文件名字。

 

//delev/src/Makefile.am

INCLUDES = -I ../include   要包括的头文件在哪里 , -I 表示这个头文件

bin_PROGRAMS = hello     要生成的可执行文件

hello_SOURCES = hello.c   生成可执行文件需要的文件,如果有多个可以空格分开

                                        注意  可执行文件是XX 就要写成XX_SOURCES

hello_LDADD = ../lib/libhello.a //需要连接的静态文件

 

---------------------------------------------------------------------

//delev/lib/Makefile.am

noinst_LIBRARIES=libhello.a

libhello_a_SOURCES = test.c

noinst_LIBRARIES 表示要安装的库

libhello_a_SOURCES表示生成这个库需要的文件

 

|-----------------------------------tips------------------------------------------|

对于可执行文件和静态库类型,如果只想编译,不想安装到系统中,可以用noinst_PROGRAMS代替bin_PROGRAMSnoinst_LIBRARIES代替lib_LIBRARIES

|---------------------------------------------------------------------------------|

执行过程

white@white-desktop:~/share/myprogram/automake/delev$ aclocal

white@white-desktop:~/share/myprogram/automake/delev$ autoconf

white@white-desktop:~/share/myprogram/automake/delev$ automake --add-missing

white@white-desktop:~/share/myprogram/automake/delev$ ./configure

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking for gcc... gcc

checking whether the C compiler works... yes

checking for C compiler default output file name... a.out

checking for suffix of executables...

checking whether we are cross compiling... no

checking for suffix of object files... o

checking whether we are using the GNU C compiler... yes

checking whether gcc accepts -g... yes

checking for gcc option to accept ISO C89... none needed

checking for style of include used by make... GNU

checking dependency style of gcc... gcc3

checking for ranlib... ranlib

configure: creating ./config.status

config.status: creating Makefile

config.status: creating lib/Makefile

config.status: creating src/Makefile

config.status: executing depfiles commands

white@white-desktop:~/share/myprogram/automake/delev$ make

 

 

下面是整个过程的图示:

 

*号的是可执行文件。

过程是

1---autoscan 生成configure.sacn 修改configure.scan

configure.scan 改名为configure.in

2---运行aclocal 脚本 生成aclocal.m4

3---运行autoconf 生成configure脚本

 

4---编写Makefile.am文件。

5---执行 automake –add-missing 生成 makefile.in等文件

 

6---执行./configure 生成 Makefile 文件。

7---make 生成可执行文件爱你

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值