stevesearle.com
Home
Champions
Techie Stuff
FAQ
Automake and Autoconf
MySQL and C++
CentOS 5.0 Server
Fedora 7 Workstation
Customise that Desktop
Blocking Online Ads
Downloads
Spam

Viewable With Any Browser

Valid HTML 4.01!
© Steve Searle 1999, 2011
Created and maintained using
Vim
Techie Stuff - Automake and Autoconf with C++

Creating and maintaining a C++ development is much easier if you use Automake and Autoconf, especially if you want to work to GNU standards. Much of the following information came from Openismus's Using Automake and Autoconf with C++.

The following shows how to set up the directories and files for a C++ program, hello. Assume this consists of the following source files:

Create a project directory, hello-0.0.1. The version number (0.0.1) can be in whatever format you prefer. Inside this creat a src directory.

 $ mkdir hello-0.0.1
 $ cd hello-0.0.1
 $ mkdir src

In the hello-0.0.1 directory, create configure.ac as follows.

 AC_INIT(hello, 0.0.1)
 AC_CONFIG_SRCDIR(src/hello.cpp)
 AM_INIT_AUTOMAKE(gnu 0.0.1 no-define dist-bzip2)
 AC_CONFIG_HEADERS(config.h)
 AC_PROG_CC
 AC_PROG_CXX
 AC_PROG_INSTALL
 AC_OUTPUT(Makefile src/Makefile)

The AC_INIT line contains the program name and version number. AC_CONFIG_SRCDIR is the module containing the main function. In AM_INIT_AUTOMAKE the version number is given again, and dist-bzip2 causes a tar.bz2 file to be created when the make dist command is given.

Also create Makefile.am, to show where the source can be found.

 SUBDIRS = src

A Makefile.am is also needed in the src directory.

 bin_PROGRAMS = hello
 hello_SOURCES = hello.cpp world.h world.cpp

The executable file name is given by bin_Programs and ..._SOURCES is the names of all the sources that make up the program. Note that this has to be prefixed by the project name.

In the hello-0.0.1 directory, run the following commands.

 $ autoheader
 $ touch NEWS README AUTHORS ChangeLog
 $ aclocal
 $ autoconf
 $ automake --add-missing

Once you have created the sourc files, you should have the following directories and files.

 $ ls -l hello-0.0.1
 total 228
 -rw-rw-r--  1 steve steve  38499 May  8 16:04 aclocal.m4
 -rw-rw-r--  1 steve steve      0 May  8 16:04 AUTHORS
 drwxr-xr-x  2 steve steve   4096 May  8 16:04 autom4te.cache
 -rw-rw-r--  1 steve steve      0 May  8 16:04 ChangeLog
 -rw-rw-r--  1 steve steve    471 May  8 16:03 config.h.in
 -rwxrwxr-x  1 steve steve 147240 May  8 16:04 configure
 -rw-rw-r--  1 steve steve    203 May  8 16:02 configure.ac
 lrwxrwxrwx  1 steve steve     31 May  8 16:04 COPYING -> /usr/share/automake-1.9/COPYING
 lrwxrwxrwx  1 steve steve     31 May  8 16:04 depcomp -> /usr/share/automake-1.9/depcomp
 lrwxrwxrwx  1 steve steve     31 May  8 16:04 INSTALL -> /usr/share/automake-1.9/INSTALL
 lrwxrwxrwx  1 steve steve     34 May  8 16:04 install-sh -> /usr/share/automake-1.9/install-sh
 -rw-rw-r--  1 steve steve     14 May  8 16:03 Makefile.am
 -rw-rw-r--  1 steve steve  18106 May  8 16:05 Makefile.in
 lrwxrwxrwx  1 steve steve     31 May  8 16:04 missing -> /usr/share/automake-1.9/missing
 -rw-rw-r--  1 steve steve      0 May  8 16:04 NEWS
 -rw-rw-r--  1 steve steve      0 May  8 16:04 README
 drwxrwxr-x  2 steve steve   4096 May  8 16:10 src
 $ ls -l hello-0.0.1/src
 total 32
 -rw-rw-r--  1 steve steve   172 May  8 16:08 hello.cpp
 -rw-rw-r--  1 steve steve    65 May  8 16:03 Makefile.am
 -rw-rw-r--  1 steve steve 12675 May  8 16:05 Makefile.in
 -rw-rw-r--  1 steve steve   515 May  8 16:10 world.cpp
 -rw-rw-r--  1 steve steve   316 May  8 16:10 world.h

You can now develop the program with ./configure and make. As root, make install will install it. You can also use make clean to remove tempory files, and make dist to create tar.gz and tar.bz2 distirbution files.

If you want to set CPPFLAGS or LDFLags this is done in src/Makefile.am. For instance, if you want to use the MySQL libraries, this file contains the following.

 bin_PROGRAMS = hello
 hello_SOURCES = hello.cpp world.h world.cpp
 AM_CPPFLAGS = -I/usr/include/mysql
 AM_LDFLAGS = -L/usr/lib/mysql -lmysqlclient

By default this will set the file up under the GNU General Public Licence, but you can release it under a different licence if you choose to do so. To change the version number in the future, change the appropriate parts of configure.ac, and if you add new modules, these should be included in src/Makefile.am.