ld

来源:互联网 发布:mac做java开发工具 编辑:程序博客网 时间:2024/05/02 22:11

在新安装一个库之后如何让系统能够找到他。

如果安装在/lib或者/usr/lib下,那么ld默认能够找到,无需其他操作。
如果安装在其他目录,需要将其添加到/etc/ld.so.cache文件中,步骤如下:
1.编辑/etc/ld.so.conf文件,加入库文件所在目录的路径
2.运行ldconfig,该命令会重建/etc/ld.so.cache文件

  •        ldconfig should normally be run by the superuser as it may require  write  permission  on  some  root  owned  directories and files.最好在root权限下使用
  •        /sbin/ldconfig -p  打印当前的共享库
  •        /lib/ld.so          run-time linker/loader
  •        /etc/ld.so.conf     File  containing  a  list  of  colon,  space, tab, newline, or comma-separated directories in which to search for libraries.
  •        /etc/ld.so.cache    File containing an ordered list of libraries found in the directories specified in /etc/ld.so.conf.


       -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 file systems.


           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.

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

           一次-rpath-link可以接一组路径(以冒号分隔),或多次-rpath-link。

           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. -rpath选项在运行时使用,而 -rpath-link只在链接时起作用。

              如gcc -o hello main.c -lmyhello  -Wl,-L.  运行时找不到libmyhello.so,使用gcc -o hello main.c -lmyhello  -Wl,-rpath . -Wl,-L.,则可以运行。而-rpath-link则不行。

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

           4.  On SunOS, if the -rpath option was not used, search any directories specified using -L options.搜索-L指定的路径

           5.  For a native linker, 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.


原创粉丝点击