Makefile
Blue forest http://www.lslnet.com, March 22, 2001 08:44
Abstract:
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 open source of Open Source, or just want to write a writer under 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 complex, and often do some adjustments, to reduce program designers to maintain Makefile's burden. So there is 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: 1. Gnu automake2. Gnu autoconf3. GNU M44. Perl5. 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 Hat Linux 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 off% CD develop% mkdir Hello% CD Hello
Write a Hello.c file with the editor, # include stdio.hint main (int argc, char ** argv) {printf (`` Hello, GNU! '); 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% lsconfigure.scan hello.c
2. Edit the configure.scan file, as shown below, and change its file name to configure.indnl process this file with autoconf to production acon figure script.ac_init (Hello, 1.0) DNL Checks for programs.AC_PROG_CCdnl Checks for libraries.dnl Checks for header files.dnl Checks for typedefs, structures, and compiler ch aracteristics.dnl Checks for library functions.AC_OUTPUT (Makefile)
3. Execute ACLOCAL and AutoConf, generate ACLOCAL.M4 and Configure two file% aclocal% autoconf% lsaclocal.m4 configure configure.in Hello.c
4. Edit the Makefile.Am file, the content is as follows_Options = foreignbin_programs = HelloHello_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: configure.in: installing `./install-sh'automake: configure.in: installing `./mkinstalldirs'automake: configure.in:. installing` ./missing'6 last execution ./configure,% ./configurecreating cache ./config.cachechecking for a BSD compatible install ... / usr / bin / in stall -cchecking whether build environment is sane ... yeschecking whether make sets $ {MAKE} ... yeschecking for working aclocal ... foundchecking for working autoconf ... foundchecking for working automake ... foundchecking for working autoheader ... foundchecking for working makeinfo ... foundchecking for gcc ... gccchecking whether the C compiler (gcc) works ... yeschecking whether the C compiler (gcc) is a cross-co mpiler ... nochecking whether we are USING GNU C ... YESCHECKING WHETER GCC Accepts -g ... Yesupdating cache ./config.cachecreating ./config.statusCreating 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!
% makegcc -dpackage = "hello" -dversion = "1.0" -i. -i. -g -o2 -c he llo.cgcc -g-^ o hello hello.O% ./hellohello! 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.
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. In this example, the files generated by AutoConf and Automake tools include configure.scan, ACLOCAL.M4, Configure, makefile.in, and we need to join the settlers for configure.in and makefile.am. 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:
The word behind the DNL 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 a version number.
Ac_Prog_cc Checks the C compiler available to the system. 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 of the results to the Makefile.IN file and generate 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 Sets the option for 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 defines the execution file we have to produce. If you want to generate multiple execution files, each file is separated by a blank character.
Hello_sources defines the original file required for the '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. Suppose the 'Hello' is needed to 'Hello.c', 'main.c',
The words of 'Hello.h' three files, define hello_sources = hello.c main.c hello.h If we define multiple execution files, you need to define relative filename_sources for each execution 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 produces the target 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 Clears the previously compiled executive file (Object file, * .o).
The Make DistClean is also cleared by clearing the Makefile generated by Configure in addition to clearing the execution file and destination.
Make Install 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 Package 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 defined by am_init_automake (package, version) in the configure.in file. In this example, 'Hello-1.0.Tar.gz' file will be generated.
Make DistCheck and make Dist are similar, but the addition of 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 kit, basically give any people who have GNU development 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/.
6. Conclusion
Via AutoConf and Automake auxiliary, generating a makefile does not seem to be as difficult as before, and using AutoConf also makes us spread and compile the compilation between different platforms or Unix, this is on the UNIX system The developed program has alleviated a lot of burdens. Properly use these GNU tool software to help us easier to develop programs, and easier to maintain the original code.
In 1998, it was a year of Open Source, and many Open Source's software was generally welcomed and used on the Internet. Thanks to all people dedicated to Open Source, I hope that by this article can attract more people to add "free", "open" OpenSource ranks.
About this document ...
Makefile1
This document was generated using the LaTeX2HTML translator Version 98.2 beta6 (August 14th, 1998) Copyright (C) 1993, 1994, 1995, 1996, Nikos Drakos, ComputerBased Learning Unit, University of Leeds.Copyright (C) 1997, 1998, Ross Moore , Mathematics Department, Macquarie University, Sydney.The Command Line Arguments WERE: LATEX2HTML -SPLIT 0 -SHOW_SECTION_NUMBERS Automake.Texthe Translation Was Initiated by iN 1999-02-11
FootNotes ... itle1 This document is made using ChiLatex. ... CLE2CLE (Chinese Linux Extension, Linux Chinese Extension Kit), http://cle.linux.org.tw/
... STANDARDS3GNU Coding Standards, Richard Stallman.