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

相关推荐
烬奇小云5 小时前
使用 unicorn 和 capstone 库来模拟 ARM Thumb 指令的执行,并实现多个钩子(hook)来监控代码执行、系统调用和内存读写操作(二)
java·arm开发·python
CYRUS STUDIO15 小时前
Android 下内联汇编,Android Studio 汇编开发
android·汇编·arm开发·android studio·arm
艾格北峰1 天前
STM32 BootLoader 刷新项目 (九) 跳转指定地址-命令0x55
arm开发·stm32·单片机·嵌入式硬件
一丝晨光1 天前
编译器、IDE对C/C++新标准的支持
c语言·开发语言·c++·ide·msvc·visual studio·gcc
橘色的喵2 天前
嵌入式ARM平台Linux网络实时性能优化
linux·网络·arm开发·性能优化·实时·内核优化
一丝晨光3 天前
GCC和clang的爱恨情仇
macos·objective-c·xcode·apple·clang·gcc·llvm
荣世蓥3 天前
2.ARM_ARM是什么
arm开发
憧憬一下3 天前
Linux 内核中断描述符 (irq_desc) 的初始化与动态分配机制详解
arm开发·嵌入式硬件·嵌入式·c/c++·linux驱动开发
亿道电子3 天前
【ARM】MDK-E203 Undefined identifier
arm开发·stm32·单片机·arm
ShuQiHere3 天前
【ShuQiHere】️计算机架构:x86 与 ARM 指令集架构的对比与发展
arm开发·架构