AutoTools 静态链接库

Jump to: navigation, search

简述

本文将演示使用autotools生成静态链接库的方法

下载

准备

在自己准备的路径下建立目录,例如:

mkdir auto

cd auto

目录结构

auto

lib/add.c

include/test.h

src/test.c


本目录的Makefile.am文件

SUBDIRS = lib src

include目录下的内容

vi test.h

  1. include<stdio.h>

extern int add(int,int);

lib目录下的内容

lib 下的add.c

  1. include<stdio.h>

int add(int a,int b)

{

    return a+b;

}

lib下的Makefile.am

noinst_LIBRARIES=libtest.a

libtest_a_SOURCES=add.c

src下的内容

编写test.c


  1. include "test.h"

int main() {

       int a = 3, b = 5;
       print("a+b=%d\n",add(a,b));  //静态库里的add函数
       return 0;

}


编写 Makefile.am

INCLUDES= -I../include

bin_PROGRAMS=test hello_SOURCES=test.c hello_LDADD=../lib/libtest.a


头文件的位置 ../include 执行文件 test 源代码文件 test.c 静态库的位置 ../lib/libtest.a

生成configure.scan

autoscan

vi configure.scan

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

AC_PREREQ(2.59)

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

AM_INIT_AUTOMAKE

AC_CONFIG_SRCDIR([src/hello.c])

AC_CONFIG_HEADER([config.h])

  1. Checks for programs.

AC_PROG_CC

  1. Checks for libraries.

AC_PROG_RANLIB

  1. Checks for header files.
  1. Checks for typedefs, structures, and compiler characteristics.
  1. Checks for library functions.

AC_OUTPUT([Makefile

                       lib/Makefile
                       src/Makefile])


保存 然后改名

mv configure.scan configure.in

生成configure makefile

autoreconf -fvi

./configure

make

make install

检查结果