Windows/Linux链接器加载动态库的搜索路径顺序

Windows/Linux链接器加载动态库的搜索路径顺序


如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033

目录

文章目录

  • Windows/Linux链接器加载动态库的搜索路径顺序
    • @[toc]
    • 前言
    • 1.Windows链接器加载动态库的搜索路径顺序
    • 2.Linux链接器加载动态库的搜索路径顺序

系统:Ubuntu 16.04.5 64bit
系统:windows 7 64bit


前言

Windows/Linux程序运行时,其动态库的路径搜索是有顺序的。本文介绍其搜索顺序。

1.Windows链接器加载动态库的搜索路径顺序

通过隐式和显式链接,Windows首先搜索“已知DLL”,例如Kernel32.dll和User32.dll。 Windows然后按以下顺序搜索DLL:

  1. 当前进程的可执行模块所在的目录
  2. 当前目录
  3. Windows系统目录。如C:\Windows\System32,GetSystemDirectory函数检索此目录的路径
  4. Windows目录。 如C:\Windows,GetWindowsDirectory函数检索此目录的路径
  5. PATH环境变量中列出的目录

英文原文:

Windows then searches for the DLLs in the following sequence:
1. The directory where the executable module for the current process is located.
2. The current directory.
3. The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.
4. The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.
5. The directories listed in the PATH environment variable.

2.Linux链接器加载动态库的搜索路径顺序

链接器使用以下搜索路径来查找所需的共享库:

  1. -rpath-link选项指定的任何目录。

  2. -rpath选项指定的任何目录。-rpath和-rpath-link之间的区别在于-rpath选项指定的目录包含在可执行文件中并在运行时使用,而-rpath-link选项仅在链接时有效。

gcc main.c -rpath dir_path
  1. 在ELF系统上,对于本机链接器,如果是-rpath和-rpath-link选项未使用,搜索环境变量“LD_RUN_PATH”的内容。 在SunOS上,如果未使用-rpath选项,搜索指定的任何目录使用-L选项。

  2. 对于本机链接器,搜索环境变量的内容“LD_LIBRARY_PATH”。

  3. 对于本机ELF链接器,共享的“DT_RUNPATH”或“DT_RPATH”中的目录在库中搜索它所需的共享库。 “DT_RPATH”条目是如果存在“DT_RUNPATH”条目,则忽略。

  4. 默认目录,通常是/lib和/usr/lib。

  5. 对于ELF系统上的本机链接器,如果文件/etc/ld.so.conf存在,则列表在该文件中找到的目录。

如果找不到所需的共享库,链接器将发出警告并且继续链接。

英文原文:

ld - The GNU linker

-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-relocatable
           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.

           This option should be used with caution as it overrides the search path that may have
           been hard compiled into a shared library. In such a case it is possible to use
           unintentionally a different search path than the runtime linker would do.

           The linker uses the following search paths to locate required shared libraries:

           1.  Any directories specified by -rpath-link options.

           2.  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. Searching -rpath in this way is only supported by native linkers and
               cross linkers which have been configured with the --with-sysroot option.

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

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

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

           6.  For a native ELF linker, the directories in "DT_RUNPATH" or "DT_RPATH" of a shared
               library are searched for shared libraries needed by it. The "DT_RPATH" entries are
               ignored if "DT_RUNPATH" entries exist.

           7.  The default directories, normally /lib and /usr/lib.

           8.  For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list
               of directories found in that file.

           If the required shared library is not found, the linker will issue a warning and
           continue with the link.

Refrence:

  1. Search Path Used by Windows to Locate a DLL
  2. ld on ubuntu
  3. Using ld The GNU linker

觉得文章对你有帮助,可以扫描二维码捐赠给博主,谢谢!
Windows/Linux链接器加载动态库的搜索路径顺序_第1张图片
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033

你可能感兴趣的:(Linxux,Windows编程)