Linux automake Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will learn about the Automake tool, which is used to generate Makefiles for software projects. Automake is part of the GNU build system and is commonly used in open-source software development. We will start by installing the necessary packages for using Automake, then create a basic Automake project and generate a Makefile. Finally, we will explore customizing the Automake configuration to suit our project's needs.

The steps in this lab cover the introduction to Automake, creating a basic Automake project, and customizing the Automake configuration. The lab aims to provide a practical understanding of using Automake for managing software build processes.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/apt -.-> lab-422564{{"`Linux automake Command with Practical Examples`"}} linux/touch -.-> lab-422564{{"`Linux automake Command with Practical Examples`"}} end

Introduction to Automake

In this step, we will learn about the Automake tool, which is used to generate Makefiles for software projects. Automake is part of the GNU build system and is commonly used in open-source software development.

Automake is a tool that automatically generates Makefiles from a higher-level description file called Makefile.am. This file contains instructions for building, installing, and distributing the software. Automake takes care of the details of generating the Makefile, allowing developers to focus on the project structure and build process.

Let's start by installing the necessary packages for using Automake:

sudo apt-get update
sudo apt-get install -y automake

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Get:5 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [588 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1792 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [748 kB]
Fetched 3451 kB in 2s (2032 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
automake is already the newest version (1:1.16.5-1.1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Now that we have Automake installed, we can start creating a basic Automake project in the next step.

Creating a Basic Automake Project

In this step, we will create a basic Automake project and generate a Makefile.

First, let's create a new directory for our project and navigate to it:

mkdir ~/project/automate-demo
cd ~/project/automate-demo

Next, we need to create the initial Automake files. We'll start with the configure.ac file, which contains the high-level configuration for the project:

touch configure.ac

Open the configure.ac file in a text editor and add the following content:

AC_INIT([automate-demo], [1.0], [[email protected]])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_OUTPUT

This configuration sets the project name, version, and maintainer email address, initializes Automake, and specifies that we want to use the C compiler.

Now, let's create the Makefile.am file, which contains the instructions for building the project:

touch Makefile.am

Open the Makefile.am file and add the following content:

AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = main.c

This Makefile.am file specifies that we want to build a program called "hello" from the main.c source file.

The final step is to generate the Makefile from the Automake files:

autoreconf -i

This command will generate the Makefile and other necessary files for the project.

Example output:

autoreconf: Entering directory '.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize --copy --force
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'config'.
libtoolize: copying file 'config/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'config'.
autoreconf: running: /usr/bin/autoconf --force
autoreconf: running: /usr/bin/autoheader --force
autoreconf: running: automake --add-missing --copy --force-missing
autoreconf: Leaving directory '.'

Now, we have a basic Automake project set up, and we can move on to the next step to customize the configuration.

Customizing Automake Configuration

In this step, we will customize the Automake configuration to add more functionality to our project.

First, let's create a simple C program that we will build using Automake. Create a new file called main.c in the ~/project/automate-demo directory with the following content:

#include <stdio.h>

int main() {
    printf("Hello, Automake!\n");
    return 0;
}

Now, let's update the Makefile.am file to include the new main.c file and add a custom target:

cat << EOF > ~/project/automate-demo/Makefile.am
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = main.c
install-data-local:
    @echo "Custom install step"
EOF

The new install-data-local target will be executed during the make install step.

Next, let's update the configure.ac file to add a custom configuration option:

cat << EOF > ~/project/automate-demo/configure.ac
AC_INIT([automate-demo], [1.0], [[email protected]])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_ARG_ENABLE([debug],
              [AS_HELP_STRING([--enable-debug],
                             [enable debugging])],
              [debug=yes], [debug=no])
AC_MSG_CHECKING([whether to enable debugging])
AC_MSG_RESULT([$debug])
AM_CONDITIONAL([DEBUG], [test "$debug" = yes])
AC_OUTPUT
EOF

This configuration adds a --enable-debug option that can be used to enable debugging when building the project.

Now, let's generate the Makefile and build the project:

cd ~/project/automate-demo
autoreconf -i
./configure
make

Example output:

checking whether to enable debugging... no
make  all-am
make[1]: Entering directory '/home/labex/project/automate-demo'
gcc -DPACKAGE_NAME=\"automate-demo\" -DPACKAGE_TARNAME=\"automate-demo\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"automate-demo\ 1.0\" -DPACKAGE_BUGREPORT=\"[email protected]\" -DPACKAGE_URL=\"\" -DSTDC_HEADERS=1 -I. -g -O2 -MT hello-main.o -MD -MP -MF .deps/hello-main.Tpo -c -o hello-main.o main.c
mv -f .deps/hello-main.Tpo .deps/hello-main.Po
gcc -g -O2 -o hello hello-main.o
make[1]: Leaving directory '/home/labex/project/automate-demo'

We can now install the project:

sudo make install

Example output:

make[1]: Entering directory '/home/labex/project/automate-demo'
 /usr/bin/mkdir -p '/usr/local/bin'
 /usr/bin/install -c 'hello' '/usr/local/bin/hello'
Custom install step
make[1]: Leaving directory '/home/labex/project/automate-demo'

The custom install-data-local target has been executed during the installation process.

Summary

In this lab, we learned about the Automake tool, which is used to generate Makefiles for software projects. We started by installing the necessary packages for using Automake, then created a basic Automake project by generating the initial Automake files, including the configure.ac and Makefile.am files. We also learned how to customize the Automake configuration to suit our project's needs, such as adding source files, libraries, and installation targets.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like