[Reprint] Use automake and Autoconf to generate makefile

Source: Internet
Author: User
Tags gz file automake
Use automake and Autoconf to generate makefile
People who have written programs on UNIX, especially those who use C to develop programs, generally encounter makefile. It is really convenient to use make to develop and compile programs, however, writing a makefile is not that easy. GNU make's hundreds of pages of files scared many people. Of course, there are many files about make, but writing a makefile is always a very annoying thing. The GNU Autoconf and automake software are used to help program developers easily generate makefile files. Currently, GNU software, such as Apache and MySQL MiniGUI, uses Autoconf and automake for automatic compilation. You only need to use "./configure", "make", and "make install" to install the program to the system.

Introduction
Makefile is basically a series of rules composed of target, dependencies, and action. Make decides how to compile (compile) and link programs or other actions based on makefile rules. Of course, make can not only compile and connect programs. For example, in the FreeBSD port collection, makefile can also automatically download remote programs, extract (extract), patch (patch), and set, then compile and install it to the system.

Although the basic structure of makefile is very simple, using these rules can transform many different patterns. Because of this, many people will feel that there are no rules to follow when they first learn to write makefiles. The makefiles written by each person are not the same, and they do not know where to start, in addition, it is often restricted by the development environment. As long as the environment parameters are different or the path is changed, makefile may have to be modified. Although GNU makefile conventions (GNU makefile Convention) has developed some standards and specifications for writing makefile during GNU program design, its content is very long and complex, in addition, we often make some adjustments. In order to reduce the burden on program developers to maintain makefiles, automake emerged.

With automake, programmers only need to write some predefined macros (macro) and submit them to automake for processing. A makefile. In file that can be used by Autoconf will be generated. Then, use the automatic configuration file configure generated by Autoconf to generate a makeifle that complies with the GNU makefile convention.

Required Software
Before using automake, make sure that your system has the following software installed:

1. GNU automake
2. GNU Autoconf
3. GNU M4
4. Perl
5. GNU libtool (if you need to generate shared library)

It is best to use the gnu c/C ++ compiler, GNU make, and other GNU tool programs as the development environment. These tools are not only free but also powerful open source software. If you are using Red Hat Linux, you can find all the RPM files of the above software.

A simple example
In addition to program compilation and connection, makefile generated by automake can also be used to generate documents (such as manual page and info files ), you can also package source code files for release. Therefore, the directory structure stored in the source code of the program is best in line with the GNU standard practice. Next we will use a hello. C is used as an example.

Create a new subdirectory devel under the working directory, and create a subdirectory "hello" under devel.
As the place to store the hello program and its related files:

% Mkdir devel; CD devel; mkdir hello; CD hello

Write a hello. c file in the editor,

# Include <stdio. h>
Int main (INT argc, char ** argv)
{
Printf ("Hello, GNU! N ");
Return 0;
}

Next we will use Autoconf and automake to generate the MAKEFILE file,

1. Use autoscan to generate a Configure. In prototype. After autoscan is executed, a Configure. Scan file will be generated, which can be used as the blueprint for the configure. In file.
 
% Autoscan
% Ls
Configure. Scan hello. c

2. Edit the configure. Scan file, as shown below, and change it to configure. In.

DNL process this file with Autoconf to produce a configure script.
Ac_init (hello. c)
Am_init_automake (hello, 1.0)
DNL checks for programs.
Ac_prog_cc
DNL checks for libraries.
DNL checks for header files.
DNL checks for typedefs, structures, and compiler characteristics.
DNL checks for library functions.
Ac_output (makefile)

3. Execute aclocal and Autoconf. The aclocal. M4 and configure files will be generated separately.

% Aclocal
% Autoconf
% Ls
Aclocal. M4 configure Configure. In hello. c

4. Edit the makefile. Am file. The content is as follows:

Automake_options = foreign
Bin_programs = Hello
Hello_sources = Hello. c

5. Execute automake -- add-missing. automake will generate some files based on the makefile. Am file, including the most important makefile. In

% Automake -- add-Missing
Automake: Configure. In: Installing './install-Sh'
Automake: Configure. In: Installing './mkinstalldirs'
Automake: Configure. In: Installing './missing'

6. Finally execute./configure:

%./Configure
Creating cache./config. Cache
Checking for a BSD compatible install.../usr/bin/install-C
Checking whether build environment is sane... yes
Checking whether make sets $ {make}... yes
Checking for working aclocal... Found
Checking for working Autoconf... Found
Checking for working automake... Found
Checking for working autoheader... Found
Checking for working makeinfo... Found
Checking for GCC... gcc
Checking whether the C compiler (GCC) works... yes
Checking whether the C compiler (GCC) is a cross-compiler... no
Checking whether we are using gnu c... yes
Checking whether GCC accepts-G... yes
Updating cache./config. Cache
Creating./config. Status
Creating makefile

$ Ls
Makefile aclocal. M4 config. Status hello. c mkinstalldirs
Makefile. Am config. cache configure install-SH
Makefile. In config. Log Configure. In missing

Now a MAKEFILE file has been generated in your directory. Enter the make command to compile hello. C!

% Make
Gcc-dpackage = "hello"-dversion = "1.0"-I.-I.-G-O2-C hello. c
Gcc-g-O2-O hello. o

You can also try "make clean", "make install", "Make Dist":
[Root @ localhost Hello] # Make clean
Test-z "hello" | RM-F hello
Rm-f *. O core *. Core
[Root @ localhost Hello] # make install
Gcc-dpackage = "hello"-dversion = "1.0"-I.-I.-G-O2-C hello. c
Gcc-g-O2-O hello. o
Make [1]: Entering directory '/home/Joe/devel/hello'
/Bin/sh./mkinstalldirs/usr/local/bin
/Usr/bin/install-C Hello/usr/local/bin/Hello
Make [1]: Nothing to be done for 'Install-data-am '.
Make [1]: Leaving directory '/home/Joe/devel/hello'
[Root @ localhost Hello] # Make Dist
Rm-RF hello-1.0
Mkdir hello-1.0
Chmod 777 hello-1.0
Here = 'CD. & pwd ';
Top_distdir = 'CD hello-1.0 & pwd ';
Distdir = 'CD hello-1.0 & pwd ';
CD.
& Automake -- include-deps -- Build-Dir = $ here -- srcdir-name =. -- output-Dir = $ top_distdir -- foreign makefile
Chmod-r a + R helm-1.0
Gzip = -- best gtar chozf hello-1.0.tar.gz hello-1.0
Rm-RF hello-1.0
Everything works well! Of course, you must have the root permission to copy files to the system directory during make install.

Further steps
The above process of generating makefile is very different from the previous method of self-writing. To use automake, you only need to use some defined macros. We will write the macro and target in the makefile. Am file. After automake reads the makefile. Am file, it will expand the defined macro and generate the corresponding
Makefile. In, and then the configure shell script generates a suitable makefile Based on makefile. In.
The specific process is as follows:
Code --> [autoscan *] --> [Configure. Scan] --> Configure. In
Configure. In --... ------> Autoconf * -----> Configure
+ --- +
[Aclocal. M4] -- + '---.
[Acsite. M4] --- '|
+ --> [Autoheader *]-> [config. H. In]
[Acconfig. H] ----. |
+ -----'
[Config. H. Top] -- +
[Config. H. Bot] --'

Makefile. Am -- & #61664; [Autoconf *] -------> makefile. In

. -------------> Config. Cache
Configure * ------------ + -------------> config. Log
|
[Config. H. In]-. V.-> [config. H]-.
+ --> Config. Status *-++ --> make *
Makefile. In --- ''-> makefile ---'

Indicates the files to be used and generated during the entire process. The star number (*) indicates the executable file. In this example, the additional files generated by the Autoconf and automake tools include configure. scan, aclocal. m4, configure, makefile. in, you need to add configure to the settings. in And makefile. am. The files to be written by the developer are confiugre. In And makefile. Am. In the MiniGUI project, we concentrate a series of commands into a batch file: autogen. sh:

#! /Bin/sh
Aclocal
Autoheader
Automake -- add-Missing
Autoconf

You only need to execute the batch file and combine Configure. In And makefile. AM to generate the required makefile.

Edit the configure. In File
Autoconf is a tool used to generate a 'configure 'file. 'Configure 'is a shell script that automatically sets compilation parameters so that the program can be compiled with conditions to conform to Unix systems of different platforms. Autoconf reads the configure. In file and generates the shell script 'configure.

The content of the configure. In file is a series of GNU M4 macros. After Autoconf is processed, these macros become shell scripts that check system features. The macro sequence in the configure. In file is not specified, but each Configure. In file must add the ac_init macro before all other macros, and then add the ac_output macro at the end of all other macros. Generally, you can use autoscan to scan the original file to generate a Configure. Scan file, and then modify Configure. Scan to the configure. In file. The macros used in the example are as follows:

DNL
The content after this macro will not be processed. It can be considered as a comment.

Ac_init (file)
This macro is used to check the path where the source code is located. autoscan will automatically generate the Macro. Generally, you do not need to modify it.

Am_init_automake (package, Version)
This is a necessary macro for automake. Package is the name of the software to be generated, and version is the version number.

Ac_prog_cc
Check the available C compiler. If the source code is written in C, this macro is required.

Ac_output (file)
Set the file to be generated by Configure. If it is makefile, configure will fill the result it checks into the makefile. In file and generate a suitable makefile.

In fact, some other macros are needed when automake is used. We use aclocal to help generate these macros. Executing aclocal will generate the aclocal. M4 file. If there is no special purpose, you do not need to modify it. The macro generated by aclocal will tell automake how to act.

With the configure. In And aclocal. M4 files, you can execute Autoconf to generate the configure file.

Edit makefile. Am File
Next, edit the makefile. Am file. automake converts makefile. Am To The makefile. In file with the help of Perl Based on the macro in configure. In. The makefile. Am file defines the target:

Automake_options
Set the automake option. Automake is mainly used to help GNU software developers maintain the software. Therefore, when executing automake, it will check whether there are files in the standard GNU software in the directory, for example, 'News', 'author ',
Files such as 'changelog. When it is set to foreign, automake will use the standard of general software to check.

Bin_programs
Defines the name of the execution file to be generated. If multiple execution files are to be generated, each file name is separated by a blank space.

Hello_sources
Defines the original file required by the execution program 'Hello. If the 'hello' program is generated by multiple original files,
All the original files used by the tool must be listed and separated by blank spaces. Assume that 'hello' requires three files, namely 'hello. C', 'main. C', and 'hello. H '.
Hello_sources = Hello. C main. c hello. h
If multiple execution files are defined, the corresponding filename_sources must be defined for each execution program.

After you edit the makefile. Am file, you can use automake -- add-missing to generate makefile. In. Add the -- add-missing option to tell automake to package the file required by a software program. If you do not use this option, automake may complain about the missing file. The makefile. In file generated by automake fully complies with the GNU makefile Convention. You only need to execute the configure shell.
Scripts can generate suitable makefile files.

Use makefile
The makefile generated by configure has several preset targets for use. Here we only use a few brief descriptions as follows:

Make all
Generate a set target, which is an executable file in the example. You can only press make. At this time, the source code will be compiled, connected, and the execution file will be generated.

Make clean
Clear the previously compiled executable files and target files (object file, *. O ).

Make distclean
In addition to clearing executable and target files, the makefile generated by configure is also cleared. This command is usually executed before the software is released.

Make install
Install the program to the system. If the source code is compiled successfully and the execution result is correct, you can install the program to the pre-defined execution file storage path of the system. If the bin_programs macro is used, the program will be installed under/usr/local/bin.

Make Dist
Package programs and related documents as a compressed document for release (distribution ). After the execution is complete,
PACKAGE-VERSION.tar.gz is the name of the file. The package and version parameters are based on the configure. In
The definition of am_init_automake (package, version. In our example, the 'hello-1.0.tar.gz 'file will be generated.

Make distcheck
Similar to make Dist, but it is added to check whether the compressed file after packaging is normal. In addition to packaging the program and related documents into a tar.gz file, this object will automatically unbind the compressed file, execute configure and make all. After confirming that the compilation is correct, the tar.gz file is ready and can be released. When you see:
========================================================== =
Hello-1.0.tar.gz is ready for distribution
========================================================== =

You can release your software with peace of mind. After checking the approved kits, You Can recompile the software for anyone with the GNU development environment.
It should be noted that the software kits generated by using Autoconf and automake can be used in environments where Autoconf and automake are not installed, because configure is a shell script, it has been designed to be executed in the shell of a Common Unix Sh. However, if you want to modify the configure. In And makefile. Am files to generate new configure and makefile. In files, you must have Autoconf and automake.

Related Materials
We usually get started with some basic knowledge. When there are new requirements, we can solve the problem by referring to relevant documents and other people's examples, and continue to improve in practice.
Autoconf and automake functions are very powerful. You can find detailed instructions in the info documents attached to them. If you like HTML, you can download the hmtl version from the gun website. You can also find related configure from many existing GNU software or open source software such as MiniGUI. in or makefile. am files. They are the best examples of learning more Autoconf and automake skills.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.