Linux autoconf Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the purpose and usage of the autoconf command in Linux. autoconf is a tool used to generate shell scripts that can automatically configure software source code packages to adapt to many kinds of Unix-like systems. We will start by installing the autoconf package, then create a simple C program and use autoconf to generate the necessary configuration files. Finally, we will customize the autoconf configuration files for more complex projects.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/PackagesandSoftwaresGroup -.-> linux/software("`Linux Software`") subgraph Lab Skills linux/software -.-> lab-422562{{"`Linux autoconf Command with Practical Examples`"}} end

Understand the Purpose and Usage of autoconf Command

In this step, we will explore the purpose and usage of the autoconf command in Linux. autoconf is a tool used to generate shell scripts that can automatically configure software source code packages to adapt to many kinds of Unix-like systems.

First, let's install the autoconf package:

sudo apt-get update
sudo apt-get install -y autoconf

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  m4 perl
Suggested packages:
  autoconf-archive gnu-standards autoconf-doc
The following NEW packages will be installed:
  autoconf m4 perl
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,245 kB of archives.
After this operation, 5,138 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...

The autoconf command is used to generate configuration scripts (usually named configure) from template files (usually named configure.ac or configure.in). These configuration scripts are then used to configure the source code for compilation and installation on the target system.

Here's a simple example of how to use autoconf:

## Create a simple C program
cat > hello.c << EOF
#include <stdio.h>

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

## Create the configure.ac file
cat > configure.ac << EOF
AC_INIT([hello], [1.0], [[email protected]])
AC_PROG_CC
AC_OUTPUT([Makefile])
EOF

## Generate the configure script
autoconf

Now, let's run the generated configure script and build the program:

./configure
make

Example output:

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
configure: creating ./config.status
config.status: creating Makefile
make  all-am
make[1]: Entering directory '/home/labex/project'
gcc -g -O2 -o hello hello.c
make[1]: Leaving directory '/home/labex/project'

The autoconf command generates the configure script, which is then used to configure the source code for compilation and installation. The configure script detects the system's features and capabilities, and generates a Makefile that can be used to build the software.

Configure and Build a Simple C Program Using autoconf

In this step, we will use the autoconf command to configure and build a simple C program.

First, let's create a simple C program named hello.c:

#include <stdio.h>

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

Next, we'll create the configure.ac file, which is used by autoconf to generate the configure script:

cat > configure.ac << EOF
AC_INIT([hello], [1.0], [[email protected]])
AC_PROG_CC
AC_OUTPUT([Makefile])
EOF

Now, let's generate the configure script:

autoconf

Example output:

configure.ac:1: installing './install-sh'
configure.ac:1: installing './missing'

With the configure script generated, we can now run it to configure the project:

./configure

Example output:

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
configure: creating ./config.status
config.status: creating Makefile

Finally, we can build the program using make:

make

Example output:

make  all-am
make[1]: Entering directory '/home/labex/project'
gcc -g -O2 -o hello hello.c
make[1]: Leaving directory '/home/labex/project'

The hello executable is now built and ready to run.

Customize autoconf Configuration Files for Complex Projects

In this step, we will explore how to customize the autoconf configuration files for more complex projects.

While the previous steps covered a simple C program, real-world projects often have more complex requirements, such as supporting multiple platforms, checking for dependencies, and customizing the build process.

Let's start by creating a more complex project structure:

mkdir -p myproject/src
cat > myproject/src/main.c << EOF
#include <stdio.h>
#include "myheader.h"

int main() {
    printf("Hello, %s!\n", get_greeting());
    return 0;
}
EOF

cat > myproject/src/myheader.h << EOF
#ifndef MYHEADER_H
#define MYHEADER_H

char* get_greeting();

#endif

Now, we'll create the configure.ac file to handle this more complex project:

cd myproject
cat > configure.ac << EOF
AC_INIT([myproject], [1.0], [[email protected]])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AC_PROG_CC
AC_CHECK_HEADERS([stdlib.h])
AC_OUTPUT([Makefile src/Makefile])
EOF

The key changes in this configure.ac file are:

  • AC_CONFIG_SRCDIR([src/main.c]): Specifies the location of the main source file.
  • AC_CONFIG_HEADERS([config.h]): Generates a config.h header file that can be used by the project.
  • AC_CHECK_HEADERS([stdlib.h]): Checks for the existence of the stdlib.h header file.
  • AC_OUTPUT([Makefile src/Makefile]): Generates the Makefiles for the project.

Now, let's generate the configure script and build the project:

autoconf
./configure
make

Example output:

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for stdlib.h... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
make  all-recursive
make[1]: Entering directory '/home/labex/myproject'
Making all in src
make[2]: Entering directory '/home/labex/myproject/src'
gcc -DHAVE_CONFIG_H -I. -g -O2 -c main.c
gcc -g -O2 -o myproject main.o
make[2]: Leaving directory '/home/labex/myproject/src'
make[1]: Leaving directory '/home/labex/myproject'

The generated configure script now handles the more complex project structure, including the myheader.h header file and the config.h header file generated by autoconf.

Summary

In this lab, we first learned about the purpose and usage of the autoconf command in Linux, which is a tool used to generate shell scripts that can automatically configure software source code packages to adapt to various Unix-like systems. We then configured and built a simple C program using autoconf by creating a configure.ac file and generating the configure script. Finally, we explored how to customize the autoconf configuration files for more complex projects.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like