# # # 利用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_CXX AC_PROG_CC # Checks for libraries. # Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS([limits.h malloc.h stdlib.h string.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_HEADER_STDBOOL AC_C_CONST AC_C_INLINE # Checks for library functions. AC_FUNC_MALLOC AC_FUNC_REALLOC AC_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=1 lt_age=1 lt_revision=12 dist_version=0.1.12 AM_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"; then AC_DEFINE(HAVE_LIBDL, 1, [Define if DL lib is present]) DL_LIBS="-ldl" AC_SUBST(DL_LIBS) fi # Check for libm M_PRESENT="" AC_CHECK_LIB( m, sin, M_PRESENT="yes",, $M_LIBS -lm ) if test "x$M_PRESENT" = "xyes"; then AC_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"; then AC_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=1 lt_age=1 lt_revision=12 dist_version=0.1.12 AM_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_LIBTOOL AM_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 dl DL_PRESENT="" AC_CHECK_LIB( dl, dlopen, DL_PRESENT="yes",, $DL_LIBS -ldl ) if test "x$DL_PRESENT" = "xyes"; then AC_DEFINE(HAVE_LIBDL, 1, [Define if DL lib is present]) DL_LIBS="-ldl" AC_SUBST(DL_LIBS) fi # Check for libm M_PRESENT="" AC_CHECK_LIB( m, sin, M_PRESENT="yes",, $M_LIBS -lm ) if test "x$M_PRESENT" = "xyes"; then AC_DEFINE(HAVE_LIBM, 1, [Define if libm is present]) M_LIBS="-lm" AC_SUBST(M_LIBS) fi # Check for pthread (如示例中检测pthread,在生成的Makefile就会自动添加-lpthread) PTHREAD_PRESENT="" AC_CHECK_LIB( pthread, pthread_create, PTHREAD_PRESENT="yes",, $PTHREAD_LIBS -lpthread ) if test "x$PTHREAD_PRESENT" = "xyes"; then AC_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/sh set -x aclocal autoheader automake --foreign --add-missing --copy autoconf
保存后修改权限 chmod a+x autogen.sh
3.运行脚本./autogen.sh, 生成configure脚本. 这里可能会遇到错误, 可以根据错误提示作相应修改.
4.运行./configure脚本.自动生成src目录下的makefile文件
5. 切换到目录src, 运行make 自动在当前目录下建立.libs文件, 编程生成的库文件就保存在该目录下. make install 安装在默认目录 /usr/local/lib/下.
6.如果要生成其它的安装目录,Makefile.am就要这样写 MAINTAINERCLEANFILES = Makefile.in INCLUDES = -I../include lib_LTLIBRARIES = libtt.la libdir = $(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 |