4

I am converting a C++ program which uses the autotools build system to use a shared library, introducing the use of libtool. Most of the program functionality is being placed in the shared library, which is loaded by the main program, so that the common code can be accessed by other programs in future.

Throughout the program and library sources the autoheader generated config.h is used with the usual macro:

#if HAVE_CONFIG_H
# include <config.h>
#endif

In configure.ac I use the macro to generate it:

AC_CONFIG_HEADERS([config.h])

My question is, do I need to install config.h for others to be able to use my library, and if so, what is the appropriate way to do it, and should it be renamed to avoid conflicts etc?

The most information I have found on this is here:

http://www.openismus.com/documents/linux/building_libraries/building_libraries#installingheaders

But this is hardly an official source.

crobar
  • 2,810
  • 4
  • 28
  • 46

2 Answers2

11

Never ever install autoheader's config.h.

The last thing the users of your library need is interference from the macros leaking out of your config.h. Your library may have HAVE_FOOBAR, but my software might be compiled in a way that foobar is disabled, so that HAVE_FOOBAR will break my compilation.

The AX_PREFIX_CONFIG macro from the archive is a workaround, where everything gets prefixed.

A better approach is to create a template file (e.g. blargconfig.h.in) with lines like:

typedef @BLARG_TYPE@ blarg_int_t;

@BLARG_RANDOM_INCLUDE@

And then AC_SUBST() those variables in configure.ac:

AC_SUBST(BLARG_TYPE, ["unsigned short"])
AC_SUBST(BLARG_RANDOM_INCLUDE, ["#include <somerandomheader.h>"])

Then list it as an output file:

AC_CONFIG_FILES([Makefile
                 src/Makefile
                 ...
                 include/blargconfig.h])

The .h file should be listed with nodist_include_HEADERS; the .h.in file will be automatically distributed because it's listed in AC_CONFIG_FILES.

Destination for such files is commonly $libdir/packagename/include. See GLib for example, although they generate glibconfig.h without a template (by writing the whole creation code inline in configure.ac, as the autobook suggests). I find this approach less maintainable than using AC_SUBST, but it's more flexible.

Of course, to help the compiler find the platform-dependent header you'll probably also want to write a pkgconfig script, like GLib does.

DanielKO
  • 4,422
  • 19
  • 29
  • Thanks, it's curious to me that the whole process has not been anticipated by the autotools developers, it must come up quite often. – crobar Nov 19 '13 at 08:29
  • 1
    @crobar In a small statistic of 14 libraries that distributed a config.h I saw 9 of them renaming it, and 4 including it. – rwst May 12 '16 at 13:40
  • `compiler find the platform-dependent header you'll probably also want to write a pkgconfig script` could you be more specific, please? I would like to search existing library for platform (configure) dependent header files. – kravemir Dec 27 '17 at 15:00
  • I'm not sure I understand what you mean. Are you creating a library that needs to install such headers, or are you trying to find such headers? The tool I mentioned, `pkg-config`, is just a way to hide all the magic required in order to use the library. Try `man pkg-config`, or Google. – DanielKO Jan 08 '18 at 17:19
1

You will need to install config.h if it affects the interface. In practical terms, if the #define's are required by the header(s), not just the .cc implementation / compilation units.

If config.h is a problem, you can specify another name in the AC_CONFIG_HEADERS macro. e.g., AC_CONFIG_HEADERS([foo_config.h]).

The easiest way to install the header, assuming automake, is with:

nodist_include_HEADERS = foo_config.h

in the top-level Makefile.am. the nodist prefix tells automake that foo_config.h is generated rather than distributed with the package.

If not using automake, install foo_config.h in $includedir. $exec_prefix/include is arguably a more correct location for a generated header, but in practice the former location is fine.


I avoid using config.h, by passing relevant definitions in CPPFLAGS or foo_CPPFLAGS along with AC_SUBST to Makefile.am for source builds, or AC_SUBST to foo.h.in to generate headers at configure-time. A lot of config.h is test-generated noise. It requires more infrastructure, but it's what I prefer. I wouldn't recommend this approach unless you're comfortable with the autotools.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • if I just change the name, and not the actual definitions in foo-config.h is this likely to ever be a problem? – crobar Oct 25 '13 at 15:02
  • @crobar - I don't think so. `autoheader` (or `autoreconf`) should honour the `AC_CONFIG_HEADERS` name to produce `foo_config.h.in`; and configure should use this template to generate `foo_config.h`. – Brett Hale Oct 25 '13 at 15:07
  • 1
    I'm working on an existing project, it uses throughout it things like nr_double_t for double as defined in config.h. In fact I've realized my headers do not require this. I'll mark your answer correct, but just add that others might be interested in the m4 macro `AX_PREFIX_CONFIG_H`, which also changes all the definitions in the config file to be unique to your project. Thanks! – crobar Oct 25 '13 at 15:46