People who write programs on UNIX have passed Makefile, especially those who use C to develop programs. It is very convenient to develop and compile the program with make, but it is not easy to write a makefile. Introduction to Makefile is not much file, Gnu make, the file is a few hundred files, and the light is overfold, it is no wonder that many people will smell Unix color change. This article will introduce how to use GNU AutoConf and Automake to assist us "Auto" to produce Makefile files, and let the developed software can be like Apache, MySQL, and common GNU software, as long as it will `` ./configure '' , `` Make '', `` `` '' can be installed in the system. If you have an openSource's software, or just want to write a write in the UNIX system. I hope this introduction file can help you easily access the hall of UNIX Programming.
1 Introduction
Makefile is basically a series of rules composed of "target", "DependenCIES" and "Actions". Make will determine how to compile (LINK) based on the rules of Makefile. In fact, Make can do is not just compilation and linking, such as FreeBSD's port collection, makefile can also download the original program kit, extract, patch (PATCH), set, then compile, install In the system.
Although the basic structure of Makefile is simple, it can also become a lot of different tricks properly use these rules. But there is therefore, many when you start learning to write makefile, you will feel that there is no specification, and everyone writes. Makefile is not too much. I don't know where to start, and often limited to my own development environment, as long as the environment Different variables or path change, maybe makefile will be modified. Although there are some standards and specifications for GNU Makefile Conventions to book Makefile when designing GNU program design, it is very long and complicated, and often do some adjustments, to reduce program designers to maintain Makefile's burden, With Automake.
Program designers only need to write some pre-defined huge gauges (Macro), which will produce a Makefile.IN file for AutoConf after handing it to Automake. The AUTOCONF can be again generated to generate Makeifle that conforms to GNU Makefile conventions.
2. Before going on
Before you start trying with Automake, please confirm your system to install the following software:
GNU Automake
2. gnu autoconf
3. GNU M4
4. Perl
5. GNU Libtool (if you need shared library)
I would recommend that you also use the GNU C / C compiler, GNU Make, and other GNU tools to be developed, these tools belong to Open Source Software not only free and powerful. If you use Red Hat Linux to find all the above-mentioned soft rpm files, FreeBSD also has ready-made packages to install directly, or you can also download the original gear back DIY. The following example is done in the environment of Red Hatlinux 5.2 Cle2. 3. A simple example
The Makefile generated by Automake can have the action of how to generate program files (such as the Manual Page, Info files, and DVI files), and the original program package is considered. Go in, the directory architecture stored by the original program is best in line with the standard practice of GNU, and I will take Hello.c as an example.
Create a new subdirectory `` west in the working directory, then build a subdirectory of a `` hello '' under DEVEL, this directory will be placed as us of Hello and its related files:
% MKDir Devel
% CD Devel
% MKDIR Hello
% CD Hello
Write a Hello.c file with the editor.
#include
INT main (int Argc, char ** argv)
{
Printf (`` Hello, GNU! / n ');
Return 0;
}
Next, use AutoConf and Automake to help us produce your Makefile file.
1. Generate a configure.in's prototype with AutoScan. After performing AutoScan, you will generate a configure.scan file, we can use it as a CONFIGURE.IN file.
% AutoScan
% LS
Configure.scan hello.c
2. Edit the configure.scan file, as shown below, and change its file name 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 Program.
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, which generate two files in ACLOCAL.M4 and Configure, respectively.
% ACLOCAL
% AutoConf
% LS
Aclocal.m4configureconfigure.inhello.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 according to 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 / in
Stall -c
Checking WHether Build Environment Is Sane ... YES
Checking WHETHER Make Set $ {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 WHETER GCC Accepts -g ... YES
Updating cache ./config.cache
Creating ./config.status
Creating makefile
Now you have generated a Makefile file in your directory, the next `` Make '' instruction can start compiling Hello.c into execution file, execute ./hello and gnu sound!
% make
gcc -dpackage = / "hello /" -dversion = / "1.0 /" -i. -i. -g -o2 -c hello.c
GCC -G -O2 -O Hello Hello.o
% ./hello
Hello! GNU!
You can also try a `` make clean '', '' make install '', '' make dist '' see what results will have. You can also give your boss, let him look at you from here :-)
4. A exploration
The process of producing Makefile is very different from the past, discarding the rules of traditional self-definition make, using Automake only need to use some of the already defined huge sets. We write the giant set and target in the Makefile.Am file, and Automake will define this string after reading the Makefile.Am gear and generates the corresponding Makefile.IN file, then follow configure This shell script produces suitable Makefile according to Makefile.in. 4.1 Edit Configure.in
AutoConf is a tool for generating a 'configure' file. 'configure' is a shell script that automatically sets the original program to meet the characteristics of the Unix system on different platforms, and generate suitable makefile files or C's header files according to the system parameters and the environment. To make the original program can be easily compiled on these different platforms. AutoConf reads the configure.IN file and generates 'Configure' this shell script.
The content of the Configure.in file is a collection of GNU M4, which will turn to check the system features after the AutoConf process. The order of the conifure.in is not particularly regulations, but every configure.in file must be added to the AC_INIT gather before all the gauges, and then add the AC_OUTPUT gather on all the gauges. We can use AutoScan to scan the original gear to generate a configure.scan file, and then modify the configure.in file for configure.scan. The gauge used in the example is as follows:
DNL
The words behind this gather will not be processed and can be considered as an annotation.
AC_INIT (File)
This gauge is used to check the path where the original code is located, and AutoScan will automatically generate, we do not have to modify it.
AM_INIT_AUTOMAKE (Package, Version)
This is the huge set of automake, and package is the name of the software we have to generate, and Version is the version number.
AC_PROG_CC
Check the C compiler that the system is available, if the original program is written with C, you need this gauge.
AC_OUTPUT (File)
Set the file to be generated by configure, if it is makefile, Configure will take the result it checks into the Makefile.IN file and produce the appropriate makefile.
In fact, when we use Automake, we must also have some other gauges, and these additional huge gather we use ACLOCAL to help us. Executing Aclocal will generate aclocal.m4 files. If there is no special purpose, we can do not have to modify it, and you will tell Automake how to do it with ACLOCAL.
After two files with configure.in and aclocal.m4, AutoConf can be executed to generate Configure files.
4.2 Edit Makefile.Am
Next we want to edit the makefile.Am file, Automake will turn makefile.am to makefile.in files according to the gauge in Configure.in. Makefile.Am file defines the goal we have to produce:
Automake_Options
Set the option of Automake. Automake is mainly to help develop a GNU software to maintain a software kit, so when executing Automake, there is a file file that should have in the standard GNU software suite, such as 'news', 'author', 'Changelog' and other files. files. When it is Fornow, Automake will check the standard of the general software suite. BIN_PROGRAMS
Define the implementation file name we have to produce. If you want to generate multiple execution files, each file is separated by a blank character.
Hello_sources
Define the original file required by 'Hello'. If the 'Hello' is generated by multiple original files, it must be listed in the original files it used to separate it with a blank graph. Assuming the 'Hello' requires 'Hello.c', 'main.c', 'hello.h' three files, define hello_sources = hello.c main.c hello.h If we define multiple execution files, The relative filename_sources is defined for each execut file.
To edit your makefile.AM file, you can use Automake --Add-missing to generate makefile.in. Plus - ADD-MISSING Options is to tell Automake to help us join the files necessary to package a software suite. The Makefile.in file produced by Automake is fully conforming to GNU Makefile, and we can generate the appropriate makefile file as long as you perform Configure Script.
4.3 Using Makefile
There are several preset targets that use the makefile file generated by Configure to use, and we only take the following brief description:
Make all
Generate the goal we set, that is, the execution file in this example. Only Make can also, at which time you start compiling the original code, then connect, and generate an execution file.
Make clean
Clear the previously compiled execution file and destination file (Object file, * .o).
Make distclean
In addition to clearing the execution file and destination, the makefile generated by Configure is also cleared.
Make Install
The program is installed into the system. If the original code is compiled, and the execution result is correct, you can install the program to the system preset executive storage path. If we use bin_programs to gather, the program will be installed to / usr / local / bin.
Make dist
Pack the program and related files into a compressed file for district. Performed in the directory will generate a file with package-version.tar.gz name. Package and Version These two variables are based on the Configure.IN file
AM_INIT_AUTOMAKE (Package, Version) definition. In this example, 'Hello-1.0.Tar.gz' file will be generated.
Make disccheck
Similar to Make Dist, but add to whether the compressive file after the package is normal. In addition to putting the program and the relevant file into tar.gz file, this automatically unspeak this compressed file, executes configure, and performs Make ALL's action. After confirming compilation, this tar.gz file is ready It is good for spreading. This check is very useful, check the clearance kit, basically give any people who have a GNU development environment to recompile. In addition to the Hello-1.Tar.gz, in addition to the redhat Linux, the freebsd 2.2.x can also be recompiled correctly. It should be noted that the software kit produced by using AutoConf and Automake is available in an environment where AutoConf and Automake is installed, because configure is a shell script, which is designed to execute under the SHELL of general Unix. . But if you want to modify the configure.in and makefile.Am files, you must have AutoConf and Automake when new Configure and makefile.in files are generated.
5. Related messages
AutoConf and Automake feature are very powerful, you can find detailed usage from the INFO files they attach. You can also find related configure.in or makefile.Am files from many existing GNU software or open source software, which is the best example of learning AutoConf and Automake more techniques.
This introduction only uses autoconf and automake's fur, if you join the ranks of open source software development, I hope this file can help you have a simple basis for generating makefile. Others related to the development of GNU program or C programming and Makefile, I suggest you read from GNU Coding Standards3 (GNU coding standard), which contains GNU Makefile conventions, as well as standard procedures for developing GNU software kits and Convention. These GNU software's online description files can be found on http://www.gnu.org/.