[转] libtool的作用及应用
今天使用 autoconf 的时候遇到一个 libtool 的问题,觉得这个东西挺有意思,找了个文档过来,暂时记录。
转自:http://blog.csdn.net/larntin2002/article/details/1821430
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,非常方便!
[转] libtool的作用及应用的更多相关文章
- libtool
[从网上摘录的,忘了从哪摘的了] libtool常见于autoconf/automake,单独用的例子很少,所以我想仔细研究一下,为将来兄弟们看起来方便. 一.libtool的作用offer a ...
- libtool工具的使用
http://blog.sina.com.cn/s/blog_602f87700100fc8t.html libtool作用: libtool 是一个通用库支持脚本(/usr/bin/libtool) ...
- libtool的工作原理
libtool 是一个通用库支持脚本,将使用动态库的复杂性隐藏在统一.可移植的接口中:使用libtool的标准方法,可以在不同平台上创建并调用动态库.可以认为libtool是gcc的一个抽象,其包装了 ...
- if __name__== "__main__" 的意思(作用)python代码复用
if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog http://www.dabu.info/if-__-name__ ...
- (转载)linux下各个文件夹的作用
linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...
- github中的watch、star、fork的作用
[转自:http://www.jianshu.com/p/6c366b53ea41] 在每个 github 项目的右上角,都有三个按钮,分别是 watch.star.fork,但是有些刚开始使用 gi ...
- web.xml中welcome-file-list的作用
今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...
- web.xml中load-on-startup的作用
如下一段配置,熟悉DWR的再熟悉不过了:<servlet> <servlet-name>dwr-invoker</servlet-name> <ser ...
- SQLSERVER中NULL位图的作用
SQLSERVER中NULL位图的作用 首先感谢宋沄剑提供的文章和sqlskill网站:www.sqlskills.com,看下面文章之前请先看一下下面两篇文章 SQL Server误区30日谈-Da ...
随机推荐
- 同志亦凡人第五季/全集BQueer As Folk 5迅雷下载
同志亦凡人 第五季 Queer as Folk Season 5 (2005) 本季看点:这是一群生活在匹兹堡男人和男人,女人和女人的故事.在他们的王国里有各色人物.王国的国王Brian(葛尔?哈罗德 ...
- 好汉两个半第十二季/全集Two and a Half Men迅雷下载
本季Two And A Half Men Season 12 (2014)看点:<好汉两个半>的“半个”是因为第一季播出时杰克年纪太小,只能算半个.故事就在这三个主角和他们周围的女人中发生 ...
- Android Studio安装和配置的方法 & 错误解决
Android Studio终于到了1.0时代了,所以我也试着去学习了下如何使用它.上手确实比较麻烦,今天就分享下如何去下载并使用它. 一.下载 & 安装 大家可以去google给出的地址下载 ...
- Easyui numberbox获取焦点事件
Html文件: <input id="auctionBrandNoGetByHand" style="width:160px;" class=" ...
- HTML5实现简单圆周运动示例
一.使用JS实现圆周运动 根据指定圆心.半径,在定时器中移动固定的弧度,重绘圆圈的位置 源代码: <!DOCTYPE html> <html lang="en"& ...
- Java中线程池,你真的会用吗?
在<深入源码分析Java线程池的实现原理>这篇文章中,我们介绍过了Java中线程池的常见用法以及基本原理. 在文中有这样一段描述: 可以通过Executors静态工厂构建线程池,但一般不建 ...
- [已解决]下载chromium源码 download_from_google_storage 无法下载文件
当使用 gclient runhooks 后 出现下面的错误 File gs://chromium-gn/1088992877b3a13f25b61c8fc18e25296d8cab33 for sr ...
- iOS:调节系统的亮度
一.简单介绍 亮度是UIScreen的一个浮点型属性,而UIScreen是一个单例,所以这个亮度是全局的,任何一个地方改动,整个手机的亮度都会改变.这个亮度在iOS5.0后被苹果开放,开发者可以很方便 ...
- cesium原理篇(二)--网格划分【转】
转自:http://www.cnblogs.com/fuckgiser/p/5772077.html 上一篇我们从宏观上介绍了Cesium的渲染过程,本章延续上一章的内容,详细介绍一下Cesium网格 ...
- SharePoint自定义程序页面部署 不用重启IIS
SharePoint的部署方式默认是部署WSP包,尤其是有多个前端的时候WSP包的部署显得非常方便和快捷,但是WSP的部署需要重启整个IIS服务会造成SharePoint站点一段时间不能访问.结合自己 ...