Easy Makefile
Blue forest http://www.lslnet.comstone March 22, 2001 08:44
Author: Xu Ming Yans
Abstract: People written on UNIX probably touched Makefile, especially those who developed with C. 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 to the road, please try to use Automake before you first confirm that your system has installed the following software: 1. 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 Hat Linux 5.2 CLE2. 3. A simple example Automake generated by Makefile, in addition to the programming and linking, how to generate program files (such as the Manual Page, Info files, and DVI files), and pack the original program The dispersive movements are considered, so the directory architecture stored by the original program is best in line with the GNU standard practice, and I will take Hello.c as an example. Create a new subdirectory `` '' in the working directory, then build a subdirectory of `` hello '' under DEVEL, this directory will serve as our Hello's program and its related files:% MKDir Devel % CD Develop% Mkdir Hello% CD Hello writes a Hello.c file with editors, #include stdio.h int main (int argc, char ** argv) {printf (`` `Hello, GNU! '); Return 0 Next, you will use AutoConf and Automake to help us generate makefile files. 1. Use AutoScan to generate a configure.in's prototype. After performing AutoScan, you will generate a configure.scan file, we can use it as configure Set of blue.
% 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 con figure 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 ch aracteristics. dnl Checks for library functions. AC_OUTPUT (Makefile) 3. perform aclocal and autoconf, and configure each of which produce two files aclocal.m4% aclocal% autoconf% ls aclocal.m4 configure configure.in hello.c 4. Makefile.am edit files, 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 files, 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 . ...................... FOUND CHECKING for WORKING ... FOUND Checking for Working AutoConf ...fact Checking for Working AutoConf ...fact Working Automake ... 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 crossher ... No checking WETHER WEE Are Using GNU C ... YES Checking WHETHER GCC Accepts -g ... Yes Updating cache ./config.cache Creating ./config.status CREATING Makefile now Your directory A Makefile file has been produced, the next `` make '
'The instruction can start compiling the Hello.c into the execution file, execute ./hello and gnu sound! % make gcc -dpackage = "hello" -dversion = "1.0" -i. -i. -g -O2 -c he llo.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 you the boss, let him look at you from this to you :-) 4. A exploring the above-mentioned process of producing Makefile is very different, abandon the rules of the traditional self-definition MAKE Use Automake only need to use some of the highly defined huge gauges. 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. [Figure 1: Flow to produce Makefile using AutoConf and Automake] The above figure shows the files you want to use in the previous example and generated files, as an asterisk (*) represents the executable file. 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 Editing configure.IN file AutoConf is a tool for generating 'configure' files. '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 word behind this gauge will not be processed, which can be considered 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 we have to produce a software kit. Version is a 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 huge set. 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 convert makefile.am into 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 setting into Foreign, Automake will use the standard of the general soft kit to check. 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 space off with blank. 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 The Makefile file generated by Configure has several preset targets, we only have several briefings as follows: make all produces the target we set, that is, the execution file in this example. Only Make can also be compiled, and then the original code is started, and then connect, and the execution file is generated. 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, it can be installed to the system preset execution file storage path. If we use bin_programs, our program will be installed to / usr / local / bin this directory. Make Dist Package the program and related files into a compressed file for district. It will generate a file that is named by package-version.tar.gz executing. 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 non-commonly useful, check the kits, basically gives any people with GNU development to recompile. In addition to Hello-1.Tar.gz, in addition to the red hat linux, you can also correctly recompile the FreeBSD 2.2.x. 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 assistant, generating a makefile does not seem to be as difficult as before, and using AutoConf also makes us spread and compile the program between different platforms or Unix, this is A number of burden is reduced by developing programs on UNIX systems. 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.