linux shared library

The linker(ld) options of shared library:
 -soname=name

When  creating  an  ELF shared object, set the internal DT_SONAME field to the specified name.  When an executable is linked with a shared object which has a DT_SONAME field, then when the executable is  run  the  dynamic  linker  will attempt  to  load the shared object specified by the DT_SONAME field rather than the using the file name given to the linker.

gcc −shared −Wl,−soname,your_soname −o library_name file_list library_list

/sbin/ldconfig - configure dynamic linker run time bindings

       /sbin/ldconfig [ -nNvXV ] [ -f conf ] [ -C cache ] [ -r root ] directory ...

       ldconfig  creates  the  necessary  links  and  cache  to  the  most  recent  shared  libraries  found  in  the directories specified on the command line, in the file
       /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib).  The cache is used by the run-time linker, ld.so or ld-linux.so.  ldconfig checks the header and
       filenames of the libraries it encounters when determining which versions should have their links updated.

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

readelf - Displays information about ELF files

The linker(ld) 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. It is for the native linker only.
  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.
  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.

The shared libraries needed by the program are searched by dynamic loader(ld-linux.so*) for in various places:

  1. (ELF only) Using the DT_RPATH dynamic section attribute of the binary if present and DT_RUNPATH attribute does not exist.  Use of DT_RPATH is deprecated.
  2. Using the environment variable LD_LIBRARY_PATH.  Except if the executable is a set-user-ID/set-group-ID binary, in which case it is ignored.
  3.  (ELF only) Using the DT_RUNPATH dynamic section attribute of the binary if present.
  4.  From the cache file /etc/ld.so.cache which contains a compiled list of candidate libraries previously found in the augmented library path. If, however, the binary was linked with  -z  nodeflib  linker  option,  libraries  in  the default library paths are skipped.
  5.  In  the  default path /lib, and then /usr/lib.  If the binary was linked with -z nodeflib linker option, this step is skipped.
-( archives -)
       --start-group archives --end-group
           The archives should be a list of archive files.  They may be either explicit file names, or -l options.
           The specified archives are searched repeatedly until no new undefined references are created.  Normally,  an  archive is  searched  only once in the order that it is specified on the command line.  If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on  the  command  line,  the linker  would not be able to resolve that reference.  By grouping the archives, they all be searched repeatedly until all possible references are resolved.
           Using this option has a significant performance cost.  It is best to use it only when there are unavoidable  circular references between two or more archives.


demo:

gcc −fPIC −g −c −Wall a.c

gcc −fPIC −g −c −Wall b.c

gcc −shared −Wl,−soname,libmystuff.so.1 −o libmystuff.so.1.0.1 a.o b.o −lc

ldconfig −n directory_with_shared_libraries


This HOWTO for programmers discusses how to create and use program libraries on Linux:

http://www.tldp.org/HOWTO/pdf/Program-Library-HOWTO.pdf


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值