Through the previous study, we can know that makefile has a complex syntax structure, and it is not very convenient to write and maintain, so we designed the autotools tool specially used to generate Makefile to reduce the burden of making makefile.
Autonomous software is mainly released in source code form and needs to be recompiled on a variety of systems. The source code package installation is divided into 3 steps, configure, make and make install. Many files are involved in the construction process, and the production is very complicated. Using the autotools tool to generate the Makefile file can greatly facilitate the production of the source code installation package.
1. How Autotools work
An autotool project requires at least a configure script named configure and a makefile template named Makefile.in. There is a Makefile.in file in each directory of the project. The Autotools project also uses other files. These files are not necessary, and some are generated automatically. If you look at the content of the relevant files, you will find that they are very responsible, but these files are generated by Autotools through easy-to-write template files.
In fact, Autotools is not needed to build the Autotools package. Configure is a shell script that runs on the most basic shell. It checks the user system to obtain each feature and writes the makefile through the template.
configure creates all files in each directory. This directory is called the creation directory. If you run it from source, you can use ./configure, and the same is true for creating directories.
configure accepts several options on the command line for installing files in different directories. Help can be obtained by executing the configure --help command, and here is a list of the most commonly used options.
--help: List all variable options. --help host: compile to make it run on another system (cross-compile) --prefix dir: select the installed project path in the root directory, the default is /usr/local
configure generates several additional files: config.log (a log file, for details about problems that arise), config.status (a shell script that gives you a real view of the current configuration), config.h (a header file, from the template config. h.in), but these files are not particularly important.
The makefie file generated by configure is more complex, and its definition is all standard targets required by the GNU standard. The commonly used targets are listed as follows:
make or make all: create program
make install: install program
make distclean: delete all files generated by configure.
Generate the relationship diagram between the files in the configure process
2. Autotools
Autotools consists of autoconf, automake, prel locale, and M4, among others. It contains five commands: aclocal (processing local macro definitions), autoscan (checking source files), autoconf (generating the configuration program configure according to the information collected by autoscan and user modifications), autoheader (creating configuration header files), automake (template makefile.in that creates Makefiles).
The following is to install autoconf using the apt-get command
3. Autotools example
1. Prepare the source code
First write main.c
#include <stdio.h> #include "common.h" intmain() { hello_method(); return 0; }
hello.c file
#include <stdio.h> #include "common.h" void hello_method() { printf("hello_world!\n"); }
common.h file
void hello_method();
2. Run the autoscan command
Switch to the project working directory and execute the autoscan command to scan the working directory to generate the configure.scan file
Generated configure.scan file
# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) AC_CONFIG_SRCDIR([main.c]) AC_CONFIG_HEADERS([config.h]) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_OUTPUT
Among them are M4 macro commands, the main content of these macro commands is the detection system.
3. Modify the autoscan.scan file and rename it to configure.ac
The configure.ac file is the configuration file of autoconf. Modify and edit the configuration file as follows:
# -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) AC_INIT([hello], [1.0], [[email protected]]) AC_CONFIG_SRCDIR([hello.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_CONFIG_FILES([Malkefile]) AC_OUTPUT
The macro AC_INIT is modified here, and the macros AM_INIT_AUTOMAKE and AC_CONFIG_FILES are added.
The macros in the configure.ac configuration file are explained in the following table
4. Execute the aclocal command
Execute the aclocal command to scan the configure.ac file to generate the aclocal.m4 file
The aclocal.m4 file mainly deals with local macro definitions. The aclocal command centrally defines the macros required by the configure.ac file into the file aclocal.m4 based on the installed macros, user-defined macros, and macros in the acinclude.m4 file.
5. Execute the autoconf command
Execute the autoconf command to generate the configure file.
This command expands the macros in the configure.ac file to generate the configure script. This process may use the macros defined in aclocal.m4.
6. Execute the autoheader command
Execute the autoheader command to generate the config.h.in file.
If the user needs to attach some symbol definitions, they can create an acconfig.h file, and autoheader will automatically copy the symbol definitions from the acconfig.h file.
7. Create Makefile.am file
Create a Makefile.am file in the project directory for the automake tool to convert Makefile.am into a Makefile.in file according to the parameters in configure.in. The Makefile defines some rules for generating Makefiles. The content of Makefile.am created in this example is as follows:
AUTOMARK_OPTIONS = foreign bin_PROGRAMS = hello hello_SOURCES = main.c hello.c common.h
Where AUTOMARK_OPTION is the option of automake. GNU has strict specifications for the software released by itself. For example, the license statement file COPYING must be attached. Otherwise, an error will be reported when automake is executed. Automake provides 3 software levels; foreign, gnu and gnits for users to choose. The default is GNU, this In this example, the lowest foreign level is used, and only necessary files are detected.
bin_PROGRAMS defines the executable file name to be generated. If multiple executable files are to be generated, separate each file name with a space.
file_SOURCES defines the source files that are required to generate executable files.
7. Execute the automake command
Execute the automake command in the project directory to generate the Makefile.in file. Usually use --add-missing to let automake automatically add some required script files. However, there are still several necessary files that have not been created. Here, they are created by the touch command, and then the automake command is executed.
So far, the autotools tool has been used to complete the preparation for the source code installation, and then you can follow the three steps of the source code installation to complete the software compilation and installation.
9. Execute the ./configure command
Execute the ./configure command to generate the final Makefile based on Makefile.in, which adds some configuration parameters to the Makefile.
10. Execute the make command and run the program
Execute the make command, compile the source code file based on the Makefile file and generate an executable file.
Then run the resulting executable in that directory for testing.
10. Execute the make install command
Execute the make install command in the project directory to install the software package into the system. The default setting will install the software to the /usr/local/bin directory, which requires root privileges. After the installation is complete, you can directly run the hello command
The automatically generated Makefile file indicates the main target, such as executing the make uninstall command to uninstall the installed software from the system; executing the make clean command to clear the compiled files, including the target file *.o and executable files. The make command executes the make all command by default.
11. Execute the make dist command to package the software
If you want to publish it externally, you can execute the make dist command in the project directory to package the program and related documents into a compressed file. as follows