Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct installation of config.h for shared library using autotools

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.

like image 292
crobar Avatar asked Sep 21 '25 12:09

crobar


1 Answers

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. Example: GLib, before they switched to meson:

# From glib-2.53.1/glib/Makefile.am:
#
# Generate glibconfig.h
#
# The timestamp of the stamp file is used to indicate if glibconfig.h is
# up to date with respect to config.status.  In the usual case, changes
# to config.status will not result in changes to glibconfig.h so we
# avoid touching its timestamp (in order not to rebuild the whole tree).
#
DISTCLEANFILES += glibconfig-stamp glibconfig.h glibconfig.h.win32
BUILT_SOURCES += glibconfig-stamp
configexecincludedir = $(libdir)/glib-2.0/include
nodist_configexecinclude_HEADERS = glibconfig.h
glibconfig-stamp: ../config.status
        $(AM_V_GEN) cd $(top_builddir) && \
          $(SHELL) ./config.status glib/glibconfig.h
        $(AM_V_GEN) cd $(top_builddir) && \
          $(SHELL) ./config.status glib/glibconfig.h.win32
        @touch glibconfig-stamp

Note that they generate glibconfig.h without a template, by invoking config.status; the generation code originates from configure.ac:

dnl From glib-2.53.1/configure.ac:
dnl
dnl this section will only be run if config.status is invoked with no
dnl arguments, or with "glib/glibconfig.h" as an argument.
AC_CONFIG_COMMANDS([glib/glibconfig.h],
[
        outfile=glib/glibconfig.h-tmp
        cat > $outfile <<\_______EOF
/* glibconfig.h
 *
 * This is a generated file.  Please modify 'configure.ac'
 */

#ifndef __GLIBCONFIG_H__
#define __GLIBCONFIG_H__

#include <glib/gmacros.h>
(...etc...)
        if cmp -s $outfile glib/glibconfig.h; then
          AC_MSG_NOTICE([glib/glibconfig.h is unchanged])
          rm -f $outfile
        else
          mv $outfile glib/glibconfig.h
        fi
],[
dnl commands to set the environment variables used during output
])

This is what the autobook suggests, though its examples are failing to render. 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.

like image 147
DanielKO Avatar answered Sep 23 '25 01:09

DanielKO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!