必威电竞|足球世界杯竞猜平台

libtool
來源:互聯網

GNU libtool是一種GNU建構系統的程序設計工具,通過提供統一、可移植的命令行接口,隱藏了在不同計算平臺上編譯共享庫的命令行差異。它與autoconf和automake工具一起使用,用于生成可攜式的函式庫。

正文

官方下載地址? :? ftp://ftp.gnu.org/gnu/libtool/

相關程序和庫文件

相關程序:?libtoolize

相關庫文件:libltdl.[a,so].

簡短說明

libtool 提供通用的庫編譯支持。

libtoolize 提供了一種標準方式來將libtool支持加入到一個軟件包。

libltdl 隱藏 dlopening 庫的復雜細節。

依賴關系

Libtool 依賴于: Bash, Binutils, Coreutils, Diffutils,GCC, Glibc, Grep, Make, Sed.

利用libtool自動生成動態庫的Makefile的生成方法

# 利用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 編譯器 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@gmailcom[/email])

增加版本信息(為生成lib庫做準備)

lt_少校=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_天秤座( 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_天秤座( 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

增加依賴庫,這里就僅僅列舉了Pthread庫,生成的Makefile會自動加上-pthread

# 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_少校=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_天秤座( 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_天秤座( 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

# Checks for header files.

#AC_HEADER_DIRENT

#AC_HEADER_STDC

#AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/時間h unistd.h])

# Checks for typedefs, structures, and 編譯器 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)\""

天秤座_LTLIBRARIES = libtest.la

libtest_la_LDFLAGS = -version-info @lt_少校@:@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腳本. 這里可能會遇到錯誤, 可以根據錯誤提示作相應修改.(注意:如果您修改了Makefile.am中的項,那么就得重新執行這一步)

4.運行./configure腳本。自動生成src目錄下的makefile文件

5. 切換到目錄src, 運行make 自動在當前目錄下建立.libs文件, 編程生成的庫文件就保存在該目錄下.

make install 安裝在默認目錄 /usr/local/lib/下.

6.如果要生成其它的安裝目錄,Makefile.am就要這樣寫

MAINTAINERCLEANFILES = Makefile.in

INCLUDES = -I../include

天秤座_LTLIBRARIES = libtt.la

libdir = $(prefix)/lib/test? //這個就是安裝目錄

libtt_la_LDFLAGS = -version-info @lt_少校@:@lt_revision@:@lt_age@

libtt_la_LIBADD = @PTHREAD_LIBS@

libtt_la_SOURCES = \

tt.c \

video.c \

video.h

Makefile中的語法規則中還有很多宏定義,可以在Makefile的官方網站找到說明。

Libtool創建和使用linux下動態庫

文件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_11

int 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_111

void output(char *ss);

#endif

main.c:

#i nclude

#i nclude "hello.h"

#i nclude "fun.h"

void

main()

{

output("world");

printf("Test Value:%d\n", add(1, 2));

}

使用libtools創建和使用安裝動態庫步驟:

(1)

libtool --mode=compile gcc -g -O -c hello.c

libtool --mode=compile gcc -g -O -c fun.c

libtool --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.la

libtool --mode=install install -c libhello.la /usr/local/lib/libhello.la

libtool -n --mode=Finish /usr/local/lib

libtool install -c test /usr/local/bin/test

#安裝動態庫

然后就可以運行/usr/local/bin/test了,當然路徑可以任意設置,這是手動過程,寫成Makefile文件為:

OBJS = fun.o hello.o

LO_OBJS = main.lo fun.lo hello.lo

PACKAGE_VERSION = 1:1:1

LIBDIR=/usr/local/天秤座

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/811024HTML

參考資料 >

生活家百科家居網