How to package your software in Linux using GNU Autotools

A better way to deliver your code.

What is AutoTools?

Autotools is a quick and easy way to manage and package source code so users can compile and install software. It also makes a source code package portable to a range of systems, including UNIX and others. Autotools is also known as the GNU Build System.

An easy way to tell if the project uses Autotools is to look for the presence of its primary input files:

Autotools configuration

A basic overview of how the main autotools components fit together.[1]

First, make sure that you have installed Autoconf in Linux with the command sudo apt-get install autoconf, let us create a file called configure.ac in our project’s root directory to configure our project, the project will be a simple C program that prints out hello world. In the image below i created a folder called Autotools with subfolder src which has our hello.c file.

Now in the parent folder Autotools we create our configuration file by using the nano command nano configure.ac

So first we initialize our project in AC_INIT with name and version and email. AC stands for Autoconfig and AM stands for AutoMake. The macro AM_INIT_AUTOMAKE. tell us that we are going to use Automake for this project. I’m building the program in C so need to type AC_PROG_CC, so it add for us the gcc compiler for the C language. I also added CXX for the C++ program. AC_CONFIG_FILES is one of the configuration files that we need and we also want to package it into our build and in the distribution. AC_OUTPUT this will output all the files.

The next file we need is Makefile.am . create it with nano. It will looks like this.

AUTOMAKE_OPTIONS=foreign this line tells about the layout of the project since I’m not following the actual standard layout of the GNU project so I’m using a foreign, you can use GNU if you are actually calling the complete format. We have to provide what kind of binary we want to build also we tell it what required to build this program by typing the sources, in our case we have only hello.c

Now we run the aclocal command. aclocal command in Linux is used to automatically generate aclocal.m4 files from configure.in file. automake in Linux contains a lot of autoconf macros that can be used in the different packages. These macros must be defined in the aclocal.m4. If not, then it can’t be accessed by the autoconf[2], and now if i run autoconf command, it will generate for us our configure script.

For better practice use clean-local: in Makefile.am for cleaning/removing things that will be generated by some different commands, and that’s good when committing these to a git repo. it will looks like this

clean-local:
@rm config.status configure config.log
@rm -r autom4te.cache/
@rm aclocal.m4

Now we can run the next command called automake --add-missing

The —-add-missingoption copies some boilerplate files from Automake installation into the current directory. If we now type ls

We can see that Makefile.am has generated Makefile.in If we look again to this figure which i borrowed from https://devmanual.gentoo.org/

A basic overview of how the main autotools components fit together.[1]

Now we did the autoconf command and the automake command which generated for us Makefile.in, we can now run our configure script to get our final Makefile script. use command /.configure

Type ls and you will see our final Makefile . It's a huge file, you can view it with less command. Now we can create our tarball file by using the command make dist . then type ls and you will see our final tar.gz file which can be now delivered to any environment :)

Testing the program

Let's move our tar.gz to another folder and test it.

As you see i created a tmp folder and moved myprogram-1.0.tar.gz to this folder. it time now to unzip it and run the program.

As you see it conaints all the source files. To install the open-source package we need to type the following commands

./configure && make && sudo make install

./configure is used for prepare(setup) environment for building. make: Building the system. sudo make install : Install to the system. Lets now type ls and run the program ./myProgram and see the magic :)

That’s it folks ;) well done for reaching the end.

— Maher;

References

[1] https://devmanual.gentoo.org/general-concepts/autotools/index.html

[2] GeeksForGeeks aclocal command in Linux with Examples