zoukankan      html  css  js  c++  java
  • Linux ToolChain (二) --- Linker (1)链接选项 -L -rpath -rpath-link

     一、动态库的链接和链接选项-L,-rpath-link,-rpath

      (1). 现代连接器在处理动态库时将链接时路径(Link-time path)和运行时路径(Run-time path)分开,

        用户可以通过-L指定连接时库的路径,

        通过-R(或-rpath)指定程序运行时库的路径,大大提高了库应用的灵活性。

              

    Link-time path -L指定连接时库的路径 -L
    Run-time path -R(或-rpath)指定程序运行时库的路径 -rpath-link
    -rpath

      比如我们做嵌入式移植时#arm-linux-gcc $(CFLAGS) –o target –L/work/lib/zlib/ -llibz-1.2.3 (work/lib/zlib下是交叉编译好的zlib库),

      将target编译好后我们只要把zlib库拷贝到开发板的系统默认路径下即可。或者通过-rpath(或-R )、LD_LIBRARY_PATH指定查找路径。

      链接器ld的选项有 -L,-rpath 和 -rpath-link,看了下 man ld,大致是这个意思:

    •  -L:  “链接”的时候,去找的目录,也就是所有的 -lFOO 选项里的库,都会先从 -L 指定的目录去找,然后是默认的地方。

          编译时的-L选项并不影响环境变量LD_LIBRARY_PATH, -L 只是指定了程序编译连接时库的路径,并不影响程序执行时库的路径,

          系统还是会到默认路径下查找该程序所需要的库,如果找不到,还是会报错,类似cannot open shared object file。

    •  -rpath-link:这个也是用于“链接”的时候的,例如你显示指定的需要 FOO.so,但是 FOO.so 本身是需要 BAR.so 的,后者你并没有指定,而是 FOO.so 引用到它,

             这个时候,会先从 -rpath-link 给的路径里找。

    • -rpath: “运行”的时候,去找的目录。运行的时候,要找 .so 文件,会从这个选项里指定的地方去找。

          对于交叉编译,交叉编译链接器需已经配置 –with-sysroot 选项才能起作用。

          也就是说,-rpath指定的路径会被记录在生成的可执行程序中,用于运行时查找需要加载的动态库。-rpath-link 则只用于链接时查找。

     

      (2).  在gcc中使用ld链接选项时,需要在选项前面加上前缀-Wl(是字母l,不是1),     

      if the linker is being invoked indirectly,via a compiler driver(gcc)then all the linker command line options should be prefixed by -Wl, 
      (or whatever is appropriate for the particular compiler driver) like this:   <!-- lang: shell -->   gcc -Wl,--start-group foo.o bar.o -Wl,--end-group   This is important, because otherwise the compiler driver program may silently drop the linker options, resulting in a bad link.

        The -Wl,xxx option for gcc passes a comma-separated list of tokens as a space-separated list of arguments to the linker. So

            gcc -Wl,aaa,bbb,ccc

        eventually becomes a linker call

            ld aaa bbb ccc

        In your case, you want to say "ld -rpath .", so you pass this to gcc as -Wl,-rpath,.Alternatively, you can specify repeat instances of -Wl:

            gcc -Wl,aaa, -Wl,bbb -Wl,ccc

        Or, in your case, 

            -Wl,-rpath -Wl,.

          (3).  直接 man ld 查看详细文档

  • 相关阅读:
    VScode 修改中文字体
    missing KW_END at ')' near '<EOF>'
    SQL inner join, join, left join, right join, full outer join
    SQL字符替换函数translater, replace
    SQL COOKBOOK SQL经典实例代码 笔记第一章代码
    sqlcook sql经典实例 emp dept 创建语句
    dateutil 2.5.0 is the minimum required version python
    安装postgresql后找不到服务 postgresql service
    Postgres psql: 致命错误: 角色 "postgres" 不存在
    【西北师大-2108Java】第十六次作业成绩汇总
  • 原文地址:https://www.cnblogs.com/yaozhongxiao/p/4335166.html
Copyright © 2011-2022 走看看