GCC 中 -L、-rpath和-rpath-link的区别

来源 http://blog.csdn.net/q1302182594/article/details/42102961

关于这3个参数的说明,有不少资料,但是看完了还是觉得模糊,分不清它们的区别。本文将用实验的方法去探讨这3个参数的区别。

1、三个.c文件

1.1 world.c

  1. #include<stdio.h>
  2. void world(void) {
  3. printf("world.\n");
  4. }

1.2 hello.c

  1. #include <stdio.h>
  2. void world(void);
  3. void hello(void) {
  4. printf("hello\n");
  5. world();
  6. }

1.3 test.c

  1. void main(void) {
  2. hello();
  3. }

2、生成动态库

参照《Linux静态库与动态库制作》,将hello.c和world.c分别生成动态库

  1. ubuntu $ gcc -c hello.c world.c
  2. ubuntu $ gcc -shared -o libhello.so hello.o
  3. ubuntu $ gcc -shared -o libworld.so world.o

这时,生成的文件及其依赖如下图:

由上图可见,libhello.so和libworld都依赖于linux-gate.so.1、libc.so.6以及/lib/ld-linux.so.2,并且这3个库的路径都以及硬编码进libhello.so和libworld.so中了(=>右边的部分)。

然而,虽然libhello.so中调用了libworld.so的函数,但是在上图中并没有显示出此关系。为了达到使libhello.so依赖于libworld.so的目的,在生成libhello.so时要链接到libworld.so:

  1. ubuntu $ gcc -shared -o libworld.so world.o -lhello -L .

此时,再使用ldd查看libhello.so的依赖:


    由上图可见,此时libhello.so已经依赖于libworld.so。

3、编译test.c

3.1 -L

由于test.c直接依赖于libhello.so,因此使用-lhello -L

  1. ubuntu $ gcc test.c -lhello -L .

结果如下图:

由上图可见已经在-L指定的路径找打了libhello.so,只是libhello.so还需要libworld.so。虽然它都在同一目录下,但是还是没有办法自动找到libworld.so。

那么,能不能使用-lworld将libworld.so也一并链接到test.c中呢?下面做一个尝试:

  1. ubuntu $ gcc test.c -lhello -lworld -L .

没有报错,成功生成a.out。

执行a.out并且使用ldd查看a.out的依赖:

由上图可见,虽然使用-lworld参数将libworld.so链接到了a.out中,但是上面只显示a.out依赖于libhello.so。由于找不到libhello.so(=> not found)的路径,因此需要设置环境变量LD_LIBRARY_PATH

  1. ubuntu export LD_LIBRARY_PATH=/home/liyihai/documents

再次执行a.out并使用ldd命令查看a.out的依赖库:

由上图可见,libhello.so已经通过LD_LIBRARY_PATH环境变量找到,并且libworld.so也出现在a.out的依赖中!

结论:-L指定的是链接时的库路径,生成的可执行文件在运行时库的路径由LD_LIBRARY_PATH环境变量指定。

3.2 -rpath

根据3.1第1张图的提示,由于libhello.so依赖于libworld.so,可以只用-rpath或者-rpath-link来指定。这里先使用-rpath。

先清空LD_LIBRARY_PATH环境变量,然后重新编译test.c并且带上-rpath参数:

  1. ubuntu $ export LD_LIBRARY_PATH=
  2. ubuntu $ gcc test.c -lhello -L . -Wl,-rpath .

执行a.out,并且使用ldd命令查看a.out的依赖:

由上图可见,虽然没有明确指出链接libworld.so,但是libworld.so还是出现在a.out的依赖中。

另外,虽然LD_LIBRARY_PATH已经清空,但是a.out还是可以执行,这说明库的路径已经被编译进a.out中了。需要注意的是,libhello.so和libworld.so的路径都是通过-rpath指定的路径找到的。

3.2.1 实验1

这时候,如果libhello.so和libworld.so的路径改变了,将会发生什么情况呢?下面做一个实验。

创建一个lib_tmp目录,然后将libhello.so和libworld.so移动进这个目录。

  1. ubuntu $ mdir lib_tmp
  2. ubuntu $ mv libhello.so lib_tmp/
  3. ubuntu $ mv libworld.so lib_tmp/

这时再执行a.out时,提示找不动态库,使用ldd命令查看a.out的库路径:

由上图红色圈部分可见,libhello.so的路径是not found的,并且libworld.so没有出现在其中。这和3.1的情况是相同的。

究其原因,就是要先找到libhello.so再去找libworl.so,因为是libhello.so依赖于libworld.so,而不是a.out依赖于libworld.so。

由此可见,使用了-rpath参数指定库的路径后,生成的可执行文件的依赖库路径并非就固定不变了。而是执行时先从-rpath指定的路径去找依赖库,如果找不到,还是会报not fund。

那么,这时候,LD_LIBRARY_PATH对a.out是否还有影响呢?下面将LD_LIBRARY_PATH设为当前libhello.so和libworld.so所在的路径

  1. ubuntu $ export LD_LIBRARY_PATH=./lib_tmp

再次执行a.out,并且使用ldd查看此时a.out的依赖库路径:

由上图可见LD_LIBRARY_PATH还是起作用的!由上图可见,和使用-rpath指定路径的效果是一样的。

3.2.2 实验2

将LD_LIBRARY_PATH清空,然后将libhello.so移动到lib_tmp中,而libworld.so则留在documents目录中。

执行a.out,并且使用ldd查看此时a.out的依赖库:

由上图可见,找不到libhello.so。这时,再指定LD_LIBRARY_PATH的路径为libhello.so所在的路径:

  1. ubuntu $ export LD_LIBRARY_PATH=./lib_tmp/

再次执行a.out,并且使用ldd查看其依赖库:

由上图可见,一切又恢复了正常。此时,libhello.so是通过LD_LIBRARY_PATH找到的,而libworld.so则是通过-rpath指定的路径找到的。

3.2.3 回顾

其实,经过测试,在3.1小节中,如果先指定LD_LIBRARY_PATH的值为libhello.so和libworld.so所在的路径,然后再编译test.c(执行3.1节的第1条编译命令),是可以成功编译的,并不会报3.1小节第1张图的那种错误。也就是说,LD_LIBRARY_PATH不仅指定可执行文件的库路径,还指定了库所依赖于其它库的路径。

3.2.4 结论

并非指定-rpath参数后,就抛弃LD_LIBRARY_PATH环境变量,只是多了个可选的依赖库路径而已。

3.3 -rpath-link

先将LD_LIBRARY_PATH的值清空,然后将libworld.so移动到lib_tmp目录中,而libhello.so则留在documents目录中,使用以下命令对test.c进行编译:

  1. ubuntu $ gcc test.c -lhello  -L . -Wl,-rpath-link ./lib_tmp

执行a.out并且使用ldd查看a.out的依赖库:

找不到 libhello.so,这在预料之中。下面指定LD_LIBRARY_PATH的值为libhello.so的路径,然后在执行a.out,并且查看a.out的依赖:

由上图可见,libhello.so已经通过LD_LIBRARY_PATH找到,但是libworld.so由于没有在LD_LIBRARY_PATH指定的路径中,而且编译时a.out又没有包含库的路径,因此找不到。这

对比3.2.2可以得出结论:-rpath和-rpath-link都可以在链接时指定库的路径;但是运行可执行文件时,-rpath-link指定的路径就不再有效(链接器没有将库的路径包含进可执行文件中),而-rpath指定的路径还有效(因为链接器已经将库的路径包含在可执行文件中了。)

最后,不管使用了-rpath还是-rpath-link,LD_LIBRARY_PATH还是有效的。

4、ld命令的man手册

4.1  -rpath=dir

  1. Add a directory to the runtime library search path.  This is used when linking an ELF executable with shared objects.
  2. All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at
  3. runtime.  The -rpath option is also used when locating shared objects which are needed by shared objects explicitly
  4. included in the link; see the description of the -rpath-link option.  If -rpath is not used when linking an ELF
  5. executable, the contents of the environment variable "LD_RUN_PATH" will be used if it is defined.
  6. The -rpath option may also be used on SunOS.  By default, on SunOS, the linker will form a runtime search path out of
  7. all the -L options it is given.  If a -rpath option is used, the runtime search path will be formed exclusively using
  8. the -rpath options, ignoring the -L options.  This can be useful when using gcc, which adds many -L options which may
  9. be on NFS mounted file systems.
  10. For compatibility with other ELF linkers, if the -R option is followed by a directory name, rather than a file name, it
  11. is treated as the -rpath option.

4.2  -rpath-link=dir

  1. When using ELF or SunOS, one shared library may require another.  This happens when an "ld -shared" link includes a
  2. shared library as one of the input files.
  3. When the linker encounters such a dependency when doing a non-shared, non-relocatable link, it will automatically try
  4. to locate the required shared library and include it in the link, if it is not included explicitly.  In such a case,
  5. the -rpath-link option specifies the first set of directories to search.  The -rpath-link option may specify a sequence
  6. of directory names either by specifying a list of names separated by colons, or by appearing multiple times.
  7. This option should be used with caution as it overrides the search path that may have been hard compiled into a shared
  8. library. In such a case it is possible to use unintentionally a different search path than the runtime linker would do.

4.3  search paths

  1. The linker uses the following search paths to locate required shared libraries:
  2. 1.  Any directories specified by -rpath-link options.
  3. 2.  Any directories specified by -rpath options.  The difference between -rpath and -rpath-link is that directories
  4. specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is
  5. only effective at link time. Searching -rpath in this way is only supported by native linkers and cross linkers
  6. which have been configured with the --with-sysroot option.
  7. 3.  On an ELF system, for native linkers, if the -rpath and -rpath-link options were not used, search the contents of
  8. the environment variable "LD_RUN_PATH".
  9. 4.  On SunOS, if the -rpath option was not used, search any directories specified using -L options.
  10. 5.  For a native linker, search the contents of the environment variable "LD_LIBRARY_PATH".
  11. 6.  For a native ELF linker, the directories in "DT_RUNPATH" or "DT_RPATH" of a shared library are searched for shared
  12. libraries needed by it. The "DT_RPATH" entries are ignored if "DT_RUNPATH" entries exist.
  13. 7.  The default directories, normally /lib and /usr/lib.
  14. 8.  For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list of directories found in that
  15. file.

参考资料

[1]动态库的链接和链接选项-L,-rpath-link,-rpath

[2]ld的-rpath与-rpath-link选项

GCC 中 -L、-rpath和-rpath-link的区别的更多相关文章

  1. gcc中动态库和静态库的链接顺序

    so文件:动态库a文件: 静态库exe文件:可执行程序(linux下以文件属性来标示是否是可执行文件,与后缀名无关) 经过自己写的一些测试程序,大致了解了下gcc中链接顺序问题,总结出以下几点:1,动 ...

  2. GNU C/C++ __attributes__ GCC中的弱符号与强符号

    最近在看一些源代码,遇到了一些使用__attribute__修饰函数和变量的属性方面的代码,不是太了解,很是汗颜,再此做个总结:   GCC使用__attribute__关键字来描述函数,变量和数据类 ...

  3. gcc中关于静态库和动态库使用(转)

    转自:http://blog.chinaunix.net/uid-25871104-id-3069931.html 1,如何生成静态库 静态库只是一堆object对象的集合,使用ar命令可以将.o文件 ...

  4. gcc中的内嵌汇编语言(Intel i386平台)

    [转]http://bbs.chinaunix.net/thread-2149855-1-1.html 一.声明  虽然Linux的核心代码大部分是用C语言编写的,但是不可避免的其中还是有一部分是用汇 ...

  5. GCC 中零长数组与变长数组

    前两天看程序,发现在某个函数中有下面这段程序: int n; //define a variable n int array[n]; //define an array with length n 在 ...

  6. [转] GCC 中的编译器堆栈保护技术

    以堆栈溢出为代表的缓冲区溢出已成为最为普遍的安全漏洞.由此引发的安全问题比比皆是.早在 1988 年,美国康奈尔大学的计算机科学系研究生莫里斯 (Morris) 利用 UNIX fingered 程序 ...

  7. C++中L和_T()之区别(转)

    C++中L和_T()之区别 分类: C/C++2011-01-12 11:45 2878人阅读 评论(1) 收藏 举报 c++编译器apic 字符串前面加L表示该字符串是Unicode字符串._T是一 ...

  8. GCC 中的编译器堆栈保护技术

    GCC 中的编译器堆栈保护技术 前几天看到的觉得不错得博客于是转发了,但这里我补充一下一些点. GCC通过栈保护选项-fstack-protector-all编译时额外添加两个符号,__stack_c ...

  9. GCC中的内嵌汇编语言

    原文可参考:GCC中的内嵌汇编语言 一.声明   虽然Linux的核心代码大部分是用C语言编写的,但是不可避免的其中还是有一部分是用汇编语言写成的.有些汇编语言代码是直接写在汇编源程序中的,特别是Li ...

随机推荐

  1. JSPatch库, 一个Apple官方支持的实现在线更新iOS应用的库

    简介 项目主页: https://github.com/bang590/JSPatch 示例下载: https://github.com/ios122/ios122 JSPatch 可以让你用 Jav ...

  2. Vscode插件--微信小程序格式化以及高亮组件wxml-vscode

    wxml-vscode wxml-vscode 仓库 提问题 安装 通过 F1 或者 CMD + Shift + P 输入 install. 选择: Install Extension. 特性 格式化 ...

  3. 微信小程序开发踩坑与总结 -

    原文链接:https://segmentfault.com/a/1190000008516296 前段时间把公司小程序项目开发完成了,所以来写写自己开发过程中碰到的问题和解决方法,以及用到的提高效率的 ...

  4. avalon.js的循环操作在表格中的应用

    avalon.js的循环操作在表格中的应用 一个JAVA开发,因为做的门户系统中,数据的展示加载的速度很影响使用效果,想到的是尽量少的请求后台,然后接触到了avalon,看介绍这是一个很轻很轻的MVV ...

  5. 泉五培训Day3

    T1 家庭作业 题目 [问题描述] 小P为了能高效完成作业,规定每项作业花一个单位时间. 他的学习日从0时刻开始,有100000个单位时间.在任一时刻,他都可以选择编号1~N的N项作业中的任意一项作业 ...

  6. BZOJ1509: [NOI2003]逃学的小孩(树的直径)

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1126  Solved: 567[Submit][Status][Discuss] Description ...

  7. dataTables设置下拉滚动出现表头挤在一起的解决方法

    1.引入datatable的CSS文件 <link href="http://cdn.datatables.net/1.10.15/css/jquery.dataTables.min. ...

  8. 微信小程序开发入门学习(1):石头剪刀布小游戏

    从今天起开始捣鼓小程序了2018-12-17   10:02:15 跟着教程做了第一个入门实例有兴趣的朋友可以看看: 猜拳游戏布局 程序达到的效果 猜拳游戏的布局是纵向显示了三个组件:文本组件(tex ...

  9. 使用windows api安装windows服务程序(C#)

    3个步骤: 1.安装器代码编写 2.安装器工具类编写 1)安装.启动服务) 2)卸载服务 3.windows服务程序编写(参考:多线程.方便扩展的Windows服务程序框架) 4.代码下载,在文末(注 ...

  10. Skyscrapers Covered in Solar Pancels【太阳能电池板覆盖的摩天大楼】

    Skyscrapers Covered in Solar Panels An office tower on Miller Stree in Manchester is completely cove ...