【从网上摘录的,忘了从哪摘的了】
 
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. 009_linuxC++之_友元函数

    (一)定义:友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数.类授予它的友元特别的访问权.通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其 ...

  2. MySQL 热快问题解决

    原文地址:http://blog.itpub.net/22664653/viewspace-1269948 一  背景 某个业务线 商品开放开用户申请免费试用,当某个商品特别吸引人时,比如iPhone ...

  3. Security Study

    1.WebGoat http://www.owasp.org.cn/ 学习Web应用漏洞最好的教程----WebGoat http://blog.csdn.net/bill_lee_sh_cn/art ...

  4. 网络1911、1912 C语言第4次作业--函数批改总结

    目录 网络1911.1912 C语言第4次作业--函数批改总结 一.评分规则 二.本次作业亮点 三.本次作业存在的问题 四.助教有话说 五.得分详情 网络1911.1912 C语言第4次作业--函数批 ...

  5. 2019.7.9 校内测试 T3 15数码问题

    这一次是交流测试?边交流边测试(滑稽 15数码问题 大家应该都玩过这个15数码的游戏吧,就在桌面小具库那里面哦. 一看到这个题就知道要GG,本着能骗点分的原则输出了 t 个无解,本来以为要爆零,没想到 ...

  6. oc Learning Blog

    http://www.cnblogs.com/heyonggang/p/3351269.html M了个J :http://www.cnblogs.com/mjios/tag/objective-c/ ...

  7. springboot+druid+sqlite遇到的问题

    1.springboot中使用druid查询sqlite报错getFetchDirection error ResultSet closed https://blog.csdn.net/u011943 ...

  8. zip flags 1 and 8 are not supported解决方案

    原因是因为使用了mac自带的软件打包成了zip,这种zip包unzip命令无法解压的. 所以解决方案就是使用zip命令进行压缩,zip -r 目标文件 源文件

  9. iview -- vue modal 显示到最顶层 层级

    给想要显示到顶层的modal 设置class属性 .my-modal-parent { position: fixed; // 浮动 z-index:; }

  10. CDH构建大数据平台-Kerberos高可用部署【完结篇】

    CDH构建大数据平台-Kerberos高可用部署[完结篇] 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.  一.安装Kerberos相关的软件包并同步配置文件 1>.实验环境 ...