Search code examples
makefilebuildstatic-linkingautotoolslibtool

Libtool: link one of the project generated libs statically


I have a project that builds with

autoreconf --install
./configure
make

and contains configure.ac and Makefile.am
The project builds 2 libraries and one executable that dynamically links them. One library is cpp, other and the executable are pure C. I want to link one of the libs (cpp one) statically and keep c library to link dynamically. My simplified Makefile.am is

ACLOCAL_AMFLAGS = -I m4

SUBDIRS = include doc po

AM_CPPFLAGS = -Iinclude
AM_CPPFLAGS += -D__STRICT_ANSI__
AM_CPPFLAGS += -DNDEBUG

WARNFLAGS = -Wall

AM_CFLAGS = $(WARNFLAGS) $(OPENMP_CXXFLAGS) -std=c11
AM_CXXFLAGS = $(WARNFLAGS) -std=c++11

localedir = $(datadir)/locale
DEFS = -DLOCALEDIR=\"$(localedir)\" @DEFS@

lib_LTLIBRARIES = libmy.la libmy-settings.la

libmy_la_SOURCES = src/sharedlib.c src/sharedlib.h

libmy_settings_la_SOURCES = src/staticlib.cpp src/staticlib.h

bin_PROGRAMS = myapp

myapp_SOURCES = myapp.c

myapp_LDADD = libmy.la libmy-settings.la

The libtool generates both .a and .so files in ./.libs/ but Libtool prefers .so. In the last make command I see gcc -Wall -std=c11 -g -O2 -o .libs/myapp myapp.o ./.libs/libmy.so ./.libs/libmy-settings.so. If I replace it with gcc -Wall -std=c11 -g -O2 -o .libs/myapp myapp.o ./.libs/libmy.so ./.libs/libmy-settings.a - it works and gives me that I'm trying to achieve. The question is: how to achieve it automatically wit help of Makefile.am?

As I understand, I can't just add myapp_LDFLAGS=-lmy-settings.a bcs it won't replace shared linking argument but add a new one. And I can't remove the libmy-settings.la from lib_LTLIBRARIES or myapp_LDADD bcs it won't generate libmy-settings.a or libmysettings.so in ./.libs/ at all. Any clues?


Solution

  • Since libmy-settings does not need to be a separately installable library, you have two pretty straightfoward alternatives:

    1. Add its sources to myapp_SOURCES, and leave libmy-settings.la completely out of the picture. That the additional sources are located in a different directory and written in a different (autotools-supported) language should not be a problem. This approach may be to your advantage in terms of the Autotools choosing the correct linker driver and options without your intervention, too.

    2. Build libmy-settings.la as a utility library instead of an installable one. This is achieved by marking it noinst:

      lib_LTLIBRARIES = libmy.la
      noinst_LTLIBRARIES = libmy-settings.la