In this article, I explain how to use autotools, also known and the GNU build system, to create portable shared libraries that can be easily built and used in various unix/linux systems.
The first we need is to have the functions exported by the library implemented. We need to implement a .c file and a .h file. Lets assume our library offers list manipulation functions. We would then implement list manipulation functions in list.c and define the function prototypes in test.h
Apart from these source files, we need to prepare a couple of other files. These files are as follows :
This file will be converted by autotools to Makefile.am which will be later converted to a Makefile by the configure script for compiling and building the library.
This is the content of our Makefile.am
# General settings lib_LTLIBRARIES = liblist.la liblhs_la_SOURCES = list.c test.c liblhs_la_LDFLAGS = -version-info 1:0:0 include_HEADERS = list.h # Specify some flags to be passed to the compiler AM_CFLAGS = -I/some/include/path -L/some/lib/path # Specify distributable binaries bin_PROGRAMS = test test_SOURCES = test.c # Name of the static library LDADDBASE = liblist.la # Specify the libraries to compile our library and the accompanying binaries (change these according to your needs) LDADD = $(LDADDBASE) -lcrypto -lcrypt -lkrb5 -lroken -lasn1
If our library had more source files, the name of these files must be added to the liblhs_la_SOURCES and include_HEADERS variables accordingly.
This file will generate the configure script that checks for system resources and prepares for the compilation and building of our library. The configure script
The contents of this file are pretty generic. You just need to customized it by changing in the first section “General stuff”. You may also add checks for particular programs or other dependencies. In our example, we check for the library libmm (memory mapping).
#General stuff AC_INIT(liblist, 1.0, yourname@yourdomain.com) AM_INIT_AUTOMAKE(liblist, 1.0) AC_CONFIG_SRCDIR([list.c]) AC_CONFIG_HEADER([config.h]) # Checks for programs. AC_PROG_CXX AC_PROG_CC AC_PROG_CPP AC_PROG_INSTALL AC_PROG_LN_S AC_PROG_MAKE_SET AC_PROG_LIBTOOL # Checks for header files. AC_HEADER_STDC AC_HEADER_SYS_WAIT AC_CHECK_HEADERS([fcntl.h limits.h memory.h stdlib.h string.h strings.h sys/file.h sys/param.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_C_CONST AC_TYPE_UID_T AC_TYPE_MODE_T AC_TYPE_OFF_T AC_TYPE_PID_T AC_TYPE_SIZE_T # Checks for library functions. AC_FUNC_CHOWN AC_FUNC_FORK AC_FUNC_MALLOC AC_FUNC_MMAP AC_CHECK_FUNCS([ftruncate getpagesize memset munmap strerror]) # Check if libmm is installed and whether the MM_malloc function is defined AC_CHECK_LIB([mm], [MM_malloc]) AC_OUTPUT([Makefile])
For more information on configure.ac, check my previous article Autotools by example
Now that we have prepared the Makefile.am and the configure.ac files, we need to run autotools to build the Makefile.in file and the configure script.
This script does this job and adds other additional files.
#!/bin/sh # Function to test and add generic files function Check_And_Add { if ! test -f $1 then echo Creating New file $1 touch $1 fi } # Generate aclocal.m4 macros from configure.in # these M4 macros will be used by autoconf to # create the configure script aclocal # Generate the configure script autoconf Check_And_Add INSTALL Check_And_Add COPYING Check_And_Add NEWS Check_And_Add README Check_And_Add AUTHORS Check_And_Add ChangeLog # Creates a config.h.in, useful when we want the code to depend on # information provided by configure autoheader # Create Makefile.in from Makefile.am automake -a -c -i --gnu
Now, we can configure and compile the library.
$./configure $make
Once we checked that the library is compiling fine, we can create a .tar.gz for distribution.
$make dist
| Labels: coding, howto, unix |
|