链接与运行时动态库依赖

前端之家收集整理的这篇文章主要介绍了链接与运行时动态库依赖前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. The -L option supplies a colon-separated library path that is to be
  2. searched at LINK TIME for libraries. Thus
  3.  
  4. cc -o foo foo.c -L/usr/local/lib -lfoo
  5.  
  6. means that either libfoo.a or libfoo.so should be found in either
  7. /usr/local/lib,or elsewhere in the default search patch (in
  8. GNU/Linux,the directories can be listed in /etc/ld.so.conf,and the
  9. cache updated by running /etc/ldconfig).
  10.  
  11. Whether the .a or .so form of the library is needed is platform
  12. dependent (e.g.,IBM AIX uses only the .a form),and also dependent on
  13. compiler options to select dynamic or static linking. The default is
  14. normally dynamic linking to save disk space and waste cpu time.
  15.  
  16. However,this means while that the executable foo may have been
  17. successfully linked against a shared library,at RUN TIME,the
  18. run-time loader looks for it in the default search path,possibly
  19. prefixed by a colon-separated list of libraries supplied by the
  20. LD_LIBRARY_PATH variable.
  21.  
  22. If,in our example,/usr/local/lib is not part of the default path,then the run-time loader will not be able to find the shared library,EVEN THOUGH LINKING SUCCEEDED (because of the -L/usr/local/lib
  23. option).
  24.  
  25. You can check whether shared libraries can be found by running
  26.  
  27. env -i ldd foo
  28.  
  29. (the "env -i" says to ignore any existing environment variables,such
  30. as LD_LIBRARY_PATH).
  31.  
  32. For example,on one of my systems,I find
  33.  
  34. % env -i ldd /usr/local/bin/emacs
  35. libXaw3d.so.5 => (file not found)
  36. libXmu.so.4 => /usr/lib/libXmu.so.4
  37. libXt.so.4 => /usr/lib/libXt.so.4
  38. ...
  39.  
  40. Notice the "(file not found") line. That library is actually present
  41. on that system in /usr/local/lib,and I can make it succeed like this:
  42.  
  43. % env -i LD_LIBRARY_PATH=/usr/local/lib ldd /usr/local/bin/emacs
  44. libXaw3d.so.5 => /usr/local/lib/libXaw3d.so.5
  45. libXmu.so.4 => /usr/lib/libXmu.so.4
  46. ...
  47.  
  48. Thus,when shared libraries are present in nondefault directories,you
  49. need to supply an additional linker option,usually -R or -Wl,-rpath=,with a run-time library path. Our example above becomes for gcc
  50.  
  51. gcc -o foo foo.c -L/usr/local/lib -lfoo -Wl,-rpath=/usr/local/lib
  52.  
  53. In a Makefile,I would write this as
  54.  
  55. gcc -o foo foo.c -L$(prefix)/lib -lfoo -Wl,-rpath=$(prefix)/lib
  56.  
  57. so that the same library path is used at link time as at run time,and
  58. so that the executable file,foo,records that path. With GNU
  59. autoconf,the normal condition is that prefix is the root of the file
  60. tree into which you install software locally,so the above command is
  61. fairly typical. Unfortunately,software developers who have
  62. nondefault library search paths often forget to supply the -Wl,-rpath
  63. or -R options in their Makefiles,with the result that the code builds
  64. and runs at their sites,but not at end user sites.
  65.  
  66. >From notes that I keep:
  67.  
  68. >> ...
  69. >> Unfortunately,there are at least three incompatible kinds of
  70. >> command-line options that tell the compiler to instruct the linker to
  71. >> save library paths in the executable:
  72. >>
  73. >> -Wl,-rpath,/path/to/dir gcc,g++,FreeBSD,SGI,Sun compilers
  74. >> -rpath /path/to/dir Compaq/DEC,SGI compilers
  75. >> -Rdir:dir:dir Portland Group,Sun compilers
  76. >>
  77. >> Notice that SGI and Sun support two such flavors.
  78. >> ...
  79.  
  80. In my view,there is clearly brain damage here: (1) compiler writers
  81. should have standardized on the same option name for recording the
  82. run-time library path (I'd vote for -R),and (2) the linker should
  83. really record the run-time library path by default,so that -R would
  84. almost never be needed.

  85. Address:

    http://gcc.gnu.org/ml/gcc-help/2005-12/msg00017.html

猜你在找的设计模式相关文章