交叉编译时--sysroot,-rpath,-rpath-link,-L之间的关系与注意点

使用场景

在进行交叉编译的时候,出现 "not found (try using -rpath or -rpath-link)" 错误,可能是由于没有设置sysroot,而使用了rpath进行运行库的搜索路径指定,导致在链接的时候找不到对应的动态库。可以使用rpath-link来替代rpath参数。

一、使用说明

  • -l 就是表示添加需要链接的库文件,如果没有用后缀指明动态库还是静态库,则优先使用动态库。
  • -L 就是添加链接库的搜索目录,可以看成是为-l服务的。
  • --sysroot = dir 将dir作为逻辑root目录,用于搜索头文件和依赖库文件,例如--sysroot=/home/build,那么如果之前默认去/usr/lib下面去搜索依赖库,则在sysroot的作用下会定位到/home/build/usr/lib目录下进行搜索。这个参数在交叉编译的时候会影响到rpath,如果没有设置这个sysroot,则rpath在编译阶段是不起作用的
  • -rpath = dir 这个参数在编译可执行文件的时候,用来指定运行库的搜索目录,该目录会被记录在elf可执行文件中。与此同时在链接阶段,也可以兼职-rpath-link的功能,作为编译时动态库的搜索路径。但是如果启用了该参数,-L参数就会失效。在交叉编译环境中,该参数需要同sysroot共同作用,如没有sysroot则该参数会部分失效,无法将dir添加进链接库的搜索路径列表中,最终导致" not found (try using -rpath or -rpath-link)" 错误提示。
  • -rpath-link 这个参数类似rpath,提供运行库的搜索路径,但是该参数只是在链接阶段起作用,不会被写入elf文件中。

二、官方手册

  1. -l 的解释 larchive

    --library=archive

    Add archive file archive to the list of files to link. This option may be used any number of times. ld will search its path-list for occurrences of libarchive.a for every archive specified. On systems which support shared libraries, ld may also search for libraries with extensions other than .a. Specifically, on ELF and SunOS systems, ld will search a directory for a library with an extension of .so before searching for one with an extension of .a. By convention, a .so extension indicates a shared library. The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again. See the -( option for a way to force the linker to search archives multiple times. You may list the same archive multiple times on the command line. This type of archive searching is standard for Unix linkers. However, if you are using ld on AIX, note that it is different from the behaviour of the AIX linker.

  2. -L 的解释 -Lsearchdir

    --library-path=searchdir

    Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts. You may use this option any number of times. The directories are searched in the order in which they are specified on the command line. Directories specified on the command line are searched before the default directories. All -L options apply to all -l options, regardless of the order in which the options appear. The default set of paths searched (without being specified with `-L') depends on which emulation mode ld is using, and in some cases also on how it was configured. See section Environment Variables. The paths can also be specified in a link script with the SEARCH_DIR command. Directories specified this way are searched at the point in which the linker script appears in the command line.

  3. sysroot --sysroot=dir

    Use dir as the logical root directory for headers and libraries. For example, if the compiler normally searches for headers in /usr/include and libraries in /usr/lib, it instead searches dir/usr/include and dir/usr/lib.

    If you use both this option and the -isysroot option, then the --sysroot option applies to libraries, but the -isysroot option applies to header files.

    The GNU linker (beginning with version 2.16) has the necessary support for this option. If your linker does not support this option, the header file aspect of --sysroot still works, but the library aspect does not.

  4. -rpath的解释
    1. -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 environment variable LD_RUN_PATH will be used if it is defined. The -rpath option may also be used on SunOS. By default, on SunOS, the linker will form a runtime search patch out of all the -L options it is given.**If a -rpath option is used, the runtime search path will be formed exclusively using the -rpath options, ignoring the -L options.**This can be useful when using gcc, which adds many -L options which may be on NFS mounted filesystems. For compatibility with other ELF linkers, if the -R option is followed by a directory name, rather than a file name, it is treated as the -rpath option.

  5. -rpath-link的解释 -rpath-link DIR

    When using ELF or SunOS, one shared library may require another. This happens when an ld -shared link includes a shared library as one of the input files. When the linker encounters such a dependency when doing a non-shared, non-relocateable link, it will automatically try to locate the required shared library and include it in the link, if it is not included explicitly. In such a case, the -rpath-link option specifies the first set of directories to search. The -rpath-link option may specify a sequence of directory names either by specifying a list of names separated by colons, or by appearing multiple times. The linker uses the following search paths to locate required shared libraries.

    Any directories specified by -rpath-link options.

    Any directories specified by -rpath options. The difference between -rpath and -rpath-link is that directories specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time.

    On an ELF system, if the -rpath and rpath-link options were not used, search the contents of the environment variable LD_RUN_PATH.

    On SunOS, if the -rpath option was not used, search any directories specified using -L options.

    For a native linker, the contents of the environment variable LD_LIBRARY_PATH.

    The default directories, normally `/lib' and `/usr/lib'.

​​​​​​​Using LD, the GNU linker - Options

Directory Options (Using the GNU Compiler Collection (GCC))

相关推荐
AI服务老曹4 小时前
【架构深评】打通 X86/ARM 异构屏障:基于 GB28181/RTSP 的企业级 AI 视频管理平台架构解析
arm开发·人工智能·架构
szxinmai主板定制专家4 小时前
基于ARM+FPGA高性能MPSOC 多轴伺服设计方案
arm开发·人工智能·嵌入式硬件·fpga开发·架构
AI服务老曹4 小时前
[深度解析] 兼容 X86/ARM 与多模态 NPU:基于 GB28181/RTSP 的工业级 AI 视频中台架构设计
arm开发·人工智能·音视频
青柠小苍兰5 小时前
Mac(M4 Pro)安装 Parallels Desktop 20 + Windows 11 ARM 完整教程
arm开发·macos·虚拟机·parallels
相偎5 小时前
arm平台编译mpp、ffmpeg和xfreerdp
arm开发·ffmpeg
时空自由民.21 小时前
蓝牙协议栈知识和网络协议栈知识对比
网络·arm开发·网络协议
QAQ小菜鸟1 天前
五、keil添加AC5
arm开发
花无缺就是我1 天前
内网穿透哪个好,之神卓互联Linux版Arm安装教程2026最新
linux·运维·arm开发
以梦为马无处可栖1 天前
AxVisor 深度学习笔记-ARM 虚拟化硬件原理
arm开发·笔记·深度学习
CinzWS1 天前
UVM验证环境构建:CPU验证的方法论——从零构建ARM A53验证帝国的艺术
arm开发·架构·芯片验证·原型验证·a53