# 利用libtool自动生成动态库1. autoscan命令在当前目录生成configure.scan文件, 内容为:# -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_PREREQ(2.57)AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)AC_CONFIG_SRCDIR([src/bot.h])AC_CONFIG_HEADER([config.h])# Checks for programs.AC_PROG_CXXAC_PROG_CC# Checks for libraries.# Checks for header files.AC_HEADER_STDCAC_CHECK_HEADERS([limits.h malloc.h stdlib.h string.h unistd.h])# Checks for typedefs, structures, and compiler characteristics.AC_HEADER_STDBOOLAC_C_CONSTAC_C_INLINE# Checks for library functions.AC_FUNC_MALLOCAC_FUNC_REALLOCAC_CHECK_FUNCS([memset strcasecmp strchr strdup])AC_OUTPUT将其改名为configure.ac 然后修改:configure.ac 文件是 autoconf 的输入文件,经过 autoconf 处理,展开里面的 m4宏,输出的是 configure 脚本。第 4 行声明本文件要求的 autoconf 版本,因为本例使用了新版本 2.57,所以在此注明。第 5 行 AC_INIT 宏用来定义软件的名称和版本等信息AC_INIT([test], 1.0, [email]linhanzu@gmail.com[/email])增加版本信息(为生成lib库做准备)lt_major=1lt_age=1lt_revision=12dist_version=0.1.12AM_INIT_AUTOMAKE(test, $dist_version) //自动生成Makefile文件增加宏, 打开共享库AC_PROG_LIBTOOL# Check for dl DL_PRESENT=""AC_CHECK_LIB( dl, dlopen, DL_PRESENT="yes",, $DL_LIBS -ldl )if test "x$DL_PRESENT" = "xyes"; thenAC_DEFINE(HAVE_LIBDL, 1, [Define if DL lib is present])DL_LIBS="-ldl"AC_SUBST(DL_LIBS)fi# Check for libmM_PRESENT=""AC_CHECK_LIB( m, sin, M_PRESENT="yes",, $M_LIBS -lm )if test "x$M_PRESENT" = "xyes"; thenAC_DEFINE(HAVE_LIBM, 1, [Define if libm is present])M_LIBS="-lm"AC_SUBST(M_LIBS)fi增加依赖库,这里就仅仅列举了pthread库,生成的Makefile会自动加上-pthread# Check for pthreadPTHREAD_PRESENT=""AC_CHECK_LIB( pthread, pthread_create, PTHREAD_PRESENT="yes",, $PTHREAD_LIBS-lpthread )if test "x$PTHREAD_PRESENT" = "xyes"; thenAC_DEFINE(HAVE_LIBPTHREAD, 1, [Define if libpthread is present])PTHREAD_LIBS="-lpthread"AC_SUBST(PTHREAD_LIBS)fi要生成项目工程目录和其它目录下的Makefile 文件, 必需加入AM_CONFIG_FILES的宏:例如: AC_CONFIG_FILES([Makefile src/Makefile data/Makefile docs/Makefile])修改完后Makefile.ac如下:# -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_PREREQ(2.57)AC_INIT([test],[1.0],[[email]arne_caspari@users.sourceforge.net[/email]])AM_CONFIG_HEADER(config.h)lt_major=1lt_age=1lt_revision=12dist_version=0.1.12AM_INIT_AUTOMAKE(test, $dist_version)AC_SUBST(lt_major)AC_SUBST(lt_revision)AC_SUBST(lt_age)# Checks for programs.#AC_PROG_CC#AC_PROG_INSTALL#AC_PROG_LN_S#AC_PROG_LIBTOOLAM_PROG_LIBTOOL# Checks for libraries.pkg_modules="gtk+-2.0 >= 2.0.0"PKG_CHECK_MODULES(GTK_PACKAGE, [$pkg_modules], HAVE_GTK2="yes", HAVE_GTK2="no" )AC_SUBST(GTK_PACKAGE_CFLAGS)AC_SUBST(GTK_PACKAGE_LIBS)# Check for dlDL_PRESENT=""AC_CHECK_LIB( dl, dlopen, DL_PRESENT="yes",, $DL_LIBS -ldl )if test "x$DL_PRESENT" = "xyes"; thenAC_DEFINE(HAVE_LIBDL, 1, [Define if DL lib is present])DL_LIBS="-ldl"AC_SUBST(DL_LIBS)fi# Check for libmM_PRESENT=""AC_CHECK_LIB( m, sin, M_PRESENT="yes",, $M_LIBS -lm )if test "x$M_PRESENT" = "xyes"; thenAC_DEFINE(HAVE_LIBM, 1, [Define if libm is present])M_LIBS="-lm"AC_SUBST(M_LIBS)fi# Check for pthread PTHREAD_PRESENT=""AC_CHECK_LIB( pthread, pthread_create, PTHREAD_PRESENT="yes",, $PTHREAD_LIBS-lpthread )if test "x$PTHREAD_PRESENT" = "xyes"; thenAC_DEFINE(HAVE_LIBPTHREAD, 1, [Define if libpthread is present])PTHREAD_LIBS="-lpthread"AC_SUBST(PTHREAD_LIBS)fi# Checks for header files.#AC_HEADER_DIRENT#AC_HEADER_STDC#AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/time.h unistd.h])# Checks for typedefs, structures, and compiler characteristics.#AC_TYPE_PID_T#AC_TYPE_SIZE_T#AC_HEADER_TIME# Checks for library functions.#AC_FUNC_CLOSEDIR_VOID#AC_FUNC_MALLOC#AC_CHECK_FUNCS([memset strstr])AC_CONFIG_FILES([Makefile src/Makefile data/Makefile doc/Makefile])AC_OUTPUT
2.生成各目录下的Makefile.am文件./Makefile.am //工程目录下 SUBDIR = src data doc../src/Makefile.am //源码目录下 MAINTAINERCLEANFILES = Makefile.in INCLUDES = -I../include CPPFLAGS=-DINSTALL_PREFIX="/"$(prefix)/"" lib_LTLIBRARIES = libtest.la libtest_la_LDFLAGS = -version-info @lt_major@:@lt_revision@:@lt_age@ libtest_la_SOURCES = / test.c / test_private.h / check_match.c / check_match.h / test_helpers.c / test_helpers.h / debug.h libtest_la_LIBADD = / @DL_LIBS@ / @M_LIBS@
3. 生成autogen.sh脚本, 内容#! /bin/shset -xaclocalautoheaderautomake --foreign --add-missing --copyautoconf
保存后修改权限 chmod a+x autogen.sh
3.运行脚本./autogen.sh, 生成configure脚本. 这里可能会遇到错误, 可以根据错误提示作相应修改.(注意:如果您修改了Makefile.am中的项,那么就得重新执行这一步)
4.运行./configure脚本.自动生成src目录下的makefile文件
5. 切换到目录src, 运行make 自动在当前目录下建立.libs文件, 编程生成的库文件就保存在该目录下. make install 安装在默认目录 /usr/local/lib/下.
6.如果要生成其它的安装目录,Makefile.am就要这样写MAINTAINERCLEANFILES = Makefile.inINCLUDES = -I../include lib_LTLIBRARIES = libtt.lalibdir = $(prefix)/lib/test //这个就是安装目录libtt_la_LDFLAGS = -version-info @lt_major@:@lt_revision@:@lt_age@libtt_la_LIBADD = @PTHREAD_LIBS@libtt_la_SOURCES = / tt.c / video.c / video.h
Makefile中的语法规则中还有很多宏定义,可以在Makefile的官方网站找到说明。
文件fun.c,fun.h,hello.c,hello.h,main.c动态库函数都在fun.c和hello.c里面fun.c:
int add(int a, int b){ return a+b;}
fun.h:
#ifndef _FUN_H_11#define _FUN_H_11int add(int a, int b);#endif----------------------------
hello.c:
#i nclude void output(char *ss){ printf("HELLO %s/n", ss);}
hello.h
#ifndef HELLO_H_111#define HELLO_H_111void output(char *ss);#endif----------------------------
main.c:
#i nclude #i nclude "hello.h"#i nclude "fun.h"voidmain(){ output("world"); printf("Test Value:%d/n", add(1, 2));}
使用libtools创建和使用安装动态库步骤:
(1)libtool --mode=compile gcc -g -O -c hello.clibtool --mode=compile gcc -g -O -c fun.clibtool --mode=compile gcc -g -O -c main.c#生成各自的o文件
(2)libtool --mode=link gcc -g -O -o libhello.la hello.lo fun.lo -rpath /usr/local/lib -lm #连接成动态库文件
(3)libtool --mode=link gcc -g -O -o test main.o libhello.la -lm#连接生成可执行文件test
(4)libtool --mode=install cp libhello.la /usr/local/lib/libhello.lalibtool --mode=install install -c libhello.la /usr/local/lib/libhello.lalibtool -n --mode=finish /usr/local/liblibtool install -c test /usr/local/bin/test#安装动态库
然后就可以运行/usr/local/bin/test了,当然路径可以任意设置,这是手动过程,写成Makefile文件为:
OBJS = fun.o hello.oLO_OBJS = main.lo fun.lo hello.loPACKAGE_VERSION = 1:1:1LIBDIR=/usr/local/lib
all : test
install : libhello.la test : libhello.la main.o libtool --mode=link gcc -g -O -o test main.o libhello.la -lm
libhello.la : $(OBJS) libtool gcc -g -O -o libhello.la $(LO_OBJS) -rpath ${LIBDIR} -lm -version-info ${PACKAGE_VERSION}
main.o : main.c fun.h hello.h libtool --mode=compile gcc -g -O -c main.c
fun.o : fun.c fun.h libtool --mode=compile gcc -g -O -c fun.c
hello.o : hello.c hello.h libtool --mode=compile gcc -g -O -c hello.c
clean : @rm -f OBJ/* lib*.a *~ *core *.lo *.o *.la @rm -rf .libs
http://blog.csdn.net/ruixj/archive/2006/07/05/881180.aspx
http://www.chinaunix.net/jh/4/811024.html
