Makefile (ZT) easily

xiaoxiao2021-03-06  115

The BBS Shuimu Tsinghua Station, Xu Ming Yans, who wrote a program on UNIX, and people who have encountered 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 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 Hatlinux 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), as well as the original programs package 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 editor, #include int main (int argc, char ** argv) {printf (`` Hello, GNU! / n '); Return 0; } Next, use AutoConf and Automake to help us produce makefile files. 1. Generate a configure.in's prototype with AutoScan. After performing AutoScan, you can generate a configure.scan file, we can use it as configure. The file of the IN file.

% AutoScan% ls configure.scan hello.c 2. Edit 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 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. perform aclocal and autoconf, and configure each of which produce two files aclocal.m4% aclocal% autoconf% ls aclocal.m4configureconfigure.inhello.c 4. Makefile.am edit files, as follows AUTOMAKE_OPTIONS = foreign bin_PROGRAMS = hello hello_SOURCES = hello.c 5. performed 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. Final execution ./configure,% ./configure create cache ./config.cache checking for a BSD Compatible Install ... / U sr / bin / in stall -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 WHETER THE C Compiler (GCC) IS A Cross-Compiler ... No Checking WETHER WEARE USING GNU C ... YES Checking WHETER GCC Accepts -g ... Yes Updating ./config.cache Creating ./config.status create makefile Now your directory has been generated A Makefile file, next `` make ''

The 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-^ -o hello hello.O% ./hello Hello ! Gnu! You can also try the `` 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. 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 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 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 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. 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, 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.

转载请注明原文地址:https://www.9cbs.com/read-102056.html

New Post(0)