home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / programm / 5258 < prev    next >
Encoding:
Internet Message Format  |  1992-11-11  |  2.2 KB

  1. Path: sparky!uunet!ontek!mikey
  2. From: mikey@ontek.com (euphausia superba)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: How do I make dynamic link libraries?
  5. Message-ID: <2185@ontek.com>
  6. Date: 11 Nov 92 21:57:34 GMT
  7. References: <1992Nov11.004546.15671@EE.Stanford.EDU>
  8. Organization: Ontek Corporation -- Laguna Hills, California
  9. Lines: 51
  10.  
  11. In comp.unix.programmer, tse@leland.stanford.edu (Eric Tse) writes:
  12. | Hi,
  13. |     I am new to dynamic-link libraries and wonder how I can 
  14. | create one. For example, if I want to make f1.o f2.o into a 
  15. | libtest.so.0.1, do I do:
  16. |     ld -Bdynamic f1.o f2.o -o libtest.so.0.1
  17. | ?
  18. |     Then if I want to compile a programm a.out originally
  19. | using f1.o and f2.o, do I do:
  20. |     cc -Bdynamic -o a.out other1.o other2.o libtest.so.0.1
  21. | ?
  22. |     If I want to execute a.out, do I have to
  23. |     setenv LD_LIBRARY_PATH /path_of_the_so
  24. | before I run a.out?
  25. | But it does not work! Any help? Please?
  26.  
  27. Judging by your use of the -Bdynamic option, I'm guessing that
  28. you're doing this with SunOS or at a least BSD-ish linker.
  29.  
  30. Here's what you have to do.
  31.  
  32. 1. When you compile f1.c and f2.c use the -pic option.  If you're
  33. using gcc try -fpic.  This tells the compiler that it needs
  34. to generate position-independent-code, so that the library can be
  35. linked in at any old place in the address space at run time.
  36. If the library becomes too large you will need -PIC or -fPIC.
  37.  
  38. 2. Create the library like so
  39.  
  40.   ld -o libtest.so.0.1 -assert pure-text f1.o f2.o
  41.  
  42. The -assert pure-text is there to make sure you compiled everything
  43. with -pic.  To get full benefit of the "shared" part of shared
  44. dynamic libraries you should put code which contains definitions
  45. of global variables into a separate .sa library.  RTFM ld(1) for 
  46. more details.   Your code will still run with globals, but if 
  47. it's a large library that many programs may be using at once,
  48. using a .sa library can reduce the load on the system a bit.
  49.  
  50. 3. link your program to the library.  I prefer this method:
  51.  
  52.   cc -o a.out other1.o other2.o -L/libdirname -ltest
  53.  
  54. Where libdirname is the full pathname where the test library is 
  55. located.  -Bdynamic is the default for SunOS's ld so there's
  56. no need to mention it on the command line.  The -L option
  57. makes fiddling with LD_LIBRARY_PATH less necessary.
  58.  
  59.        the krill
  60.  
  61.