Easily generate makefile using Automake

xiaoxiao2021-03-06  19

source:

Linux Eden

Abstract: People written in UNIX generally encounter Makefile, especially those who use C to develop procedures. It is really convenient to develop and compiler with make, but it is not so simple to write a makefile. Introduction to Makefile does not have much file, Gnu make, the file is a few hundred files, and the Overview I have to first Over, it is no wonder that many people will smell UNIX color change. This article will show how to use GNU AutoConf and Automake to help "Automatic" to generate the Makefile file, and let the developed software can be the same as Apache, MySQL, and common GNU software, as long as the `` ./configure '' , `` `` `` `` `` `You can install the program to the system. If you have an open source of Open Source, or just want to write a program under UNIX system. I hope this introduction file can help you easily enter 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 just compile and connect programs, such as FreeBSD port collection, makefile can also download remote programs, extract, patch, set, then compile, install In the system. Although the basic structure of the makefile is very simple, it can change many different patterns to properly use these rules. But because of this, many have just started to learn to write makefile, I feel that there is no specification that can be followed. Everyone writes a lot, I don't know where to start, and often restrictions on their own development environment, as long as environmental parameters Different or path changes, may make Makefile to follow the modifications. Although there are some standards and specifications for GNU Makefile Conventions to write Makefile when designing GNU program design, it is very long and complex, and often makes some adjustments, to reduce program developers to maintain Makefile's burden Therefore, Automake appeared. Program designers only need to write some pre-defined macros (Macro), which will generate a Makefile.in file that can be used by AutoConf after submitting the Automake process. Re-cooperates the automatic cultivation setting file Configure that uses AutoConf to generate a Makeifle that meets the GNU Makefile formula. 2. Before starting using Automake, you first confirm that your system is installed as the following software: 1. GNU Automake 2. GNU AutoConf 3. GNU M4 4. Perl 5. Gnu Libtool (if you need a shared library) recommended best The GNU C / C compiler, GNU Make, and other GNU tools are also used as the development environment, which are Open Source Software not only free and powerful.

If you are using Red Hat Linux, you can find all the above software RPM files. FreeBSD also has ready-made packages that can be installed directly, or you can download the source code of these software back to install it. 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 connection, and how to generate a program file (such as the Manual page, Info file, and DVI files), and the source file package is packaged The release is considered, so the directory structure stored by the program source code is best in line with the GNU standard practice, and then uses a Hello.c as an example. Create a new subdirectory "server" in the working directory, then build a subdirectory of "Hello" under Devel, this directory will serve as the Hello program and its related files:% mkdir off% CD Devel % mkdir hello% CD Hello writes a Hello.c file with editor, #includeint main (int argc, char ** argv) {

Printf (`` Hello, GNU! N ');

Return 0;

}

Next, use AutoConf and Automake to generate a makefile file.

1.

Use AutoScan to generate a CONFIGURE.IN prototype. After executing AutoScan, a configure.scan file can be used, which can be used as the blueprint of the Configure.in file.

% AutoScan

% LS

Configure.scan hello.c

2.

Edit the configure.scan file, as shown below, and renamed to configure.in

DNL Process this file with autoc 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, generate two files in ACLOCAL.M4 and Configure, respectively

% 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 WHETER BUILD ENVIRONMENT IS SANE ... YES CHECKING WHETHER MAKE SET $ {opportunity ... 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 WEARE USING GNU C ... YES Checking WHETHER GCC Accepts -g ... Yes Updating Cache ./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-^ -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. Chasing the roots

The above-mentioned procedure for producing Makefile is very different from the way, abandoning the rules of traditional custom make, using Automake only need to use some of the already defined macros. We write macros and targets in the makefile.am file, and Automake will open this string after reading the Makefile.am file and generates the corresponding Makefile.in file, then by configure this. Shell Script produces a suitable Makefile according to Makefile.in.

[Figure 1: Process for producing Makefile using AutoConf and Automake]

The above figure shows the file files to be used in the previous example and the generated files, and there is an asterisk (*) represents the executable file. In this example, additional files produced by AutoConf and Automake tools include configure.scan, aclocal.m4, configure, makefile.in, requiring it to join set with configure.in and makefile.am. 4.1 Editing configure.in files

AutoConf is a tool for generating a 'configure' file. 'configure' is a shell script that automatically sets the original program to comply with the characteristics of the Unix system on different platforms, and generate suitable Makefile files or C's header files (header file) according to system parameters and environments. The program can be easily compiled on a different platform. AutoConf reads the configure.in file and generates 'Configure' this shell script.

The Configure.in file content is a series of GNU M4 macros, which will turn the shell scripts that check the system characteristics after AutoConf. The order of Configure.in is not particularly specified, but each configure.in file must be added to all the AC_INIT macros before all macros, and then the AC_OUTPUT macro in all macros. You can use AutoScan to scan the original file to generate a configure.scan file, and then modify configure.in files for configure.scan. The macro used in the example is as follows:

DNL

The word behind this macro will not be processed, can be considered comment

AC_INIT (File)

This macro is used to check the path where the source code is located, and AutoScan will automatically generate, generally no need to modify it.

AM_INIT_AUTOMAKE (Package, Version)

This is the macro necessary for using Automake. Package is the name of the software suite to produce, and Version is the version number.

AC_PROG_CC

Check the C compiler that the system is available, if the source code is written with C, this macro is required.

AC_OUTPUT (File)

Set the files to be generated by Configure, if Makefile, Configure will use the result it checks into the Makefile.in file to generate the appropriate makefile.

In fact, when using Automake, there are some other macros that we use aclocal to help. Performing Aclocal generates ACLOCAL.M4 files, if there is no special purpose, you can do not need to modify it, and the macke generated by ACLOCAL tells Automake how to move.

After two files with configure.in and aclocal.m4, AutoConf can be executed to generate configure files.

4.2 Edit Makefile.AM file

Next to edit the makefile.am file, Automake will turn makefile.am to the makefile.in file according to the macros in Configure.in. Makefile.am file definitions to be generated:

Automake_Options

Set the option of Automake. Automake is mainly helping to develop people from GNU software to maintain software, so when executing Automake, there is a file that should have in the standard GNU software, such as 'news', 'author', 'Changelog' and other files. When FOREIGN is set, Automake will check the standard of general software. BIN_PROGRAMS

Define the execution file name to be generated. If you want to generate multiple execution files, each file name is separated by a blank.

Hello_sources

Define the original file required by 'Hello'. If the 'hello' is generated by multiple raw files, all the original files used to be used must be listed, separated by blank. Assuming that 'Hello' also needs 'hello.c', 'main.c', 'hello.h' three files, defined

Hello_sources = hello.c main.c hello.h

If multiple execution files are defined, each executor must define a relative filename_sources.

Edit your makefile.am file, you can use Automake --Add-missing to generate makefile.in. Plus -Add-missing options to tell Automake to make the files that are required to pack a software. Automake generated Makefile.in files are fully compliant with GNU Makefile, as long as Configure this shell script can produce a suitable makefile file.

4.3 Using Makefile

There are several predetermined targets for use with the makefile file generated by Configure, and only a few briefings are as follows:

Make all

Generate the set target, the execution file in the secondary example. Just type Make can also, start compiling the source code, then connect and generate execution files.

Make clean

Clear the previously compiled execution file and target file (Object file, * .o).

Make distclean

In addition to clearing the execution file and the destination file, the makefile generated by Configure is also cleared.

Make Install

Install the program to the system, if the source code is successful, and the execution result is correct, you can install the program to the system's pre-set execution file storage path. If you use a bin_programs macro, the program will be installed to / usr / local / Bin under bin.

Make dist

Pack the program and related documents as a compressed document for distribution. Performed in the directory will generate a file with package-version.tar.gz named. Package and Version These two parameters are defined by am_init_automake (package, version) in the configure.in file. The files of 'Hello-1.0.Tar.gz' are generated in this example.

Make disccheck

Similar to Make Dist, but add to the compressed file after the package is normal. In addition to packed the program and related document into tar.gz files, the compressed file will be automatically unwounded, execute Configure, and execute the Make All, After confirming the compilation error, the user shows this tar.gz file ready to be released. This check is very useful. Check the clearance kit, basically give any people who have a GNU development environment to recompile success. For this example, in addition to the Red Hat Linux, you can compile correctly in FreeBSD 2.2.x. It should be noted that the software suite produced by using AutoConf and Automake is available without installing AutoConf and Automake, 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 you generate new Configure and makefile.in files.

5. Related information

AutoCONF and Automake feature are very powerful, and detailed usage will be found from the INFOs they come with. You can also find related configure.in or makefile.am files from many existing GNU software or Open Source software, they are the best examples of learning AutoConf and Automake more techniques.

This brief is only used by AutoConf and Automake's fur, if you join the ranks of Open Source software development, I hope this article can help you have a simple understanding of generating makefile. Other detailed applications and techniques for developing GNU program or C programming and makefile, it is recommended to read from GNU Coding Standards (GNU coding regulations), including GNU Makefile conventions, and standard procedures and practices for developing GNU software. The online documentation of these GNU software can be found at http://www.gnu.org/.

6. Conclusion

With AutoConf and Automake, generating a makefile does not seem to be as difficult as before, and using AutoConf also makes us easy to publish and labine between different platforms or all UNIXs, this is a simple programming in the UNIX system It is for many burden. Use these GNU tool software to help us easier to develop programs, and easier to maintain source code.

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

New Post(0)