GNU Autoconf cheatsheet from a first configure script to portable checks, optional features, cross compilation, and reusable macros.
Autoconf Cheatsheet
Section titled “Autoconf Cheatsheet”GNU Autoconf creates a portable configure shell script from configure.ac. The generated script detects compilers, headers, functions, libraries, and user-selected options, then creates configured build files such as Makefile and config.h.
configure.ac --autoconf/autoheader--> configure + config.h.inMakefile.in --./configure----------> Makefileconfig.h.in --./configure----------> config.hsource files --make-----------------> program| Tool or File | Purpose |
|---|---|
configure.ac | Source file written by the project maintainer using Autoconf macros |
autoconf | Generates configure from configure.ac |
autoheader | Generates config.h.in when AC_CONFIG_HEADERS is used |
autoreconf | Runs the required Autotools commands to refresh generated files |
configure | Portable script run by someone building the package |
config.status | Recreates configured output files after configure has run |
Makefile.in | Template converted into Makefile by configure |
config.h.in | Template converted into config.h by configure |
Autoconf configures a project; it does not compile it. make, a compiler, or another build tool performs the actual build after configuration.
On Windows, Autoconf and generated
configurescripts normally run in a POSIX-like environment such as MSYS2, Cygwin, or WSL.
1. Beginner: The Normal Workflow
Section titled “1. Beginner: The Normal Workflow”Building a Released Package
Section titled “Building a Released Package”When downloading source that already contains a configure script, you usually do not need Autoconf installed:
tar -xf hello-1.0.tar.gzcd hello-1.0./configuremakemake check # If the project provides testssudo make install # Optional; consider a user-writable --prefix insteadCommon user options:
./configure --help./configure --prefix="$HOME/.local"./configure CC=clang CFLAGS="-O2 -g"make -j4make install| Option | Meaning |
|---|---|
--help | List standard and project-specific configuration options |
--prefix=DIR | Install architecture-independent and architecture-dependent files under DIR |
--bindir=DIR | Override the executable installation directory |
--includedir=DIR | Override the header installation directory |
--libdir=DIR | Override the library installation directory |
CC=clang | Select a C compiler for configure tests and the build |
CPPFLAGS="-I..." | Add preprocessor/include options |
CFLAGS="-O2 -g" | Add C compiler options |
LDFLAGS="-L..." | Add link-editor options |
LIBS="-lm" | Add libraries to the link command |
Maintainer Workflow
Section titled “Maintainer Workflow”When editing configure.ac, regenerate the configuration machinery before testing it:
autoreconf --install./configuremakeDuring active maintenance, a useful cycle is:
autoreconf --force --installmkdir -p buildcd build../configure CFLAGS="-Wall -Wextra -g"makeAn out-of-tree build keeps generated build output separate from the source directory.
2. First Autoconf Project
Section titled “2. First Autoconf Project”Directory Layout
Section titled “Directory Layout”hello-autoconf/|-- configure.ac|-- Makefile.in`-- src/ `-- main.cconfigure.ac
Section titled “configure.ac”AC_PREREQ([2.69])AC_INIT([hello-autoconf], [1.0.0], [bugs@example.org])AC_CONFIG_SRCDIR([src/main.c])AC_CONFIG_HEADERS([config.h])
AC_PROG_CCAC_CHECK_HEADERS([stdlib.h string.h])AC_CHECK_FUNCS([strdup])
AC_CONFIG_FILES([Makefile])AC_OUTPUT| Macro | What It Does |
|---|---|
AC_PREREQ([2.69]) | Requires at least the given Autoconf version when generating configure |
AC_INIT(...) | Defines package name, version, and bug-report address |
AC_CONFIG_SRCDIR([...]) | Ensures configure is being run for the expected source tree |
AC_CONFIG_HEADERS([config.h]) | Requests a generated C header containing feature definitions |
AC_PROG_CC | Finds and tests a working C compiler |
AC_CHECK_HEADERS([...]) | Defines HAVE_..._H for available headers |
AC_CHECK_FUNCS([...]) | Defines HAVE_... for available functions |
AC_CONFIG_FILES([Makefile]) | Configures Makefile from Makefile.in |
AC_OUTPUT | Creates config.status and produces configured output files |
src/main.c
Section titled “src/main.c”#include <config.h>
#include <stdio.h>
int main(void) { printf("Hello from %s!\n", PACKAGE_STRING); return 0;}Include config.h before system headers when the configuration definitions may affect declarations exposed by those headers.
Makefile.in
Section titled “Makefile.in”Recipe lines beneath targets must start with a tab.
srcdir = @srcdir@
CC = @CC@CPPFLAGS = @CPPFLAGS@ -I.CFLAGS = @CFLAGS@LDFLAGS = @LDFLAGS@LIBS = @LIBS@
PROGRAM = helloSOURCES = $(srcdir)/src/main.c
.PHONY: all clean distclean
all: $(PROGRAM)
$(PROGRAM): $(SOURCES) config.h $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $(SOURCES) $(LDFLAGS) $(LIBS)
clean: rm -f $(PROGRAM)
distclean: clean rm -f Makefile config.h config.log config.statusAutoconf replaces substitutions such as @CC@, @CFLAGS@, and @LIBS@ when it creates Makefile.
Generate, Configure, and Build
Section titled “Generate, Configure, and Build”autoreconf --install./configure CFLAGS="-Wall -Wextra -g"make./helloTypical generated files:
configure # Distribute this so builders do not need Autoconfconfig.h.in # Distribute this if config.h is generatedconfig.log # Local diagnostics from running configureconfig.status # Local regeneration helperconfig.h # Local configured headerMakefile # Local configured build rules3. configure.ac Structure and M4 Quoting
Section titled “3. configure.ac Structure and M4 Quoting”Recommended Order
Section titled “Recommended Order”AC_PREREQ([2.69])AC_INIT([package-name], [version], [bugs@example.org], [tar-name], [homepage])AC_CONFIG_SRCDIR([src/main.c])AC_CONFIG_HEADERS([config.h])
# Tool discoveryAC_PROG_CCAC_PROG_INSTALL
# Options selected by the builderAC_ARG_ENABLE(...)AC_ARG_WITH(...)
# System feature checksAC_CHECK_HEADERS(...)AC_CHECK_FUNCS(...)AC_SEARCH_LIBS(...)
# Generated outputsAC_CONFIG_FILES([Makefile src/Makefile])AC_OUTPUTQuote Arguments with Square Brackets
Section titled “Quote Arguments with Square Brackets”Autoconf input is processed by M4. Bracket-quote literal macro arguments consistently:
AC_INIT([calculator], [2.1.0], [bugs@example.org])AC_CHECK_HEADERS([stdint.h unistd.h])AC_CHECK_FUNCS([getline snprintf])Do not write shell tests as if this were C:
# Good: portable shell used inside generated configureif test "x$enable_debug" = xyes; then AC_DEFINE([ENABLE_DEBUG], [1], [Define to enable debug logging.])fiUseful Maintainer Commands
Section titled “Useful Maintainer Commands”| Command | Purpose |
|---|---|
autoconf | Regenerate only configure |
autoheader | Regenerate only config.h.in |
autoreconf --install | Regenerate required files and install auxiliary support files if needed |
autoreconf --force --install | Force regeneration after changing macros or included M4 files |
autoupdate | Rewrite obsolete Autoconf macro spellings where possible |
autoscan | Inspect a source tree and propose starting checks in configure.scan |
ifnames | List C preprocessor conditionals used by source files |
4. Substitution Variables and config.h
Section titled “4. Substitution Variables and config.h”Autoconf communicates configuration results in two common ways:
| Mechanism | Best For | Used In |
|---|---|---|
AC_SUBST and @NAME@ | Build commands, directories, compiler flags, filenames | Makefile.in, scripts, .pc.in files |
AC_DEFINE and config.h | C/C++ conditional compilation | Source and header files |
Substitute a Makefile Variable
Section titled “Substitute a Makefile Variable”AC_ARG_WITH([greeting], [AS_HELP_STRING([--with-greeting=TEXT], [message printed by the program])], [GREETING=$withval], [GREETING=Hello])AC_SUBST([GREETING])GREETING = @GREETING@Define a C Macro
Section titled “Define a C Macro”AC_DEFINE([ENABLE_LOGGING], [1], [Define to compile logging support.])Generated config.h contains a definition resembling:
#define ENABLE_LOGGING 1For a shell-derived value, use AC_DEFINE_UNQUOTED carefully:
AC_DEFINE_UNQUOTED([APP_DEFAULT_PORT], [$default_port], [Default TCP port used by the application.])Source-Level Conditional Compilation
Section titled “Source-Level Conditional Compilation”#include <config.h>
#ifdef HAVE_UNISTD_H# include <unistd.h>#endif
#ifdef ENABLE_LOGGING# include <stdio.h># define LOG(message) fprintf(stderr, "%s\n", (message))#else# define LOG(message) ((void) 0)#endif5. Detecting Compilers, Headers, Types, and Functions
Section titled “5. Detecting Compilers, Headers, Types, and Functions”Portable software should test for the feature it needs instead of guessing from an operating-system name.
Languages and Compilers
Section titled “Languages and Compilers”| Macro | Purpose |
|---|---|
AC_PROG_CC | Find a C compiler and configure C language tests |
AC_PROG_CXX | Find a C++ compiler |
AC_LANG([C]) | Select C for following checks |
AC_LANG([C++]) | Select C++ for following checks |
AC_LANG_PUSH([C++]) / AC_LANG_POP([C++]) | Temporarily perform checks as C++ |
AC_PROG_CCAC_PROG_CXX
AC_LANG_PUSH([C++])AC_COMPILE_IFELSE( [AC_LANG_PROGRAM([[#include <vector>]], [[std::vector<int> v; v.push_back(1);]])], [AC_MSG_NOTICE([C++ vector support is available])], [AC_MSG_ERROR([a usable C++ standard library is required])])AC_LANG_POP([C++])Headers
Section titled “Headers”AC_CHECK_HEADERS([stdint.h stdlib.h string.h unistd.h])AC_CHECK_HEADERS([sys/socket.h netinet/in.h arpa/inet.h])These checks commonly create definitions such as:
#define HAVE_STDINT_H 1#define HAVE_SYS_SOCKET_H 1When one header needs another header first, supply prerequisite includes:
AC_CHECK_HEADERS([sys/socket.h])AC_CHECK_HEADERS([netinet/in.h], [], [],[AC_INCLUDES_DEFAULT[#ifdef HAVE_SYS_SOCKET_H# include <sys/socket.h>#endif]])Functions
Section titled “Functions”AC_CHECK_FUNCS([getline memmove snprintf strdup strnlen])Check one function with explicit failure behavior:
AC_CHECK_FUNC([clock_gettime], [AC_DEFINE([HAVE_CLOCK_GETTIME], [1], [Define if clock_gettime is available.])], [AC_MSG_WARN([clock_gettime unavailable; using fallback timing])])Types, Members, and Sizes
Section titled “Types, Members, and Sizes”AC_CHECK_TYPES([ssize_t, uint32_t])AC_CHECK_MEMBERS([struct stat.st_mtim])AC_CHECK_SIZEOF([long])AC_TYPE_SIZE_TAC_TYPE_PID_T| Check | Possible Definition or Result |
|---|---|
AC_CHECK_TYPES([uint32_t]) | HAVE_UINT32_T |
AC_CHECK_MEMBERS([struct stat.st_mtim]) | HAVE_STRUCT_STAT_ST_MTIM |
AC_CHECK_SIZEOF([long]) | SIZEOF_LONG |
AC_TYPE_PID_T | Provides a usable pid_t fallback when needed |
6. Libraries and External Dependencies
Section titled “6. Libraries and External Dependencies”Search for a Function in a Library
Section titled “Search for a Function in a Library”Use AC_SEARCH_LIBS when a function may already be in the C library on some platforms but require an extra library on others:
AC_SEARCH_LIBS([clock_gettime], [rt], [], [AC_MSG_ERROR([clock_gettime is required])])
AC_SEARCH_LIBS([socket], [socket], [], [AC_MSG_ERROR([socket support is required])])If a library is required unconditionally and its API function is unique to it:
AC_CHECK_LIB([m], [sin], [], [AC_MSG_ERROR([the math library is required])])Successful library checks update LIBS, so preserve @LIBS@ at the end of link commands in Makefile.in:
$(CC) $(CPPFLAGS) $(CFLAGS) -o app $(OBJECTS) $(LDFLAGS) $(LIBS)Header Plus Library Requirement
Section titled “Header Plus Library Requirement”AC_CHECK_HEADERS([zlib.h], [], [AC_MSG_ERROR([zlib headers are required])])AC_CHECK_LIB([z], [inflate], [], [AC_MSG_ERROR([zlib library is required])])pkg-config with pkg.m4
Section titled “pkg-config with pkg.m4”Projects that provide .pc metadata can be checked through the PKG_CHECK_MODULES macro supplied by pkg-config:
PKG_PROG_PKG_CONFIGPKG_CHECK_MODULES([LIBXML], [libxml-2.0 >= 2.9])AC_SUBST([LIBXML_CFLAGS])AC_SUBST([LIBXML_LIBS])CPPFLAGS = @CPPFLAGS@ @LIBXML_CFLAGS@LIBS = @LIBXML_LIBS@ @LIBS@This requires the pkg.m4 macro to be available when generating configure, often through aclocal or a local m4/ directory.
7. User Options: Optional Features and Dependencies
Section titled “7. User Options: Optional Features and Dependencies”--enable-feature
Section titled “--enable-feature”Use AC_ARG_ENABLE for behavior built into your package:
AC_ARG_ENABLE([debug], [AS_HELP_STRING([--enable-debug], [compile debug logging support])], [], [enable_debug=no])
AS_CASE([$enable_debug], [yes], [AC_DEFINE([ENABLE_DEBUG], [1], [Define to compile debug logging support.])], [no], [], [AC_MSG_ERROR([--enable-debug accepts only yes or no])])./configure --enable-debug./configure --disable-debug--with-package
Section titled “--with-package”Use AC_ARG_WITH for external software or a selected path:
AC_ARG_WITH([ssl], [AS_HELP_STRING([--with-ssl=PREFIX], [use TLS support installed below PREFIX])], [ssl_prefix=$withval], [ssl_prefix=no])
AS_IF([test "x$ssl_prefix" != xno], [ AS_IF([test "x$ssl_prefix" != xyes], [ CPPFLAGS="$CPPFLAGS -I$ssl_prefix/include" LDFLAGS="$LDFLAGS -L$ssl_prefix/lib" ]) AC_CHECK_HEADERS([openssl/ssl.h], [], [AC_MSG_ERROR([OpenSSL headers not found])]) AC_CHECK_LIB([ssl], [SSL_new], [], [AC_MSG_ERROR([OpenSSL library not found])]) AC_DEFINE([WITH_SSL], [1], [Define to build TLS support.])])./configure --with-ssl./configure --with-ssl=/opt/opensslHelpful Configure Messages
Section titled “Helpful Configure Messages”AC_MSG_NOTICE([checking optional networking support])AC_MSG_WARN([documentation will not be generated])AC_MSG_ERROR([libfoo >= 2.0 is required])AC_MSG_CHECKING([whether experimental mode is enabled])AC_MSG_RESULT([$enable_experimental])Use an error when a missing capability makes the requested build impossible; use a warning when a safe reduced build remains usable.
8. Custom Compile and Link Tests
Section titled “8. Custom Compile and Link Tests”Prefer built-in macros first. When they cannot express a required API or compiler behavior, use a focused test.
Compile a Small Program
Section titled “Compile a Small Program”AC_COMPILE_IFELSE( [AC_LANG_PROGRAM( [[#include <stdatomic.h>]], [[_Atomic int counter = 0; return (int) counter;]])], [AC_DEFINE([HAVE_C11_ATOMICS], [1], [Define if C11 atomics are available.])], [AC_MSG_WARN([C11 atomics unavailable; using mutex fallback])])Link Against an API
Section titled “Link Against an API”save_LIBS=$LIBSLIBS="$LIBS -lpthread"AC_LINK_IFELSE( [AC_LANG_PROGRAM( [[#include <pthread.h>]], [[pthread_t t; return pthread_create(&t, 0, 0, 0);]])], [AC_DEFINE([HAVE_PTHREAD_CREATE], [1], [Define if POSIX threads are linkable.])], [AC_MSG_ERROR([POSIX threads are required])])LIBS=$save_LIBSFor normal thread support, an established macro from a macro archive or project convention may handle platform-specific flags more fully than a small hand-written probe.
Run-Time Tests
Section titled “Run-Time Tests”AC_RUN_IFELSE( [AC_LANG_PROGRAM([], [[return 0;]])], [AC_MSG_NOTICE([test program ran successfully])], [AC_MSG_ERROR([test program failed])], [AC_MSG_NOTICE([cross compiling: skipping run-time probe])])Always provide a cross-compiling result or a policy for AC_RUN_IFELSE: a cross build cannot normally execute target binaries on the build machine.
9. Out-of-Tree Builds, Installation, and Reconfiguration
Section titled “9. Out-of-Tree Builds, Installation, and Reconfiguration”VPATH / Separate Build Directory
Section titled “VPATH / Separate Build Directory”mkdir build-debugcd build-debug../configure --prefix="$HOME/.local" CFLAGS="-O0 -g"makemake installFor this to work cleanly:
| Practice | Reason |
|---|---|
Use @srcdir@ in hand-written Makefile.in templates | Source files remain reachable from the build directory |
Add -I. when including generated config.h | The header lives in the build directory |
Avoid generating files into source directories during make | Multiple build directories remain independent |
Recreate Outputs with config.status
Section titled “Recreate Outputs with config.status”./config.status # Recreate configured files./config.status Makefile # Recreate one configured output./config.status --recheck # Rerun configure using its saved argumentsStandard Installation Directory Variables
Section titled “Standard Installation Directory Variables”Use Autoconf directory substitutions rather than hard-coded install paths:
prefix = @prefix@exec_prefix = @exec_prefix@bindir = @bindir@includedir = @includedir@libdir = @libdir@
install: mkdir -p "$(DESTDIR)$(bindir)" cp hello "$(DESTDIR)$(bindir)/hello"./configure --prefix=/usr/localmakemake DESTDIR="$PWD/stage" installDESTDIR is commonly used to stage package contents without changing the configured prefix.
10. Cross Compilation and Cached Answers
Section titled “10. Cross Compilation and Cached Answers”Build, Host, and Target
Section titled “Build, Host, and Target”| Term | Meaning |
|---|---|
| Build | Machine on which configure and compiler commands run |
| Host | Machine on which the built program or library will run |
| Target | Machine for which a compiler or debugger produced by the package will generate code |
Most ordinary applications care about --host; compiler/toolchain projects may also care about --target.
AC_CANONICAL_BUILDAC_CANONICAL_HOSTAC_PROG_CC./configure --build=x86_64-pc-linux-gnu \ --host=aarch64-linux-gnu \ CC=aarch64-linux-gnu-gccmakeAvoid Assuming Run-Time Test Results
Section titled “Avoid Assuming Run-Time Test Results”For a cross build, allow the builder to provide a cached answer:
AC_CACHE_CHECK([whether the target has working frobnicate], [ac_cv_have_working_frobnicate], [ AC_RUN_IFELSE( [AC_LANG_PROGRAM([[#include <frobnicate.h>]], [[return frobnicate() ? 0 : 1;]])], [ac_cv_have_working_frobnicate=yes], [ac_cv_have_working_frobnicate=no], [AC_MSG_ERROR([set ac_cv_have_working_frobnicate for cross compilation])]) ])
AS_IF([test "x$ac_cv_have_working_frobnicate" = xyes], [ AC_DEFINE([HAVE_WORKING_FROBNICATE], [1], [Define if frobnicate works on the host system.])])The cross builder can then explicitly provide the result:
./configure --host=aarch64-linux-gnu \ ac_cv_have_working_frobnicate=yesCache File
Section titled “Cache File”./configure --cache-file=config.cacherm -f config.cache # Discard stale cached test answersCache files can make repeated configurations faster, but do not reuse results after changing compilers, sysroots, or target systems.
11. Optional Automake Integration
Section titled “11. Optional Automake Integration”Autoconf can configure hand-written Makefile.in files. Automake is a separate companion tool that creates Makefile.in from shorter Makefile.am declarations.
Layout
Section titled “Layout”hello-automake/|-- configure.ac|-- Makefile.am`-- src/ |-- Makefile.am `-- main.cconfigure.ac
Section titled “configure.ac”AC_PREREQ([2.69])AC_INIT([hello-automake], [1.0.0], [bugs@example.org])AM_INIT_AUTOMAKE([foreign subdir-objects])AC_CONFIG_SRCDIR([src/main.c])AC_CONFIG_HEADERS([config.h])AC_PROG_CCAC_CONFIG_FILES([Makefile src/Makefile])AC_OUTPUTMakefile.am
Section titled “Makefile.am”SUBDIRS = srcsrc/Makefile.am
Section titled “src/Makefile.am”bin_PROGRAMS = hellohello_SOURCES = main.chello_CPPFLAGS = -I$(top_builddir)Generate and Build
Section titled “Generate and Build”autoreconf --install./configuremakemake checkmake distcheck| Automake Feature | Purpose |
|---|---|
bin_PROGRAMS | Programs installed in bindir |
noinst_PROGRAMS | Programs built but not installed |
*_SOURCES | Source files for a named program or library |
check_PROGRAMS | Test programs built for make check |
TESTS | Test executables or scripts run by make check |
make dist | Produce a source distribution archive |
make distcheck | Test distribution, build, install, uninstall, and cleanup workflows |
12. Advanced: Local Macros and Macro Directories
Section titled “12. Advanced: Local Macros and Macro Directories”As configuration logic grows, move reusable probes from configure.ac into M4 macro files.
m4/ax_check_widget.m4
Section titled “m4/ax_check_widget.m4”AC_DEFUN([AX_CHECK_WIDGET], [ AC_CHECK_HEADERS([widget.h], [], [AC_MSG_ERROR([widget.h is required])]) AC_SEARCH_LIBS([widget_init], [widget], [], [AC_MSG_ERROR([libwidget is required])])])Use the Macro
Section titled “Use the Macro”AC_CONFIG_MACRO_DIRS([m4])AX_CHECK_WIDGETWith Automake, also declare the macro directory:
ACLOCAL_AMFLAGS = -I m4Then regenerate:
autoreconf --force --installAC_CONFIG_MACRO_DIRS is available in current Autoconf releases. Projects that intentionally support older Autoconf can use the older singular form, AC_CONFIG_MACRO_DIR([m4]).
Macro Authoring Guidelines
Section titled “Macro Authoring Guidelines”| Practice | Benefit |
|---|---|
Prefix project or third-party macros, such as AX_ or MYPROJECT_ | Avoid naming collisions |
Use AC_REQUIRE([AC_PROG_CC]) inside a macro that needs a C compiler | Ensure prerequisites run once and in the proper order |
Save and restore CPPFLAGS, LDFLAGS, or LIBS around temporary tests | Avoid leaking unwanted settings into later checks |
Use AC_CACHE_CHECK for expensive or user-overridable probes | Make results visible and usable for cross builds |
| Keep probes small and feature-oriented | Reduce false failures across platforms |
Macro Requiring a Compiler
Section titled “Macro Requiring a Compiler”AC_DEFUN([MYPROJECT_CHECK_FAST_INT], [ AC_REQUIRE([AC_PROG_CC]) AC_CHECK_SIZEOF([long]) AS_IF([test "$ac_cv_sizeof_long" -ge 8], [ AC_DEFINE([HAVE_64BIT_LONG], [1], [Define if long is at least 64 bits wide.]) ])])13. Debugging configure Problems
Section titled “13. Debugging configure Problems”Inspect the Evidence
Section titled “Inspect the Evidence”| File or Command | Use It For |
|---|---|
config.log | Compiler commands, error messages, and failed test programs |
./configure --help | Supported options and directory variables |
./config.status --recheck | Repeat configuration with saved arguments |
autoreconf -vfi | Verbose regeneration of generated files |
autoconf -Wall | Warnings while generating configure |
Common Failures
Section titled “Common Failures”| Symptom | Likely Cause | Fix |
|---|---|---|
configure: error: cannot find required auxiliary files | A tool such as Automake requires helper scripts | Run autoreconf --install |
config.h.in is missing | AC_CONFIG_HEADERS exists but autoheader was not run | Run autoreconf --install or autoheader |
| Macro appears as literal text in generated output | Macro is unavailable to aclocal/M4 | Install its provider or configure m4/ macro lookup |
| A library is installed but check fails | Missing include path, library path, dependency, or wrong architecture | Inspect config.log; set CPPFLAGS and LDFLAGS correctly |
Works in source tree but not build/ | Templates assume source and build directories are identical | Use @srcdir@, @top_srcdir@, and build-directory include paths |
| Cross compile fails at a run test | Configure tries to execute target code | Add a cross result path or supply a cache answer |
Changing CFLAGS seems ignored | Stale cache or reusing generated build files | Configure a fresh build directory or remove config.cache |
Debug a Failed Check
Section titled “Debug a Failed Check”./configure CPPFLAGS="-I/opt/foo/include" LDFLAGS="-L/opt/foo/lib"less config.logLook for the failing compiler invocation and its small test program. It usually reveals a missing header, unresolved symbol, wrong compiler, or unsupported flag.
14. Maintainer Practices
Section titled “14. Maintainer Practices”| Practice | Reason |
|---|---|
Distribute configure, config.h.in, and needed Makefile.in files in release archives | Users building a release should not need Autotools |
| Regenerate generated files from maintained inputs instead of hand-editing them | Changes remain reproducible |
| Test at least one clean out-of-tree build | Catches source/build path mistakes |
Test useful combinations of --enable-* and --with-* options | Finds conditional-build breakage |
Let users override CC, CPPFLAGS, CFLAGS, LDFLAGS, and LIBS | Supports alternate compilers, packaging, and cross builds |
| Test features rather than OS names | Platforms and library availability do not map cleanly to names |
Put detailed failed-probe evidence in config.log | Makes reports diagnosable |
Suggested Release Check
Section titled “Suggested Release Check”autoreconf --force --installrm -rf build-checkmkdir build-checkcd build-check../configure --prefix="$PWD/install" CFLAGS="-Wall -Wextra -O2"makemake checkmake installWhen using Automake:
make distcheck15. Quick Macro Reference
Section titled “15. Quick Macro Reference”Setup and Output
Section titled “Setup and Output”| Macro | Purpose |
|---|---|
AC_PREREQ([version]) | Require a maintainer-side Autoconf version |
AC_INIT([pkg], [version], [bugs]) | Initialize package metadata |
AC_CONFIG_SRCDIR([file]) | Verify source directory location |
AC_CONFIG_HEADERS([config.h]) | Generate a configuration header |
AC_CONFIG_FILES([Makefile]) | Generate files from .in templates |
AC_CONFIG_MACRO_DIRS([m4]) | Declare local macro directories |
AC_OUTPUT | Produce configured outputs |
Tools and Programs
Section titled “Tools and Programs”| Macro | Purpose |
|---|---|
AC_PROG_CC | Find C compiler |
AC_PROG_CXX | Find C++ compiler |
AC_PROG_INSTALL | Find a BSD-compatible install program |
AC_CHECK_PROG | Find one program |
AC_CHECK_PROGS | Find the first available program from a list |
Platform Capabilities
Section titled “Platform Capabilities”| Macro | Purpose |
|---|---|
AC_CHECK_HEADERS | Test for headers |
AC_CHECK_FUNCS | Test for functions |
AC_CHECK_DECLS | Test for declared symbols |
AC_CHECK_TYPES | Test for types |
AC_CHECK_MEMBERS | Test for structure members |
AC_CHECK_SIZEOF | Determine size of a type |
AC_SEARCH_LIBS | Find a library needed for a function |
AC_CHECK_LIB | Check a specific library for a function |
Options and Results
Section titled “Options and Results”| Macro | Purpose |
|---|---|
AC_ARG_ENABLE | Add --enable-* / --disable-* feature options |
AC_ARG_WITH | Add --with-* / --without-* dependency options |
AC_SUBST | Substitute a variable into output templates |
AC_DEFINE | Define a preprocessor symbol |
AS_HELP_STRING | Format option help text |
AS_IF / AS_CASE | Use portable generated-shell conditionals |
AC_MSG_NOTICE | Print information |
AC_MSG_WARN | Print a warning |
AC_MSG_ERROR | Stop configuration with an error |
Custom Testing
Section titled “Custom Testing”| Macro | Purpose |
|---|---|
AC_COMPILE_IFELSE | Check whether a source fragment compiles |
AC_LINK_IFELSE | Check whether a source fragment links |
AC_RUN_IFELSE | Check run-time behavior; needs cross-compile handling |
AC_CACHE_CHECK | Cache and report a configuration test result |
AC_DEFUN | Define a reusable M4 macro |
AC_REQUIRE | Ensure another macro has expanded first |
16. Learning Path
Section titled “16. Learning Path”- Configure and build an existing released package with
./configure --help,./configure, andmake. - Create a one-file C project using
configure.ac,Makefile.in,AC_PROG_CC, andAC_CONFIG_FILES. - Add
config.hwith header and function checks, then use#ifdef HAVE_...in C. - Add
--enable-debugand an optional dependency usingAC_ARG_ENABLEandAC_ARG_WITH. - Test an out-of-tree build and a custom installation prefix.
- Study
config.logafter intentionally requesting a missing header or library. - Try an Automake project and run
make distcheck. - Extract a repeated dependency test into a local M4 macro.
- Design configuration tests that still work during cross compilation.
Command Summary
Section titled “Command Summary”# User building a release./configure --prefix="$HOME/.local"makemake checkmake install
# Maintainer after editing configure.ac or M4 macrosautoreconf --force --install
# Clean separate build directorymkdir build && cd build../configure CC=clang CFLAGS="-Wall -Wextra -g"make
# Regenerate configured output files./config.status./config.status --recheck
# Cross compile example./configure --host=aarch64-linux-gnu CC=aarch64-linux-gnu-gcc
# Diagnose a failureless config.log