Force linking a static library into a shared one with Libtool
Asked Answered
N

2

12

I have a library (libfoo) that is compiled using libtool into two objects: libfoo.a and libfoo.so.

I have to create, using libtool also, another library (libbar) that will be a single shared library (libbar.so) containing all libfoo's code.

In order to do this, I have to force libbar to link against libfoo.a, and not libfoo.so.

I am in an autotools environment, so I have to solve this using standard configure.in or Makefile.am rules.

I tried several things, like in configure.in :

LDFLAGS="$LDFLAGS "-Wl,-Bstatic -lfoo -Wl,-Bdynamic"

That always results in the -Wl flags on the linking line; but -lfoo has disappeared and has been placed in an absolute-path form (/opt/foo/lib/libfoo.so) at the beginning of it.

I also tried:

LDFLAGS="$LDFLAGS "-L/opt/foo/lib libfoo.a"

or in Makefile.am:

libbar_la_LDADD = -Wl,-Bstatic -lfoo -Wl,-Bdynamic

and

libbar_la_LTLIBRARIES = libfoo.a

etc etc (with many, many variants !)

But I think that definitely I do not have knowledge enough of Autotools/Libtool to solve this alone. I have not been able to find information on the Net about it, always slightly different issues.

Nepil answered 1/6, 2012 at 0:19 Comment(0)
Y
7

You could probably use a convenience library. Convenience libraries are intermediate static libraries which are not installed. You could use the prefix noinst to build one.

noinst_LTLIBRARIES = libfoo_impl.la

lib_LTLIBRARIES = libfoo.la libbar.la
libfoo_la_LIBADD = libfoo_impl.la
libbar_la_LIBADD = libfoo_impl.la
Yield answered 20/12, 2012 at 19:9 Comment(1)
This is the proper solution. Just for the sake of completeness, the convenience libraries are compiled as static regardless of what configure flags were given. At least on Unix.Dennett
S
5

The standard way would be to build libfoo with --disable-shared. Whether to link statically or dynamically is a decision for the user to make, so there's really no way to force it as a package maintainer, but you could set the configury of libbar to fail if libfoo.so is present (I'm not sure of a clean way to do that, and believe it would be a bad idea since it really is a choice for the user.) I think the best bet is to have the user build libfoo with --disable-shared, but you can force that choice by specifying static libraries only in libfoo/configure.ac:

LT_INIT([disable-shared])

Note that if you do that, it will not be possible to build libfoo as a shared library. Perhaps that is what you want.

Sparhawk answered 1/6, 2012 at 14:48 Comment(1)
howd you do it without libtool?Neral

© 2022 - 2024 — McMap. All rights reserved.