autoconf automake 工具 管理项目

来源:互联网 发布:注册给排水知乎 编辑:程序博客网 时间:2024/05/22 03:40

 

这是一个涉及到静态库编译的文件.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 toproduce 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, andcompiler characteristics.

 

# Checks for library functions.

 

#AC_CONFIG_FILES([Makefile

#                 lib/Makefile

#                src/Makefile])

AC_OUTPUT(Makefilelib/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的联系方式等等。

当用makedist时候会生成一个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 issane... 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 outputfile name... a.out

checking for suffix of executables...

checking whether we are crosscompiling... no

checking for suffix of object files... o

checking whether we are using the GNU Ccompiler... yes

checking whether gcc accepts -g... yes

checking for gcc option to accept ISOC89... none needed

checking for style of include used bymake... 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 depfilescommands

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 生成可执行文件爱你