Abstract: when developing a system, a system is generally divided into several modules, which improves the maintainability of the system. However, due to the inevitable association between modules, therefore, when a module is changed, other modules may be updated. Of course, it is okay to manually compile the connection for small systems. However, if it is a large system, there are many modules, the manual compilation method is not applicable. In Linux, A make command is provided to automatically maintain the target file. Compared with manual compilation and connection, the make command only updates the modified file (In Linux, there is a last modification time after a file is created or updated. The make command uses this last modification time to determine whether the file has been modified.), While not modifying the file, and make command will not miss a file to be updated.
Files and files or modules may depend on each other. The make command is maintained based on this dependency. Therefore, it is necessary to understand what dependency is: if we want to play a game, we must have a game CD and a computer (the dependency between the two), and the prerequisite for a game CD and a computer is that the financial conditions permit, in addition, when you have a game CD, you need to choose which game to play based on your mood. For example:
Play games
/
/
Game CDs
//
//
Economic mood
Of course, the make command does not know these dependencies on its own, but the programmer needsWrite dependency into a file called makefile. The MAKEFILE file contains some targets. Generally, the target is the file name. For each target, a set of commands are provided to achieve the target and other targets or file names that are dependent on the target, the following is a simple example of makefile:
# A simple makefile
Prog: prog1.o prog2.o // prog target depends on prog1.o and prog2.o
GCC prog1.o prog2.o-O prog // prog1.o and prog2.o generate prog
Prog1.o: prog1.c Lib. h // prog1.o depends on prog1.c Lib. h
Gcc-c-I.-O prog1.o prog1.c
Prog2.o: prog2.c
Gcc-C prog2.c
The above mamefile defines three targets: prog, prog1, and prog2. The semicolon is followed by a list of dependent files separated by a semicolon;
For the first target file prog, it has two dependent files: prog1.o and prog2.o. If any of the dependent files is updated, prog will also be updated. The command gcc
Prog1.o prog2.o-O prog is the command for generating Prog. Make checks whether the target needs to be updated using a recursive method. recursion updates the outdated target from the bottom up. Only when all the targets on a target are the latest, this target will be updated. The above makefile is used as an example. We modified prog2.c. When executing make, because the target prog depends on prog1.o and prog2.o, we must first check
Prog1.o and prog2.o are outdated. The target prog1.o depends on prog1.c and Lib. h. Since these two files have not been modified, they have not expired. Then, check the target prog2.o. He depends on prog2.c. Since we modified prog2.c, so prog2.c is newer than prog2.o in the target file, that is, prog2.o
So that all targets dependent on prog2.o are out of date. In this way, make will first update prog2.o and then update Prog.
If a line is too long and has reached the right boundary of the text editor, a backslash () can be used as a line break. All rows connected by the backslash will be processed as a line; in addition, wildcards (? Or *).
Sometimes, to simplify the writing of commands, you can define some macros and abbreviations in makefile. The following are some frequently used abbreviations:
$ @ Indicates the full name of the target.
$ * Indicates the target name with the suffix deleted.
$ <Indicates the first target name of the target.
Now you can use the abbreviation to modify the makefile:
# Use makefile
Prog: prog1.o prog2.o
GCC prog1.o prog2.o-o $ @
Prog1.o: prog1.c Lib. h
Gcc-c-I.-o $ @ $ <
Prog2.o: prog2.c
Gcc-C $ *. c
In a project, several objects may use the same file. c. If this file is modified later, you need to modify all. c. This is troublesome. You can define macros to solve this problem. Macros can make makefile clearer:
# Makefile with abbreviations and macros
Marco = prog1.o prog2.o
Prog: $ (Marco)
GCC prog1.o prog2.o-o $ @
Prog1.o: prog1.c Lib. h
Gcc-c-I.-o $ @ $ <
Prog2.o: prog2.c
Gcc-C $ *. c
For a large project, writing makefile by yourself is very troublesome, while standard GNU software (such as apacle) runs a configure script file to generate makefile; the GNU software automake and Autoconf are used to automatically generate configure. Developers only need to define macros and automake
Then, makefine. In for Autoconf is generated, and configure can be generated by using Autoconf. To use automake and Autoconf, you must install: GNU automake, GNU Autoconf, GNU M4, Perl, and GNU
Libtool.
Assume that you have a source file test. C. You can use autoscan to generate a Configure. Scan file and edit the file.
DNL process this file with Autoconf to produce a configure script.
Ac_init (test. c)
Ac_init_automake (test, 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)
Then, set configure. scan is renamed cnfigure. in, and then execute aclocal and Autoconf to generate aclocal. m4 and configure files: Edit makefile. am file, makefile. the am file contains the macros and target files we have defined.
And automake will read this file and generate the makefile. In file according to the macro we define:
Automake_options = foreign
Run_prog = test
Test_source = test. c
Next, execute automake-A. So far, the configure file has been successfully generated.
Example: Start With helloworld
The following process is as follows:
Create three new files:
Helloworld. c
Configure. In
Makefile. AM
Then execute:
Aclocal; Autoconf; automake -- add-missing;./configure; Make;./helloworld
The makefile is generated, and helloworld. C can be compiled.
It's easy. Just a few commands can make a makefile that complies with the conventions. How do you feel.
Now we will introduce the detailed process:
1. Create a directory
Create a helloworld directory in your working directory and use it to store the helloworld program and related files, such as in/home/My/Build:
$ Mkdir helloword
$ CD helloworld
2. helloworld. c
Then, use your favorite editor to write a hellowrold. c file, such as the command VI helloworld. C. Use the following code as the content of helloworld. C.
Int main (INT argc, char ** argv)
{
Printf ("Hello, Linux World! ");
Return 0;
}
Save and exit.
Now there should be a self-written helloworld. c In the helloworld directory.
3. Generate configure
We use the autoscan command to generate a Configure. In Template File Based on the source code in the directory.
Command:
$ Autoscan
$ Ls
Configure. Scan helloworld. c
After execution, a file Configure. Scan will be generated in the hellowrold directory. We can use it as the blueprint for Configure. In.
Change Configure. Scan to configure. In and edit it. Modify the following content to remove irrelevant statements:
======================================= Configure. in content start = ====
#-*-Autoconf -*-
# Process this file with Autoconf to produce a configure script.
Ac_init (helloworld. c)
Am_init_automake (helloworld, 1.0)
# Checks for programs.
Ac_prog_cc
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
Ac_output (makefile)
======================================= Configure. in content end =================================================== ====
Then execute the commands aclocal and Autoconf to generate two files: aclocal. M4 and configure:
$ Aclocal
$ Ls
Aclocal. M4 Configure. In helloworld. c
$ Autoconf
$ Ls
Aclocal. M4 autom4te. cache configure Configure. In helloworld. c
As you can see, configure. In contains macro definitions. These macros are processed by Autoconf and become shell scripts that check system features, environment variables, and required software parameters.
Autoconf is a tool used to generate an automatic configuration software source code script (configure. The configure script can run independently of Autoconf and does not require user intervention during the running process.
To generate the configure file, you must tell Autoconf how to find the macro you are using. The method is to use the aclocal program to generate your aclocal. M4.
Aclocal automatically generates the aclocal. M4 file based on the content of the configure. In file. Aclocal is a Perl script program defined as "aclocal"
-Create aclocal. M4 by scanning Configure. AC ".
Autoconf creates configure from the template file that lists the parameters required for software compilation in configure. In.
Autoconf requires the GNU M4 macro processor to process aclocal. M4 and generate the configure script.
M4 is a macro processor. Copy the input to the output and expand the macro. Macros can be embedded or user-defined. In addition to expanding macros, M4 also has some built-in functions used to reference files, execute commands, integer operations, text operations, loops, and so on. M4 can be used either as the front-end of the compiler or as a macro processor.
4. Create makefile. AM
Create a makefile. Am file and run the following command:
$ VI makefile. AM
The content is as follows:
Automake_options = foreign
Bin_programs = helloworld
Helloworld_sources = helloworld. c
Automake will automatically generate makefile. In based on your makefile. am.
Macros and targets defined in makefile. Am will guide automake to generate specified code. For example, macro bin_programs will generate the compilation and connection targets.
5. Run automake
Command:
$ Automake -- add-Missing
Configure. In: Installing './install-Sh'
Configure. In: Installing './mkinstalldirs'
Configure. In: Installing './missing'
Makefile. AM: Installing './depcomp'