简体   繁体   中英

automake third party libraries

How to compile and link third party libraries with automake?

My file structure is:

program/
  |
  +--src/
  |   |
  |   +--Makefile.am
  |   +--main.cpp
  |
  +--lib/
  |   |
  |   +--Makefile.am
  |   +--library.cpp
  |
  +--Makefile.am
  +--configure.ac
  +--README

Contents of automake files are pretty generic:

# src/Makefile.am
bin_PROGRAMS = program
program_SOURCES = main.cpp

# Makefile.am
SUBDIRS = src lib
dist_doc_DATA = README

# configure.ac
AC_INIT([program], [1.0])
AM_INIT_AUTOMAKE([-Wall])
AC_PROG_CXX
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile src/Makefile lib/Makefile])
AC_OUTPUT

What should be the contents of lib/Makefile.am ?

(Not sure why you said "third-party" when you appear to have control of the library code yourself... For more info related to creating and working with libraries using Automake, I refer you to the GNU Automake manual's section on libraries )

lib/Makefile.am

lib_LIBRARIES = libYOURLIB.a
libYOURLIB_a_SOURCES = library.cpp

You can use noinst_lib_LIBRARIES if you don't want to install the library itself. Note that I'm assuming you want to build a static library only. See the Building A Shared Library section of the GNU Automake manual for integrating with Libtool to produce a shared library. You can do it manually of course, but it's a lot easier with Libtool as it takes care of various platform differences.

To link your library to program , you'd add the following lines in
src/Makefile.am :

program_DEPENDENCIES = $(top_builddir)/lib/libYOURLIB.a
program_LDADD = $(top_builddir)/lib/libYOURLIB.a

The _DEPENDENCIES line simply tells Automake that program relies on lib/libYOURLIB.a being built first, and the _LDADD line simply adds the library to the linker command.


The above assumes that you have a rule to build the library already. Since you're using SUBDIRS , you received a "no rule to make target XXXXXX" build failure, which indicates that you don't (at least from the perspective of the Makefile in the src subdirectory). To remedy this, you can try the following in src/Makefile.am (taken from "Re: library dependency" on the GNU Automake mailing list archives):

FORCE:

$(top_builddir)/lib/libYOURLIB.a: FORCE
<TAB>(cd $(top_builddir)/lib && $(MAKE) $(AM_MAKEFLAGS) libYOURLIB.a)

You can also simply make lib a subdirectory of src as your comment indicated of course and make it simpler.

Alternatively, you can stop using a recursive build setup and use what is perhaps a simpler non-recursive build setup. See GNU Automake Manual §7.3: An Alternative Approach to Subdirectories and Non-recursive Automake for some information on that, but the general idea would be to alter things to allow for :

configure.ac

AM_INIT_AUTOMAKE([-Wall subdir-objects])
...
AC_CONFIG_FILES([Makefile])

Makefile.am

# Instead of using the SUBDIRS variable.
include src/Makefile.am.inc
include lib/Makefile.am.inc
dist_doc_DATA = README

lib/Makefile.am renamed to lib/Makefile.am.inc

# Full path relative to the top directory.
lib_LIBRARIES = lib/libYOURLIB.a
lib_libYOURLIB_a_SOURCES = lib/library.cpp

src/Makefile.am renamed to src/Makefile.am.inc

# Full path relative to the top directory.
bin_PROGRAMS = bin/program
bin_program_SOURCES = src/main.cpp
bin_program_DEPENDENCIES = lib/libYOURLIB.a
bin_program_LDADD = lib/libYOURLIB.a

Renaming the files is optional (you could always just include src/Makefile.am ), but it helps to denote that it isn't meant to be a standalone Automake source file.

Also, supposing that lib/library.cpp and src/main.cpp both #include "library.hpp" , and it's in another directory, you might also want to use AM_CPPFLAGS = -I $(top_srcdir)/include for all files or obj_program_CPPFLAGS = -I include for all source files that are used in building bin/program , assuming library.hpp is in program/include . I'm not sure if $(top_srcdir) is right when another project includes your entire program source directory in its own SUBDIRS variable, but $(srcdir) will always refer to the top-level program directory in the case of a non-recursive automake, making it perhaps more useful in larger projects that include this package as a component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM