asudhak
asudhak

Reputation: 3117

Automake include all libraries

My directory structure looks like the following :

-misc
-- misc.h
-- misc.c
-- misc.la

-dict
-- dict.h
-- dict.c
-- dict.la

-main
-- main.c

I'm trying to build main.c into a program. main.c contains a reference to a function in dict.c and dict.c contains references to misc.c

How do I tell automake to include dict.la, misc.la to build main ? In other words, I want to include all the *.la files that were built by the top level make.

This is of course just an example, in the real scenario, i'm dealing with more than 20 such libraries each in a directory similar to the above.

Upvotes: 0

Views: 195

Answers (1)

ndim
ndim

Reputation: 37777

the recursive make case

You sound as if you were using recursive make with one top level Makefile.am, one main/Makefile.am and one FOO/Makefile.am for FOO in misc, dict, and the 20 other such libraries.

If possible, I would try to change to using non-recursive make for this use case (see below).

In that recursive make case, you will need to adapt main/Makefile.am to link main with all those libraries, and to make sure those libraries are actually (re)built if necessary:

bin_PROGRAMS     = mainprog
mainprog_SOURCES = main.c
mainprog_LDADD   =

mainprog_LDADD += $(top_builddir)/misc/libmisc.la
$(top_builddir)/misc/libmisc.la:
        cd $(top_builddir)/misc && $(MAKE) libmisc.la

You will need to repeat that _LDADD += and build rule pattern for each and every of those 20+ libraries.

the non-recursive make case

Personally, I would switch to using non-recursive make with one single top level Makefile.am, while putting both the mainprog executable and the 20+ libfoo.la files into the $(top_builddir). Then adding a library to the main program can be reduced to adding a single line

mainprog_LDADD += libmisc.la

without any extra rules being required. This also parallelizes building of those 20+ libraries, which is a very welcome side effect.

The new top level Makefile.am would contain one line per library like

include misc/Makefile-files

ending with one line

include main/Makefile-files

and the file misc/Makefile-files would contain something like

# -*- Makefile -*-
lib_LTLIBRARIES   += libmisc.la
libmisc_la_SOURCES = %reldir%/misc.c

while main/Makefile-files would look like

# -*- Makefile -*-
bin_PROGRAMS      += mainprog
mainprog_SOURCES   = main.c
mainprog_CPPFLAGS  =
mainprog_LDADD     =

# repeat for 20+ libraries
mainprog_LDADD += dict/libdict.la
mainprog_LDADD += misc/libmisc.la

If your main program uses #include "misc.h", you will need 20+ per library mainprog_CPPFLAGS += -I$(top_srcdir)/misc additions. If you could change that to #include "misc/misc.h", a single mainprog_CPPFLAGS = -I$(top_srcdir) will do instead.

You could also include main/Makefile-files in before all those library Makefile-files and consistently add the respective library to mainprog in the library Makefile-files:

bin_PROGRAMS      += mainprog
mainprog_SOURCES   = main.c
mainprog_CPPFLAGS  =
mainprog_LDADD     =

with misc/Makefile-files containing

lib_LTLIBRARIES    += libmisc.la
libmisc_la_SOURCES  = %reldir%/misc.c
mainprog_CPPFLAGS  += -I$(top_srcdir)/%reldir%
mainprog_LDADD     += libmisc.la

If you can guarantee that all the lib_LTLIBRARIES built from the top level Makefile.am are to be linked to the main program, and that the main program sources can use #include "misc/misc.h", the following top level Makefile.am will do:

include main/Makefile-files

# 20+ library includes
include dict/Makefile-files
include misc/Makefile-files

mainprog_LDADD += $(lib_LTLIBRARIES)

with main/Makefile-files being

bin_PROGRAMS     += mainprog
mainprog_SOURCES  = main.c
mainprog_LDADD    =

and misc/Makefile-files and the other libraries:

lib_LTLIBRARIES    += libmisc.la
libmisc_la_SOURCES  = misc.c

For a complete working example of a single library and a main program using non-recursive make for inspiration, see my answer https://stackoverflow.com/a/60306382/182675 and the github example I built for that.

Upvotes: 1

Related Questions