Windows/Linux鏈接器加載動態庫的搜索路徑順序

Windows/Linux鏈接器加載動態庫的搜索路徑順序


如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033

目錄

系統: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

覺得文章對你有幫助,可以用微信掃描二維碼捐贈給博主,謝謝!
微信
如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章