Question on how do I tell make to choose between static and shared versions of a library

Matthew Seaman matthew at
Sun Dec 16 17:43:00 UTC 2012

On 16/12/2012 17:22, Manish Jain wrote:
> Under /lib I have both versions - shared ( as well as static
> (libxyz.a) - of a library. How do I tell make to link to the static
> version, not the shared one ? The next obvious question is how to do the
> vice versa - tell make to link to the shared version, not the static one.

Add -static to the ld command line to produce a staticly linked binary:
this forbids ld(1) from doing any dynamic linking.  Otherwise ld will
default to dynamic linking, but fall back to linking staticly against
libraries where there isn't a dynamic shared object available.

Actually, there are about 4 different linker flags you could use that
mean 'produce a staticly linked binary.'  They don't have any different
effect; the reason they exist is for historic compatibility with
versions of ld(1) from many different sources.

It's also an all-or-nothing option.  If you wanted to use static linkage
for one particular library out of all the libraries used by your
program, then you'ld need a very different command line.  But that, as
they say, is left as an exercise for the student.



Dr Matthew J Seaman MA, D.Phil.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 266 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the freebsd-questions mailing list