链接选项-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,但是liba.so依赖库libb.so 和 libc.so,我们再编写makefile时不需要显示的链接所有的库,只要将liba.so,libb.so 和 libc.so拷贝的同一个目录中,然后利用rpath链接应用程序,这样编译便不需要显示的去链接liba.so所依赖的库了。

如:gcc -o test test.c -I. -L. -la -Wl,-rpath=.​/

但是需注意是链接器选项所以需要前面使用-Wl选项告诉编译器将后面的参数传递给链接器​。

转载于:https://www.cnblogs.com/luiz/p/6828826.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值