### Resulting configure text from quoting example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Autoconf-Language.html Shows the output in configure after the previous quoting example is processed. ```autoconf echo "Hard rock was here! --oops" echo "Hard rock was here! --AC_DC" echo "Hard rock was here! --AC_DC" ``` -------------------------------- ### Makefile.am for a simple 'Hello world' program Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Automake.html Use this Makefile.am to build and install a simple 'Hello world' program with Automake. The resulting Makefile.in (~400 lines) automatically supports standard targets, Autoconf substitutions, automatic dependency tracking, and VPATH building. 'make' builds the 'hello' program, and 'make install' installs it in /usr/local/bin (or the prefix given to configure). ```makefile bin_PROGRAMS = hello hello_SOURCES = hello.c ``` -------------------------------- ### Help string example for --with-readline Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/External-Software.html Example of a help-string for the --with-readline option, showing proper formatting with leading whitespace. Avoid tabs in help strings. ```text --with-readline support fancy command line editing ``` -------------------------------- ### Basic Package Installation Commands Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Basic-Installation.html The standard sequence of shell commands to configure, build, and install a package. The bootstrap step is only required for developer checkouts where the configure script is missing. ```shell test -f configure || ./bootstrap ./configure make make install ``` -------------------------------- ### m4_version_compare examples Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Number-processing-Macros.html Examples of comparing version strings with m4_version_compare. Each example shows the expansion result after the ⇒ symbol. ```m4 m4_version_compare([1.1], [2.0]) ⇒-1 m4_version_compare([2.0b], [2.0a]) ⇒1 m4_version_compare([1.1.1], [1.1.1a]) ⇒-1 m4_version_compare([1.2], [1.1.1a]) ⇒1 m4_version_compare([1.0], [1]) ⇒0 m4_version_compare([1.1pre], [1.1PRE]) ⇒0 m4_version_compare([1.1a], [1,10]) ⇒-1 m4_version_compare([2.61a], [2.61a-248-dc51]) ⇒-1 m4_version_compare([2.61b], [2.61a-248-dc51]) ⇒1 ``` -------------------------------- ### AC_PREPROC_IFELSE example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Running-the-Preprocessor.html Shows a complete configure.ac using AC_PREPROC_IFELSE to run the preprocessor on a C program. The macro uses CPPFLAGS, not CFLAGS, and on success the preprocessed output is available in conftest.i. The example defines HELLO_WORLD and checks that the program preprocesses successfully. ```autoconf AC_INIT([Hello], [1.0], [bug-hello@example.org]) AC_DEFINE([HELLO_WORLD], ["Hello, World\n"], [Greetings string.]) AC_PREPROC_IFELSE( [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]], [[fputs (hw, stdout);]])], [AC_MSG_RESULT([OK])], [AC_MSG_FAILURE([unexpected preprocessor failure])]) ``` -------------------------------- ### Autoconf --program-transform-name example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Transformation-Examples.html Uses a sed script to prepend 'g' to most program names, except those already starting with 'g' (like gdb) and non-GNU programs like less and lesskey. The transformation is applied during 'make install'. ```shell --program-transform-name='s/^/g/; s/^gg/g/; s/^gless/less/' ``` -------------------------------- ### configure --help output for --with-foo Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Pretty-Help-Strings.html Shows the expected output of configure --help after using the basic AS_HELP_STRING example. The option and description are aligned in columns. ```text --enable and --with options recognized: --with-foo use foo (default is no) ``` -------------------------------- ### AC_ERLANG_CHECK_LIB Example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Erlang-Libraries.html Checks if the stdlib library is installed and prints its version and directory, or triggers an error if not found. ```m4 AC_ERLANG_CHECK_LIB([stdlib], [echo "stdlib version \"$ERLANG_LIB_VER_stdlib\"" echo "is installed in \"$ERLANG_LIB_DIR_stdlib\""], [AC_MSG_ERROR([stdlib was not found!])]) ``` -------------------------------- ### configure --help output for MY_ARG_WITH Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Pretty-Help-Strings.html Shows the expected output of configure --help after using the macro argument example. The option name is transformed from underscores to hyphens. ```text --enable and --with options recognized: --with-a-b use a_b (default is no) ``` -------------------------------- ### Ash 0.2: Avoid using $? after expanding empty or unset variables or at the start of an eval Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Shellology.html This snippet demonstrates a common pitfall in Ash 0.2 where using '$?' after expanding empty or unset variables, or at the start of an 'eval', can lead to incorrect results. The example shows two problematic usages: one after expanding an empty variable and one at the start of an 'eval'. Avoid these patterns for portability. ```shell foo= false $foo echo "Do not use it: $?" false eval 'echo "Do not use it: $?"' ``` -------------------------------- ### AH_BOTTOM example with custom header include Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Autoheader-Macros.html Use AH_BOTTOM to include text at the bottom of the header template file. The text is included verbatim, so it can get mangled when the template is processed. ```m4 AH_BOTTOM([#include ]) ``` -------------------------------- ### Example conf.h.in Template Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Header-Templates.html A template file for conf.h using #undef as a hook for HAVE_UNISTD_H. ```c /* Define as 1 if you have unistd.h. */ #undef HAVE_UNISTD_H ``` -------------------------------- ### Preferred: Quoted AC_CACHE_CHECK with aligned arguments Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Coding-Style.html Shows the recommended way to write AC_CACHE_CHECK with proper quoting and aligned arguments. This is the 'write' example that improves readability and follows the style guide. ```m4 AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2], [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])], [ac_cv_emxos2=yes], [ac_cv_emxos2=no])]) ``` -------------------------------- ### basename: Safe to use with -- option Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Limitations-of-Usual-Tools.html Long ago some hosts lacked a working basename, but nowadays it is safe to use. The example shows using basename with -- to handle filenames starting with a dash. ```shell base=`basename -- "$file"` ``` -------------------------------- ### Example: Leading underscore in macro name fails on ancient make Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Special-Chars-in-Names.html Shows a Makefile with macro names starting with an underscore, which fails on some ancient make implementations (e.g., NEWS-OS 4.2R) with the error 'Make: Must be a separator on rules line 2. Stop.' ```shell $ `cat Makefile` _am_include = # _am_quote = all:; @echo this is test $ `make` Make: Must be a separator on rules line 2. Stop. ``` ```shell $ `cat Makefile2` am_include = # am_quote = all:; @echo this is test $ `make -f Makefile2` this is test ``` -------------------------------- ### Sample config.site for changing defaults Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Site-Defaults.html A sample site file read by configure when invoked with --prefix=/usr/share/local/gnu. It changes default values for prefix, exec_prefix, sharedstatedir, localstatedir, and runstatedir, and sets a shared cache file and CC compiler when the cache file is /dev/null. ```shell # /usr/share/local/gnu/share/config.site for configure # # Change some defaults. test "$prefix" = NONE && prefix=/usr/share/local/gnu test "$exec_prefix" = NONE && exec_prefix=/usr/local/gnu test "$sharedstatedir" = '${prefix}/com' && sharedstatedir=/var test "$localstatedir" = '${prefix}/var' && localstatedir=/var test "$runstatedir" = '${localstatedir}/run' && runstatedir=/run # Give Autoconf 2.x generated configure scripts a shared default # cache file for feature test results, architecture-specific. if test "$cache_file" = /dev/null; then cache_file="$prefix/var/config.cache" # A cache file is only valid for one C compiler. CC=gcc fi ``` -------------------------------- ### m4_list_cmp examples Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Number-processing-Macros.html Examples of comparing M4 lists of arithmetic expressions with m4_list_cmp. Each example shows the expansion result after the ⇒ symbol. ```m4 m4_list_cmp([1, 0], [1]) ⇒0 m4_list_cmp([1, [1 * 0]], [1, 0]) ⇒0 m4_list_cmp([1, 2], [1, 0]) ⇒1 m4_list_cmp([1, [1+1], 3],[1, 2]) ⇒1 m4_list_cmp([1, 2, -3], [1, 2]) ⇒-1 m4_list_cmp([1, 0], [1, 2]) ⇒-1 m4_list_cmp([1], [1, 2]) ⇒-1 ``` -------------------------------- ### Equivalent command for autom4te -l m4sugar Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/autom4te-Invocation.html Shows that 'autom4te -l m4sugar foo.m4' is equivalent to using --prepend-include with the default installation path /usr/local. ```shell autom4te --prepend-include /usr/local/share/autoconf \ m4sugar/m4sugar.m4f foo.m4 ``` -------------------------------- ### Failed installation command Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Automatic-Rule-Rewriting.html The specific command that fails during installation. ```shell install -m 644 \ `test -f ../pkg/src/f.h || \ echo ../pkg/src/ \ `../pkg/src/f.h \ /usr/local/include/../pkg/src/f.h; ``` -------------------------------- ### Preferred: test with $[@%:@] Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Coding-Style.html Shows the recommended test command using $[@%:@] to avoid editor issues. This is the 'use' example. ```shell test $[@%:@] != 0 ``` -------------------------------- ### Default configure --help output format Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Help-Formatting.html Shows the default help output format, where options are grouped into sections such as 'Optional Features' and 'Optional Packages'. The ellipsis (...) indicates omitted options. ```text Optional Features: … --enable-bar include bar Optional Packages: … --with-foo use foo ``` -------------------------------- ### AC_OBSOLETE usage example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Obsolete-Macros.html Example of calling AC_OBSOLETE with a suggestion for replacement. ```autoconf AC_OBSOLETE([$0], [; use AC_CHECK_HEADERS(unistd.h) instead])dnl ``` -------------------------------- ### Configure libdir for 64-bit platforms in config.site Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Site-Defaults.html This snippet shows the content for /usr/local/share/config.site to ensure libraries are installed in lib64 instead of lib on platforms that prefer it. ```shell # /usr/local/share/config.site for platforms that prefer # the directory /usr/local/lib64 over /usr/local/lib. test "$libdir" = '${exec_prefix}/lib' && libdir='${exec_prefix}/lib64' ``` -------------------------------- ### Reduced installation command Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Automatic-Rule-Rewriting.html The reduced command that installs the file in an incorrect directory. ```shell install -m 644 \ ../pkg/src/f.h \ /usr/local/include/../pkg/src/f.h; ``` -------------------------------- ### AC_CHECK_SIZEOF example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Generic-Compiler-Characteristics.html Example usage of the AC_CHECK_SIZEOF macro to determine the size of a pointer type. ```m4 AC_CHECK_SIZEOF([int *]) ``` -------------------------------- ### Configure with compiler and linker options Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Compilers-and-Options.html Run the configure script with CC, CFLAGS, and LIBS variables set on the command line to specify the compiler, compiler flags, and linker libraries. This is useful when the system requires unusual options not known to configure. See 'Defining Variables' and 'Preset Output Variables' for more details. ```shell ./configure CC=gcc CFLAGS=-g LIBS=-lposix ``` -------------------------------- ### Unquoted AC_CHECK_HEADERS example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Changed-Quotation.html An example of an improperly quoted AC_MSG_ERROR invocation within AC_CHECK_HEADERS. ```m4 AC_INIT AC_CHECK_HEADERS(foo.h, , AC_MSG_ERROR(cannot find foo.h, bailing out)) AC_OUTPUT ``` -------------------------------- ### AC_CONFIG_COMMANDS with variable initialization Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Configuration-Commands.html An example showing how to use AC_CONFIG_COMMANDS to execute commands with variable initialization. ```m4 fubar=42 AC_CONFIG_COMMANDS([fubar], [echo this is extra $fubar, and so on.], [fubar=$fubar]) ``` -------------------------------- ### AC_MSG_NOTICE usage example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Printing-Messages.html Example of using AC_MSG_NOTICE to print a general description of a feature check. ```m4 AC_MSG_NOTICE([checking if stack overflow is detectable]) ``` -------------------------------- ### Tru64 make dependency simplification example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Automatic-Rule-Rewriting.html A shell session showing a Makefile with VPATH and a dependency ../foo. Tru64 make simplifies ../foo to foo, demonstrating its automatic rule rewriting behavior. The sub/ directory does not exist, indicating simplification occurs before checking for the file. ```shell $ `cat Makefile` VPATH = sub all: ../foo echo ../foo $ `ls` Makefile foo $ `make` echo foo foo ``` -------------------------------- ### Corrected quoted AC_CHECK_HEADERS example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Changed-Quotation.html The corrected version of the previous example with proper quotation for arguments. ```m4 AC_INIT([Example], [1.0], [bug-example@example.org]) AC_CHECK_HEADERS([foo.h], [], [AC_MSG_ERROR([cannot find foo.h, bailing out])]) AC_OUTPUT ``` -------------------------------- ### config.status with a tag Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/config_002estatus-Invocation.html Shows how to specify a tag (file) to configure. The file must be given without its dependencies. ```shell ./config.status foobar ``` -------------------------------- ### C const keyword example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/C-Compiler.html An example of a const declaration that is valid in C but invalid in C++. ```c const int foo; ``` -------------------------------- ### Demonstrate Tru64 make automatic directory creation Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Tru64-Directory-Magic.html This sequence shows how Tru64 make creates the directory structure 'foo/bar' in the current build directory when VPATH points to the parent directory. ```bash $ `mkdir -p foo/bar build` $ `cd build` $ `cat >Makefile < # include #endif #ifdef _POSIX_VERSION /* Code for Posix systems. */ #endif ``` -------------------------------- ### Non-portable VPATH rule example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Automatic-Rule-Rewriting.html An example of a rule that is not portable to GNU make because of VPATH rewriting behavior. ```makefile # This isn't portable to GNU make. VPATH = ../pkg/src f.c: if.c cp if.c f.c ``` -------------------------------- ### Reusing configuration with --config Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/config_002estatus-Invocation.html Shows how to use the --config option to retrieve configuration settings and apply them to a new configure invocation, useful for debugging builds in a different build directory. ```shell args=`build-dir/config.status --config` eval src-dir/configure "$args" CFLAGS=-g --srcdir=src-dir ``` -------------------------------- ### Expected output of AC_PREPROC_IFELSE example Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Running-the-Preprocessor.html The configure output that the AC_PREPROC_IFELSE example might produce, ending with 'OK' when the preprocessor succeeds. ```text checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether the compiler supports GNU C... yes checking whether gcc accepts -g... yes checking for gcc option to enable C11 features... -std=gnu11 checking how to run the C preprocessor... gcc -std=gnu11 -E OK ``` -------------------------------- ### AC_HEADER_RESOLV: Prerequisites for resolv.h Source: https://www.gnu.org/software/autoconf/manual/autoconf-2.72/html_node/Particular-Headers.html Use this code after running AC_HEADER_RESOLV to include the necessary headers before including resolv.h. It conditionally includes sys/types.h, netinet/in.h, arpa/nameser.h, and netdb.h based on their respective HAVE_ macros. ```c #ifdef HAVE_SYS_TYPES_H # include #endif #ifdef HAVE_NETINET_IN_H # include /* inet_ functions / structs */ #endif #ifdef HAVE_ARPA_NAMESER_H # include /* DNS HEADER struct */ #endif #ifdef HAVE_NETDB_H # include #endif #include ```