Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 02 Oct 1998 08:53:59 -0700
From:      John Polstra <jdp@polstra.com>
To:        kent@iastate.edu
Cc:        current@FreeBSD.ORG
Subject:   Re: ELF library questions (building and linking)
Message-ID:  <199810021553.IAA09440@austin.polstra.com>
In-Reply-To: <199809300121.UAA04701@isua5.iastate.edu>
References:  <199809300121.UAA04701@isua5.iastate.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
In article <199809300121.UAA04701@isua5.iastate.edu>,
Kent Vander Velden  <kent@iastate.edu> wrote:
> 
> Building an ELF library questions:
> 
>   I have been trying to rebuild a few of the libraries that I use to
> be in elf format.  Is the following the correct way to build an elf library?
> 
>   for every file:
>     cc -c -fpic file.c 
>     ld -o shared/file.o -x -r file.o
> 
>   cc -shared -Wl,-x -o libA.so.1 -Wl,-soname,libA.so.1 `lorder file1 file2 ... | tsort`

Yes, that looks OK.  A good reference is "/usr/share/mk/bsd.lib.mk"
(if you can stand to read it).

When you install the shared library, install it as "libA.so.1" with a
symbolic link to it named "libA.so".

>   Does `ld -shared ...` not work correctly any longer?

It is better to use "cc -shared ...".  Besides the .o files that you
mention on the command line, there are a few more system .o files
that have to get linked in too.  "cc -shared" does that for you, but
"ld -shared" doesn't.  To see the magic, try "cc -v -shared ...".

> When I used `ld -shared ...` to build a shared library and `ar` to
> build an archive in the same directory, the linker seemed to prefer
> to link to the archive instead of the shared library.

The linker only looks for shared libraries named "libfoo.so" --
without the version number.  If you didn't create the symbolic link,
the linker couldn't have found the library.

>   Mush ELF libraries have a name of the form libName.so.version or
> can try be of the form libName.so.version.reversion?

Technicallly speaking, the name can be anything.  However, the
standard ELF convention is to use just one version number.  Using two
version numbers can lull you into thinking that the dynamic linker
will do something intelligent with them.  It won't.

> Linking to an ELF library question:
> 
>   I seem to remember there being an environment variable that could be
> set that held a search path for the linker.  Setting this variable was
> like adding a -L<path> flag to the link line.  Is such a thing still
> available?  Mainly I would like to avoid adding "-L/usr/X11/lib" to 
> a bunch of Makefiles.

If you set the environment variable LD_RUN_PATH when _building_
programs or shared libraries, then it will be used to search for
the shared libraries needed by them whenever the programs or
libraries are used.  Another way to do it is with the linker option
"-R path".  It's the usual colon-separated path syntax.

So if you build your X11 programs with LD_RUN_PATH=/usr/X11R6/lib in
the environment, they will always find their X shared libraries,
even if you haven't run ldconfig.
--
   John Polstra                                       jdp@polstra.com
   John D. Polstra & Co., Inc.                Seattle, Washington USA
   "Self-knowledge is always bad news."                 -- John Barth

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199810021553.IAA09440>