====== Autotools by example ====== There are lots of tutorials and manuals on [[http://www.gnu.org/software/autoconf/|autoconf]]/[[http://www.gnu.org/software/automake/|automake]]/ [[http://www.gnu.org/software/libtool/|libtool]] outhere that explain the basic usage. However, few articles provide examples and discuss more advanced topics. This document provides some collected examples that show the different features of the "autotools" build system. ===== Adding options in autoconf ===== The following lines add the --enable-goodbye option to configure This gives the user the choice to enter one of the following options: * --enable-goodbye * --enable-goodbye=yes * --enable-goodbye=no The following should go in configure.ac AC_MSG_CHECKING([whether we are enabling goodbye]) AC_ARG_ENABLE(goodbye, AC_HELP_STRING([--enable-goodbye], [Say goodbye as well]), [if test "${enable_goodbye}" = "yes" ; then AC_DEFINE(_WITH_GOODBYE, 1, Say goodbye as well) AC_MSG_RESULT([yes]) else AC_DEFINE(_WITH_GOODBYE, 0, Say goodbye as well) AC_MSG_RESULT([no]) fi], # Default value for configure AC_MSG_RESULT([no]) ) Assuming your project is using autotools correctly, the above addition in your configure.ac will allow you to test if the user has chosen to enable the "goodbye" option. For that you can test the value of the variable _WITH_GOODBYE as follows : #ifdef HAVE_CONFIG_H #include #endif #include #include int main() { fprintf (stdout, "Hello World!\n"); #ifdef _WITH_GOODBYE sleep (1); fprintf (stdout, "Goodbye Cruel World!\n"); #endif return (0); } ===== Check for IPv6 support ===== This code in your configure.ac will define the variable IPV6 if your system has everything for compiling and running IPv6 enabled applications. AC_MSG_CHECKING(for IPv6 support) AC_CACHE_VAL(ac_cv_ipv6, AC_TRY_RUN([ #include #include #include /* Make sure the definitions for AF_INET6 and struct sockaddr_in6 * are defined, and that we can actually create an IPv6 TCP socket. */ main() { int fd; struct sockaddr_in6 foo; fd = socket(AF_INET6, SOCK_STREAM, 0); exit(fd >= 0 ? 0 : 1); }], ac_cv_ipv6=yes, ac_cv_ipv6=no, ac_cv_ipv6=no)) AC_MSG_RESULT($ac_cv_ipv6) if test $ac_cv_ipv6 = yes ; then AC_DEFINE(IPV6,,"check if system supports IPV6") fi ===== Checking libraries ===== The following will cause the configure script to fail if the function //connect// is not defined in the library //libc// AC_CHECK_LIB(c, connect,[],[ echo "Socket library is required" exit -1 ]) The following will cause the configure script to fail if the function //connect// is not defined in the library //libc// nor in the library //socket// AC_CHECK_LIB(c, connect, [],[ AC_CHECK_LIB(socket, connect,[],[ echo "Socket library is required" exit -1 ]])) ===== Check for headers ===== The following in the configure.ac will cause the configure script to fail if any of the required headers is not present in the system. AC_CHECK_HEADERS([arpa/inet.h fcntl.h limits.h \ netdb.h netinet/in.h stddef.h \ stdlib.h string.h sys/file.h \ sys/ioctl.h sys/param.h sys/socket.h \ sys/time.h termios.h unistd.h], [],[ echo "missing header" exit -1]) ===== External links ===== * http://wiki.linuxfromscratch.org/hints/browser/trunk/autotools.txt * http://sources.redhat.com/autobook/autobook/autobook_toc.html * http://www.gnu.org/software/autoconf * http://www.gnu.org/software/automake * http://www.gnu.org/software/libtool \\ {{tag>coding howto unix}} {{tag>}}