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 in Linux with the command , 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 with subfolder which has our file.

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

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 . 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

Now we run the command. aclocal command in Linux is used to automatically generate aclocal.m4 files from 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 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

The option 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 command and the command which generated for us we can now run our script to get our final script. use command

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

Testing the program

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

As you see i created a folder and moved 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

is used for prepare(setup) environment for building. : Building the system. : Install to the system. Lets now type and run the program 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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store