Search code examples
autotoolsautomakelibtool

How to reuse objects between Automake program and tests targets?


I have a non-recursive Makefile.am with something like the following:

SHARED_SRCS = src/bar.cpp src/baz.cpp

bin_PROGRAMS = foo

foo_SOURCES = src/main.cpp $(SHARED_SRCS)
foo_CXXFLAGS = -I$(srcdir)/src $(SOME_CFLAGS)
foo_LDADD = $(SOME_LIBS)

check_PROGRAMS = test1 test2 test3
TESTS = test1 test2 test3

test1_SOURCES = tests/test1.cpp $(SHARED_SRCS)
test2_SOURCES = tests/test2.cpp $(SHARED_SRCS)
test3_SOURCES = tests/test3.cpp $(SHARED_SRCS)
test1_CXXFLAGS = $(foo_CXXFLAGS)
test2_CXXFLAGS = $(foo_CXXFLAGS)
test3_CXXFLAGS = $(foo_CXXFLAGS)
test1_LDADD = $(foo_LDADD)
test2_LDADD = $(foo_LDADD)
test3_LDADD = $(foo_LDADD)

However, every target builds its own SHARED_SRCS, getting them built 4 times, even when sharing the same flags.

Is there a way to get them built without creating a convenience library, e.g. libbar.a or libtool's libbar.la?


Solution

  • No, there's not. The point is that you're not rebuilding the sources, you're rebuilding binaries.

    What you can do is build an intermediate .o consisting only of the shared sources, and then use that in your tests, and binaries.