交叉编译时--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))

相关推荐
憧憬一下1 小时前
PCI/PCIe设备INTx中断机制和MSI中断机制
arm开发·嵌入式硬件·嵌入式·linux驱动开发·pci/pcie
最后一个bug14 小时前
STM32MP1linux根文件系统目录作用
linux·c语言·arm开发·单片机·嵌入式硬件
最后一个bug1 天前
rt-linux中使用mlockall与free的差异
linux·c语言·arm开发·单片机·嵌入式硬件·算法
李先静1 天前
用 gdbserver 调试 arm-linux 上的 AWTK 应用程序
linux·arm开发·awtk
MrJson-架构师1 天前
4.银河麒麟V10(ARM) 离线安装 MySQL
arm开发·mysql
qq_459730032 天前
4-3 MCU中ARM存储器的作用
arm开发·单片机·嵌入式硬件
轩辰~2 天前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
ARM&开发(Haidong)2 天前
ARM 获取cpu个数
arm开发
憧憬一下3 天前
PCIe_Host驱动分析_设备枚举
arm开发·嵌入式硬件·嵌入式·pcie·linux驱动开发
7yewh3 天前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux