【从网上摘录的,忘了从哪摘的了】
 
libtool常见于autoconf/automake,单独用的例子很少,所以我想仔细研究一下,为将来兄弟们看起来方便。
 
一。libtool的作用
offer a standard procedure for creating shared libraries on different platforms
libtool 是一个通用库支持脚本,将使用动态库的复杂性隐藏在统一、可移植的接口中,也就是说,你可以通过如下所示的标准方法,在不同平台上创建并调用动态库,我们 可以认为libtool是gcc的一个抽象,也就是说,它包装了gcc或者其他的任何编译器,用户无需知道细节,只要告诉libtool说我需要要编译哪 些库即可,并且,它只与libtool文件打交道,例如lo、la为后缀的文件。
 
二。libtool的使用
1.Creating object files
# libtool --mode=compile gcc -g -O -c foo.c
 gcc -g -O -c foo.c  -fPIC -DPIC -o .libs/foo.o
 gcc -g -O -c foo.c -o foo.o >/dev/null 2>&1
# libtool --mode=compile gcc -g -O -c hello.c
 gcc -g -O -c hello.c  -fPIC -DPIC -o .libs/hello.o
 gcc -g -O -c hello.c -o hello.o >/dev/null 2>&1
【说明】libtool编译出两个版本的relocatable object,一个是fPIC(位置无关的),放在.libs目录下;另一个则是普通的,放在本地。
 
2.linking shared library
# libtool --mode=link --tag=CC gcc -g -O -o libhello.la -rpath /usr/local/lib foo.lo
 rm -fr  .libs/libhello.a .libs/libhello.la .libs/libhello.lai .libs/libhello.so libs/libhello.so.0 .libs/libhello.so.0.0.0
 gcc -shared  .libs/foo.o   -Wl,-soname -Wl,libhello.so.0 -o .libs/libhello.so.0.0.0
 (cd .libs && rm -f libhello.so.0 && ln -s libhello.so.0.0.0 libhello.so.0)
 (cd .libs && rm -f libhello.so && ln -s libhello.so.0.0.0 libhello.so)
 ar cru .libs/libhello.a  foo.o
 ranlib .libs/libhello.a
 creating libhello.la
 (cd .libs && rm -f libhello.la && ln -s ../libhello.la libhello.la)
【说明】link出两个共享库,一个是static,一个则是dynamic;需要注意的是,-rpath必须有才能产生dynamic库来,如果用-static,则只创建static库。
 
ranlib的作用:
On some older UNIX systems, ranlib added a table of contents to archive libraries, which converted each archive to a form that could be linked more rapidly. This is no longer needed as the ar command automatically provides all the functionality ranlib used to provide.
在一些旧版本的系统上,ranlib负责把静态库转换为其他的某种格式,使得新的库能够更快的链接;现在ar命令已经包含了上述功能;
This command is provided as a convenience for software developers who need to maintain Makefiles that are portable across a variety of operating systems.
为了兼容性,在makefile中还是保留ranlib
 
3.install shared library
libtool --mode=install cp libhello.la /usr/local/lib/libhello.la
libtool --mode=install install -c libhello.la /usr/local/lib/libhello.la
两个命令都可以,效果相同
 
4.linking executable file
# libtool --mode=link gcc -g -O -o hello hello.lo -rpath /usr/local/lib libhello.la
 gcc -g -O -o .libs/hello .libs/hello.o  ./.libs/libhello.so
 creating hello
 -rpath项负责添加运行时库路径,否则只能手工修改LD_LIBRARY_PATH环境变量了。
 验证一下:
# ldd .libs/hello
        linux-gate.so.1 =>  (0xffffe000)
        libhello.so.0 => /usr/local/lib/libhello.so.0 (0x40019000)
        libc.so.6 => /lib/tls/libc.so.6 (0x40031000)
        /lib/ld-linux.so.2 (0x40000000)
 
5.install executable file       
#libtool --mode=install cp hello /usr/local/bin/hello
安装可执行程序。
 
6.运行
libtool --mode=execute hello
或直接运行hello
注意:此处hello已经安装在/usr/local/bin下了,可以用which hello来查看
 
【附】源码
foo.c
#include <stdio.h>
char msg[128]="Hello world";
void print()
{
        printf("%s/n", msg);
}
 
hello.c:
#include <stdio.h>
extern char msg[128];
extern void print();
int main()
{
        print();
}
 
Makefile:
LO_OBJS = foo.lo
PACKAGE_VERSION = 1:1:1
LIBDIR=/usr/local/lib
BINDIR=/usr/local/bin
 
all : hello
 
install : libhello.la hello
  libtool --mode=install install -c libhello.la
 
${LIBDIR}/libhello.la
  libtool --mode=install cp hello ${BINDIR}/hello
 
uninstall : ${LIBDIR}/libhello.la ${BINDIR}/hello
  libtool --mode=uninstall /bin/rm ${LIBDIR}/libhello.la
  libtool --mode=uninstall /bin/rm ${BINDIR}/hello
 
hello : libhello.la hello.o
  libtool --mode=install install -c libhello.la
 
${LIBDIR}/libhello.la
  libtool --mode=link gcc -g -O -o hello hello.o -rpath ${LIBDIR} libhello.la
 
libhello.la : $(LO_OBJS)
   libtool  --mode=link --tag=CC gcc -g -O -o libhello.la
 
$(LO_OBJS) -rpath ${LIBDIR} ${PACKAGE_VERSION}
 
foo.lo : foo.c
   libtool --mode=compile gcc -g -O -c foo.c
 
hello.lo : hello.c
   libtool --mode=compile gcc -g -O -c hello.c
 
clean :
  rm -f lib*.a *~ *core *.lo *.o *.la hello
  rm -rf .libs
 
这样,用户可以用make编译,make install/uninstall安装/卸载,make clean清除编译临时文件,安装成功后,可以直接执行hello,不必指明路径也不必再另设环境变量LD_LIBRARY_PATH,非常方便! 
 
gcc -MM kvm_main.c
 

libtool的更多相关文章

  1. 手动安装m4, autoconf, automake, libtool

    转自http://ruby-china.org/topics/2434 系列文章原载于自己的博客,TOPI.CO (http://topi.co) ,某天不小心就push错啦,懒得从头再来,上传到Ru ...

  2. libtool: Version mismatch error 解决

    在编译一个软件的时候,在 ./configure 和 make  之后可能会出现如下错误: libtool: Version mismatch error.  This is libtool 2.4. ...

  3. 搭建php环境时解决jpeg6 make: ./libtool:命令未找到

    搭建php环境时解决jpeg6 make: ./libtool:命令未找到 [root@bogon jpeg-6b]# make; make install ./libtool --mode=comp ...

  4. ar命令提取.a时刻,一个错误 is a fat file (use libtool(1) or lipo(1) and ar(1) on it)

    在减压.a当文件,据报一个类别似 xxx.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)的错误,经过查找资料,原来是由于该.a文 ...

  5. 源码编译安装libtool工具

    1. 获取源码 wget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz tar xvf libtool-2.4.6.tar.gz -C ~ ...

  6. libtool 创建库的工具

    libtool 创建库的工具 1. 背景 在不同的系统中建立动态链接库的方法有很大的差别,这主要是因为每个系统对动态链接库的用法和实现并不相同,以及编译器对动态链接库支持的选项也不太一样. 对于开发人 ...

  7. 安装 Autoconf, Automake & Libtool

    今天在使用sudo apt-get install命令安装autoconf和automake时,出现了问题,说是不能sudo apt-get install安装这些软件似乎不是最新的.由此,我通过搜索 ...

  8. 使用 GNU Libtool 创建库

    这篇文档向大家介绍 GNU Libtool 的用途及基本使用方法,同时描述如何结合 GNU Autoconf 和 Automake 来使用 Libtool. 3 评论: 吴 小虎, 程序员, 天用唯勤 ...

  9. [转] libtool的作用及应用

    今天使用 autoconf 的时候遇到一个 libtool 的问题,觉得这个东西挺有意思,找了个文档过来,暂时记录. 转自:http://blog.csdn.net/larntin2002/artic ...

  10. gdb调试libtool封装的可执行文件

    http://www.gnu.org/software/libtool/manual/html_node/Debugging-executables.html 3.4 Debugging execut ...

随机推荐

  1. 【线性代数】6-1:特征值介绍(Introduction to Eigenvalues)

    title: [线性代数]6-1:特征值介绍(Introduction to Eigenvalues) categories: Mathematic Linear Algebra keywords: ...

  2. unix/linux 进程间文件锁

    转自 http://www.cnblogs.com/hjslovewcl/archive/2011/03/14/2314333.html 有三种不同的文件锁,这三种都是“咨询性”的,也就是说它们依靠程 ...

  3. 面向对象(OOP)笔记

    1.本质:以类的方式组织代码,以对象的方式组织(封装)数据 2.对象:是具体的事物 3.类:是对对象的抽象(抽象 抽出象的部分) 先有具体的对象,然后抽象各个对象之间象的部分,归纳出类 通过类再认识其 ...

  4. 1937:【06NOIP普及组】数列

    woc 太捞了简直捞的一匹 我居然会写博客 反正呀没有人看 随便写写喽

  5. WPF中,Grid与Table的区别(英文)-转载

    原文地址:http://blog.csdn.net/johnsuna/article/details/1742799 How is Grid Different from Table?Table an ...

  6. MySQL个人用户的安装配置详解

    1. 我的版本是 MySQL 5.7.26.0 ,因为据说 MySQL 8 的性能虽然强悍,但是兼容性还是有问题,而且发布时间不够长,没有普及,就暂时用着5.7版本. (1) 下载地址,选择使用msi ...

  7. Protocol Buffers学习笔记

    Protocol Buffers学习笔记 1. 简介 Protocol Buffers是google发明的一种数据交换格式,独立于语言,独立于平台.与其他的数据交换格式有所不同,Protocol Bu ...

  8. Methods for Identifying Out-of-Trend Results in Ongoing Stability Data

     python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  9. kotlin之MutableMap委托

    fun main(arg: Array<String>) { val map = mutableMapOf("name" to "tom", ) v ...

  10. c++ template Queue

    #pragma once#include <iostream>#include <iomanip> using namespace std; template<class ...