您的位置:首页 > 其它

链接选项rpath,容易被忽略的一个功能

2014-05-07 18:28 323 查看

问题来源

最近在做一个项目的客户端的SDK,实现为一个动态链接库,其依赖于其他的若干个动态库文件。那么在利用SDK开发应用程序的时候,编译的时候除了链接SDK库本身,还得链接SDK库所依赖的库,这样编译命令里面得一一的添加这些库。

比如,SDK库的名称为liba.so, 其依赖于libb.so和libc.so,那么在编译应用程序的时候使用以下命令:

+++++++++++++++++++++++++++++++++++++++++++++

gcc -o test test.c -I. -L. -la -lb -lc

+++++++++++++++++++++++++++++++++++++++++++++

但是如果将SDK库即liba.so交给其他的开发人员,其他的开发人员可不想编译的时候,显示的链接liba.so所依赖的库。他们更愿意编译的时候,只显示地链接liba.so。

那么该怎么做呢?利用rpath这个链接选项!

解决方法

首先来man ld,查找链接选项rpath的含义:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

-rpath dir

Add a directory to the runtime library search path. This is used when linking an ELF executable with

shared objects. All -rpath arguments are concatenated and passed to the runtime linker, which uses

them to locate shared objects at runtime. The -rpath option is also used when locating shared objects

which are needed by shared objects explicitly included in the link; see the description of the

-rpath-link option. If -rpath is not used when linking an ELF executable, the contents of the environ-

ment variable "LD_RUN_PATH" will be used if it is defined.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

简单翻译下, rpath链接选项主要有两个功能:

(1)程序运行时,优先到rpath指定的目录去寻找依赖库

(2)程序链接时,在指定的目录中,隐式的链接那些动态库所需要的链接库。

往往我们都熟知第一个功能,忽略第二个功能。而第二个功能正是现在所需要的。

我们将liba.so,libb.so 和 libc.so拷贝的同一个目录中,然后利用rpath链接应用程序,这样编译便不需要显示的去链接liba.so所依赖的库了。

+++++++++++++++++++++++++++++++++++++++++++++

gcc -o test test.c -I. -L. -la -Wl,-rpath=.

+++++++++++++++++++++++++++++++++++++++++++++
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: