Autoconf from zero to libreadline

Hi all. Let’s say we want to use Autoconf, and that we already know how to program something in C/C++… Here is how I do it, in what probably will become a series. I also want to write an article on how to use autoconf to generate real win64 executables, and how to make an installer for those executables, all from the comfort of your Linux terminal. Stay tuned..

Goals

  1. How to start using Autoconf in a C++ program.
  2. Write configure.ac and Makefile.am
  3. Generate ./configure
  4. Make the tarball.
  5. Including a dependency: libreadline.

Let’s make the simplest possible C++ program like this:

#include <iostream>
int main(int argc, char* argv[]) {
  std::cout << "Hello, world!\n";
  return 0;
}

We stuff it in a file called ‘hello.cc’ and compile it like this:

g++ -o hello hello.cc

How to start using Autoconf in a C++ program

Autoconf is a bunch of packages, which are all named slightly differently under the different distributions.

dnf -y install autoconf automake libtool # fedora
apt-get -y install autoconf automake libtool # debian

We use basically use one autoconf command thoughout: autoreconf.

Write configure.ac and Makefile.am

Let’s make the simplest possible Autoconf configuration: one file ‘configure.ac‘ for the configuration of autoconf for this project, and ‘Makefile.am‘ to generate our ‘Makefile‘.

configure.in:

AC_INIT([hello], [1.0.0], [bert@stanzabird.nl], [], [])
AC_CONFIG_SRCDIR(hello.cc)
AC_PREREQ(2.59)
AM_INIT_AUTOMAKE
AC_CONFIG_HEADERS(config.h)
AC_PROG_CXX

AC_CONFIG_FILES(Makefile)
AC_OUTPUT

And the Makefile.am:

AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = hello.cc

That’s it when it comes to files we need to create and understand. Now would be a great time to create your git repository: git init ; git add * ; git commit -am initial

We create the autoconf cruft files by issuing:

autoreconf -if

Where -if tells autoreconf to regenerate all Makefile and cruft files from scratch. To make the Makefile, and build the project, issue:

./configure --prefix=$HOME/usr # this is where my progs live
make
make dist # create the hello-1.0.0.tar.gz tarball distfile

Including a dependency: libreadline

You could clone libreadline from it’s GNU repository, but everybody uses the easy way with the package manager:

dnf -y install readline-devel # fedora
apt-get -y install libreadline-dev # debian

at this point, you should be able to do:

g++ -o hello hello.cc -lreadline

Now to add this to Autoconf, we need to add lreadline to the Makefile.am, like this:

AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = hello.cc
dockerdeps_LDADD = -lreadline

And a C/C++ program using libreadline could be something like this:

#include <iostream>

// https://www.man7.org/linux/man-pages/man3/readline.3.html
#include <stdio.h>
#include <readline/readline.h>

int main(int argc, char* argv[]) {
  auto name = readline("Enter your name: ");
  std::cout << "Hello, \"" << name << "\".\n";
  return 0;
}

Leave a Comment

Your email address will not be published. Required fields are marked *