Building of a library by using of
configure, automake, autoconf and other autotools

Linux libraries are the files with the extension «.so» and the extension «.a». Files with the extension «.so» are dynamic libraries. Files with the extension «.a» are static libraries. Both libraries' types can be created by using of autotools.

Here is an example of a creating of a small library by using of autotools:

1. Let's create a directory for the library test.

2. Let's create the directory lib in the directory test.

3. Let's create the file lib_file1.c in the lib

/* lib/lib_file1.c */

#include <stdio.h>
#include "lib_header1.h"

void receive_byte () {
   printf ("Hello, receive_byte\n");
}

4. Let's create the file lib_file2.c in lib

/* lib/lib_file2.c */

#include <stdio.h>
#include "lib_header1.h"

void transmit_byte () {
   printf ("Bye, transmit_byte\n");
}

5. Let's create the file lib_header1.h in lib

/* lib/lib_header1.h */

void receive_byte (void);
void transmit_byte (void);

For building of a static or dynamic library one can write Makefile manually, it would be better to use autotools, but it would be better to use autotools which will generate it automatically.

For automatic creating of Makefile one needs two templates only - Makefile.am and configure.in. The first one should be written manually, the second will be generated automatically and should be edited manually.

In the root directory of the project (in the directory test) let's create the file named Makefile.am with following content:

# Makefile.am

ACLOCAL_AMFLAGS = -I m4
SUBDIRS = lib

The keyword SUBDIRS means that the source files for building are in this directory.

The file named Makefile.am should be also created in the directory lib with the following content:

#lib/Makefile.am

lib_LTLIBRARIES	= libhw.la
libhw_la_SOURCES	= lib_file1.c lib_file2.c lib_header1.h

If it is required to connect an additional library, it is necessary to add the next line:

libhw_la_LIBADD	= -lm 

That is all.

Let's create configure.in by using of the utility autoscan. This program creates the file configure.scan, which is the prototype of configure.in.

Let's run the following:

autoscan

Let's edit the configure.scan and then rename it into configure.in:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT([libhw], [1.0], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([config.h.in])
AC_CONFIG_HEADERS([config.h])

AM_INIT_AUTOMAKE
AC_PROG_LIBTOOL
AC_CONFIG_MACRO_DIR([m4])

# Checks for programs.
AC_PROG_CXX
AC_PROG_AWK
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile
                 lib/Makefile])
AC_OUTPUT

THAT IS ALL! Now let's run the following sequence and Makefile is ready.

aclocal - creates aclocal.m4.

libtoolize — these are the standard feature for adding support libtool for libraries' building.

autoconf - the utility creates the script configure in accordance with macros which are listed in the file `configure.in'.

autoheader — creates the header file (config.h.in).

touch NEWS README AUTHORS ChangeLog (these files are needed for automake)

automake --add-missing - the program creates the files "Makefile.in" based on the files "Makefile.am".

configure — creates Makefile

make — compiles the library