Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force linking a static library into a shared one with Libtool

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.

like image 228
vperron Avatar asked Jun 01 '12 00:06

vperron


2 Answers

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.

like image 27
William Pursell Avatar answered Oct 14 '22 17:10

William Pursell


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
like image 94
tprk77 Avatar answered Oct 14 '22 16:10

tprk77