Use Autotools to Manage Project
This guide will show you how to use Autotools to create makefiles and manage your project.
Problem
When developing C or C++(CXX) projects, often times it is necessary to worry about how the program is built, how to support the same code base for different platforms and OS, how to make sure all the requirements and dependencies are met, and how everything is installed.
Background
Compilers like GCC, G++, clang are used to compile C and C++ code. However, when multiple files presents in a project, it can be troublesome to type in all of the files and flags for each and every files. With the help of Make, developers can code up how project should be compiled, enabling more stable reproducibility. Most personal project should only requires this level of scripting to manage the entire code base. If on the other hand, you have more requirements like what I mentioned in the Problem section, tools like Autotools and CMake can greatly help with managing complex project and achieve even more stable reproducibility.
Solution
Step 1: Install Requirements
Install git, make, libtool, and automake.
If newer versions are needed, you can install them from source.
Step 2: Create Project
Create a new project directory like the following. It is recommended to use git to perform version control along side with the project.
.
├── .gitignore
├── Makefile.am
├── configure.ac
├── m4
│ └── .keepdir
├── readme.md
└── src
├── Makefile.am
├── lib.c
├── lib.h
└── main.c
2 directories, 9 files
One configure.ac
and Makefile.am
file is required at the root of the project. The ./configure.ac
file is responsible for finding required dependencies and generating ./configure
script, which generate makefiles for this project. ./Makefile.am
file is responsible for setting up project structure, sort of like Makefile
. Each subdirectory containing source code requires coreesponding Makefile.am like ./src/Makefile.am
. ./m4
directory is responsible to handle generated intermediate auxiliary files.
Step 3: Write configure.ac
Following is a sample of configure.ac
file. You can modify it to fit your project.
Line 2
: Set basic information for the project.Line 3
: Set auxiliary directory to hold generated files.Line 4
: Set automake flags. This is not GCC flags.Line 5
: Set source directory. Can have multiple source directories.Line 7
: Set macro holding directory.Line 12
: Set libtool settings.Line 15
: Check for libraries. Not used in this example.Line 17
: Check for header files.Line 20-28
: Check for typedefs, structures, and compiler characteristics.Line 31-33
: Check for library functions.Line 35-36
: Write out all of theMakefile.am
files.
Step 4: Write Makefile.am
Line 1
: Set subdirectories to look into.Line 4
: Set extra flags for aclocal.Line 6-36
: Clean up all the generated files. This extends themake clean
command. Really helpful when you want to clean up everything but the source code.
Step 5: Write ./src/Makefile.am
Line 1-3
: Set variables for later use.Line 15-18
: Set install directories for binaries, libraries, headers, and plugins.Line 24
: Create a library to build namedlib
. If you have multiple libraries, you can add them likeLine 25
.Line 38-43
: Add sources, headers, and compiling flags for the library. Their names should be prefixed with the library name.Line 48
: Create an executable to build namedmain
. If you have multiple executables, you can add them likeLine 49
.Line 55-57
: Add sources, compiling flags, and libraries to link for the executable. Their names should be prefixed with the executable name.
Step 6: Build Project
Execute the following commands to build the project.
Line 1
can actually be split into multiple steps, but for simplicity, this command is used instead. Also, if you prefer to do with multiple steps, people usually code them into a script ./bootstrap.sh
and execute it instead.
The world of autotools can be a lot more complex than this. However, the following example should give you a good start.
- The entire project to start with can be found in https://github.com/belongtothenight/autotools_init_setup/tree/main.
- More complex project based on this can be found in https://github.com/belongtothenight/ACN_Code/tree/main/hw5_c_trace_analyze.
Open-sourced and mature projects managed by autotools:
- https://github.com/LibtraceTeam/wandio
- https://github.com/LibtraceTeam/libtrace
- https://github.com/kgoldman/ibmtss
- https://github.com/tpm2-software/tpm2-tss
Reference
- How to use autotools (automake, autoconf, aclocal, autoheader) by Daniel Persson
- Introduction to the Autotools, part 1 by David A. Wheeler
Error Correction
If you find any mistakes in the document, please create an Issue or a Pull request or leave a message in Discussions or send me a mail directly with the mail icon at the bottom right. Thank you!