SDB:使用 AutoTools 生成静态链接库
目录
简述
本文将演示使用 autotools 生成静态链接库的方法
准备
在自己准备的路径下建立目录,例如:
mkdir auto
cd auto
目录结构
auto lib/add.c include/test.h src/test.c
本目录的 Makefile.am 文件
SUBDIRS = lib src
include 目录下的内容
vi test.h
# include<stdio.h> extern int add(int,int);
lib目录下的内容
lib 下的 add.c
#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
#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
# -*- Autoconf -*-
# 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])
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
AC_PROG_RANLIB
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT([Makefile
lib/Makefile
src/Makefile])
保存 然后改名
mv configure.scan configure.in
生成 configure makefile
autoreconf -fvi
./configure
make
make install
检查结果。