home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ontek!mikey
- From: mikey@ontek.com (euphausia superba)
- Newsgroups: comp.unix.programmer
- Subject: Re: How do I make dynamic link libraries?
- Message-ID: <2185@ontek.com>
- Date: 11 Nov 92 21:57:34 GMT
- References: <1992Nov11.004546.15671@EE.Stanford.EDU>
- Organization: Ontek Corporation -- Laguna Hills, California
- Lines: 51
-
- In comp.unix.programmer, tse@leland.stanford.edu (Eric Tse) writes:
- | Hi,
- | I am new to dynamic-link libraries and wonder how I can
- | create one. For example, if I want to make f1.o f2.o into a
- | libtest.so.0.1, do I do:
- | ld -Bdynamic f1.o f2.o -o libtest.so.0.1
- | ?
- | Then if I want to compile a programm a.out originally
- | using f1.o and f2.o, do I do:
- | cc -Bdynamic -o a.out other1.o other2.o libtest.so.0.1
- | ?
- | If I want to execute a.out, do I have to
- | setenv LD_LIBRARY_PATH /path_of_the_so
- | before I run a.out?
- |
- | But it does not work! Any help? Please?
-
- Judging by your use of the -Bdynamic option, I'm guessing that
- you're doing this with SunOS or at a least BSD-ish linker.
-
- Here's what you have to do.
-
- 1. When you compile f1.c and f2.c use the -pic option. If you're
- using gcc try -fpic. This tells the compiler that it needs
- to generate position-independent-code, so that the library can be
- linked in at any old place in the address space at run time.
- If the library becomes too large you will need -PIC or -fPIC.
-
- 2. Create the library like so
-
- ld -o libtest.so.0.1 -assert pure-text f1.o f2.o
-
- The -assert pure-text is there to make sure you compiled everything
- with -pic. To get full benefit of the "shared" part of shared
- dynamic libraries you should put code which contains definitions
- of global variables into a separate .sa library. RTFM ld(1) for
- more details. Your code will still run with globals, but if
- it's a large library that many programs may be using at once,
- using a .sa library can reduce the load on the system a bit.
-
- 3. link your program to the library. I prefer this method:
-
- cc -o a.out other1.o other2.o -L/libdirname -ltest
-
- Where libdirname is the full pathname where the test library is
- located. -Bdynamic is the default for SunOS's ld so there's
- no need to mention it on the command line. The -L option
- makes fiddling with LD_LIBRARY_PATH less necessary.
-
- the krill
-
-