9

My project compiles using GNU autotools (aclocal && autoconf && ./configure && make). I'd like to use Boost, and I'd like for other people to be able to compile it as well.

  1. Should I put Boost in my project's dir, or rely on the system's Boost?
  2. How should I tell autotools to use Boost? I've Googled and found many m4 files that claim to do this - but where should I put those m4 files? I can stash one in my /usr/share/aclocal dir, but that doesn't help someone else who wants to compile the project using ./configure && make.
SRobertJames
  • 8,210
  • 14
  • 60
  • 107

3 Answers3

17

The Archive has AX_BOOST_*.

I switched to boost.m4 for some time, but it is so painfully slow I could edit a Makefile with the full Boost path by hand in vim before boost.m4 finished testing for the second library.

I went back to the Archive and was happy to learn the boost macros are being actively maintained again; then I proceeded to purge boost.m4 from every one of my projects.

Relevant excerpts from a use case (assuming the ax_boost_*.m4 files are in subdir m4):

./bootstrap

aclocal -I m4 --install
...

./configure.ac

...
AC_CONFIG_MACRO_DIR([m4])
...
AX_BOOST_BASE([1.48],, [AC_MSG_ERROR([libfoo needs Boost, but it was not found in your system])])
AX_BOOST_SYSTEM
AX_BOOST_FILESYSTEM
...

./Makefile.am

ACLOCAL_AMFLAGS = -I m4
EXTRA_DIST = bootstrap ...
SUBDIRS = ... src ...
...

./src/Makefile.am

AM_CPPFLAGS = \
    ... \
    $(BOOST_CPPFLAGS) \
    ...

...

AM_LDFLAGS = ... \
    $(BOOST_LDFLAGS)

...

lib_LTLIBRARIES = libFoo.la

...

libFoo_la_LIBADD = \
    ... \
    $(BOOST_FILESYSTEM_LIB) \
    $(BOOST_SYSTEM_LIB) \
    ...
DanielKO
  • 4,422
  • 19
  • 29
2

There's an excellent boost.m4 macro that you can include in your project in the m4 subdirectory. These macros are located with AC_CONFIG_MACRO_DIR([m4]) in configure.ac.

Add ACLOCAL_AMFLAGS = -I m4 --install to the top-level Makefile.am, used by automake.

Here's why it's such a good macro:

BOOST_REQUIRE([1.54],[ACTION-IF-NOT-FOUND])

This will define BOOST_CPPFLAGS which you can add to CPPFLAGS, use in an AC_SUBST, etc.

For individual Boost components, it's possible to specify debug builds, static builds, etc. But the (typically) multi-threaded, dynamic libraries are best, e.g.,

BOOST_FILESYSTEM([mt])

will define BOOST_FILESYSTEM_LIBS and BOOST_FILESYSTEM_LDFLAGS. As another bonus, if the library relies on, say, Boost.System, then these library dependencies are automatically added to the LIBS, LDFLAGS, etc.


You should just specify that a Boost installation is required rather than trying to distribute it. It's easily built and installed from source, or from precompiled packages. The former takes a long time, but it's best to have an 'optimized' build on the system. There's a lot of software that can make use of a (robust and optimized) Boost implementation.

Then use the BOOST_REQUIRE error message if there's no acceptable version available.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
0
  1. I'd definitely rely on the system's Boost installation. IIRC, the last time I built Boost it required non-standard tools (wrt the portable tools required for autoconf) for Boost to build. It also took a long time.
  2. AC_CONFIG_MACRO_DIR is where those .m4 files go

    AC_CONFIG_MACRO_DIR([m4])
    

where the macros are in the "m4" directory. I'd expect the various Boost .m4 macros to have some way of telling configure what Boost package to look for (for multiple Boost installations) and where it is (nonstandard installations).

ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • Thanks. Can you give an example of how to use `AC_CONFIG_MACRO_DIR` - I find AC perplexing. Also: If the user installs Boost as per the website's directions, will AC be able to find it? I'd like the error message to tell them what to do "Error: Boost is required. Install as per directions on http://..." – SRobertJames Aug 16 '13 at 20:54
  • The system's Boost might be outdated and/or incompatible, so you might have to install a different version in your home directory (when you don't have root access). – DanielKO Aug 16 '13 at 22:29
  • In that case, I'd probably look for a prebuilt version Boost that would work. Failing that, you could possibly patch the source for the installed Boost version or download Boost from source and build it. – ldav1s Aug 16 '13 at 23:36
  • So you don't have root access on the machine, how will finding a prebuilt boost help in any way, if you are still using by default the system's boost? Downloading and building from source is exactly what I described, but you still have to make sure it's used instead of the system's. – DanielKO Aug 17 '13 at 00:05
  • Binary/source packages can usually be opened without installing them on the machine, so you get them in a directory, just as if they were built. The AX_BOOST_BASE macro you mention (and I alluded to) can point to those directories by `configure --with-boost=/path/to/boost` etc. – ldav1s Aug 17 '13 at 00:14