Makefile Detailed--implied rule (go)Makefile Series article, here is a predecessor to wash a series to introduce, a total of 26 blog posts. http://www.cppblog.com/ivenher/archives/2007/04.htmlMakefile Implicit rules ———— when we use makefile, there are things we use very often, and we use very high frequency, for example, we compile C/c++The source program is the intermediate target file (under UNIX is the [. O] File, under Windows is the [. obj] file). This chapter deals with some of the "hidden", previously agreed upon makefile rules that we do not need to write again. The "implied rule" is also a convention in which make will run in a manner that is "customary", fearing that no such rule is written in our makefile. For example, if you compile the [. c] File into the [. O] File, you don't have to write it, and make automatically deduces the rule and generates the [. o] File we need. "Implied rules" use some of our system variables, and we can change the values of these system variables to customize the runtime parameters of the implied rules. such as the system variable "CFLAGS" can control compile-time compiler parameters. We can also write down our own implicit rules by means of "pattern rules". There are a number of limitations to defining implied rules with the suffix rule. The use of "pattern rules" is more intelligent and clear, but "suffix rules" can be used to guarantee our makefile compatibility. We understand the "hidden rules" that can make them better for us and will let us know something that is "conventional," rather than making us run makefile with something that we find baffling. Of course, anything is contradictory, water can carry a boat, but also overturn it, so, sometimes "hidden rules" will give us a little trouble. Only by understanding it can we use it better. The use of implicit rules if you want to use the implicit rules to generate the desired target, all you need to do is not to write the rules of this goal. So, make will try to automatically derive the rules and commands that produce this goal, and if make can automatically deduce the rules and commands that generate the target, then this behavior is the automatic derivation of the implied rules. The implicit rule, of course, is something that make is agreed on in advance. For example, we have the following one makefile:foo:foo.o bar.o cc–o foo foo.o bar.o $ (CFLAGS) $ (ldflags) We can notice that this makefile does not write down how to live Into FOO.O and BAR.O the two goals of the rules and orders. Because the "implied rules" feature of make automatically automatically infers the dependent targets and build commands for these two targets. Make will look for available rules in its own "hidden rules" library, and if found, it will be used. If you can't find it, you'll get an error. In the above example, the implicit rule of make invocation is to place the dependent file [. c] of the target of [. O], and use C's compile command "Cc–c $ (CFLAGS) [. c]" to generate the [. O] Target. SoIs that we have absolutely no need to write down the following two rules: FOO.O:FOO.C cc–c foo.c $ (CFLAGS) bar.o:bar.c cc–c bar.c $ (CFLAGS) because, this It's already a "pact." Okay, make and we have a deal. The rule for generating [. O] Files with the C compiler "CC" is the implied rule. Of course, if we write our own rules for the [. O] File, then make does not automatically deduce and invoke the implied rules, which are faithfully executed according to the rules we have written. Also, in the "Hidden Rules library" of make, each implied rule has its order in the library, and the more forward it is, the more often it is used, so this will cause us to have some time even if we specify the target dependency, and make does not control. As the following rule (no command): The FOO.O:FOO.P dependent file "FOO.P" (the Pascal program's source file) may become meaningless. If the "foo.c" file exists under the directory, then our implied rules will take effect, and the foo.o file will be generated by the compiler of "FOO.C" Call C. Because, in the implied rules, Pascal's rules appear after the C rule, so make finds the rule that generates FOO.O C and no longer looks for the next rule. If you do not want any implied rules to be deduced, then you should not write only "dependent rules" without writing commands. Ii. list of implied rules here we will cover all the implicit rules of pre-Setup (that is, make built-in), and if we do not explicitly write the rules, then make will look for the rules and commands required in these rules. Of course, we can also use the Make parameter "-R "or"--no-builtin-"rules" option to cancel all pre-set implied rules. Of course, even if we specify the "-r "parameter, some implied rules are still in effect, because there are many implied rules that are defined using the" suffix rule ", so long as there is a" suffix list "in the implied rule (also on a system defined in the target. suffixes), then the implied rules will take effect. The default list of suffixes is:. out,. A,. LN,. O,. C,. CC,. C,. P,. F,. F,. R,. Y,. L,. S,. S,. MoD,. Sym,. def,. h,. info,. DVI,. Tex,. Texinfo,. Texi,. Txinfo,. W,. Ch. Web,. SH,. ELC,. El. We will tell you the details in detail later. Let's take a look at the usual hidden rules. 1, compiling the implicit rules for C programs. "<n>;. O "targets are automatically deduced as" <n>;. C "and its build command is" $ (CC) –c $ (cppflags) $ (CFLAGS) "2, compiling C + +the implicit rules of the program. "<n>;. O "targets of the target will be automatically deduced as" <N>;. CC "or" <N>;. C "and its build command is" $ (CXX) –c $ (cppflags) $ (CFLAGS) ". (It is recommended to use ". CC" as C + +The suffix of the source file instead of ". C ")3, compiling the implied rules of the Pascal program. "<n>;. O "targets are automatically deduced as" <n>;. P "and its build command is" $ (PC) –c $ (pflags) ". 4, compiling fortran/ratfor The implicit rules of the program. "<n>;. O "targets of the target will be automatically deduced as" <N>;. R "or" <N>;. F "or" <n>;. F ", and its generated command is:". F "" $ (FC) –c $ (fflags) "". F "" $ (FC) –c $ (fflags) $ (cppflags) "". F "" $ (FC) –c $ (fflags) $ (rflags) "5, pretreatment fortran/ratfor The implicit rules of the program. "<n>;. F "Goal of the target will be automatically deduced as" <N>;. R "or" <n>;. F ". This rule simply converts ratfor or has a pre-processed FORTRAN program to a standard FORTRAN program. The command it uses is: ". F "" $ (FC) –F $ (cppflags) $ (fflags) "". R "" $ (FC) –F $ (fflags) $ (rflags) "6, compiling modula-2 implied rules of the program. "<n>;. Sym "Target will be automatically deduced as" <N>;. Def "and its build command is:" $ (M2C) $ (m2flags) $ (defflags) ". The target of "<n.o>;" is automatically deduced as "<n>;. MoD "and its Build command is:" $ (M2C) $ (m2flags) $ (modflags) ". 7, assembly and compilation of the implicit rules of preprocessing. "<n>;. O "targets of the target will be automatically deduced as" <N>;. S ", default use of the compiled product" as"and its build command is:" $ (AS) $ (asflags) ". "<N>;. S "targets are automatically deduced as" <n>;. S ", the C pre-compiler" CPP "is used by default, and its build command is:" $ (AS) $ (asflags) ". 8, and the implied rules for linking the object file. "<n>; " The target relies on the "<N>;. O ", run the linker build (typically" LD ") by running the C compiler, whose generated command is:" $ (CC) $ (ldflags) <n>;. o $ (loadlibes) $ (ldlibs) ". This rule is valid for projects with only one source file, and also for multiple object files (generated by different source files). For example, the following rule: x:y.o z.o and "x.c", "y.c", and "z.c" are present, the implied rule executes the following command: CC-C X.C-o x.o cc-C Y.C-o y.o cc-C Z.C-o z.o cc x.o y.o z.o-o x RM-F X.O RM-F y.o RM-F Z.O If you do not have a source file (such as x.c in the previous example) associated with your target name (as in the example above), then you'd better write your own build rule, otherwise the implied rule will report an error. 9, the YACC C program's implicit rules. "<n>;. C "Dependent file is automatically deduced as" N.Y "(YACC generated file), and its generated command is:" $ (YACC) $ (YFALGS) ". ("Yacc" is a parser, for details please see the relevant information)Ten, and Lex C program's implicit rules. "<n>;. C "Dependent file is automatically deduced as" N.L "(Lex generated file), and its generated command is:" $ (Lex) $ (LFALGS) ". (For details on "Lex", please refer to the relevant information) One, Lex ratfor The program's implicit rules. "<n>;. R "'s dependent file is automatically deduced as" N.L "(the lex generated file) and its generated command is:" $ (Lex) $ (LFALGS) ". A, create an implicit rule for the lint library from a C program, a YACC file, or a lex file. "<n>;. The dependent file for ln "(lint generated file) is automatically deduced as" N.C "and its generated command is:" $ (lint) $ (LINTFALGS) $ (cppflags)-i ". for "<N>;. Y "and" <n>;. L "is the same rule. The variables used in the implied rules are basically used in the commands in the implied rules, with some pre-set variables. You can change the values of these variables in your makefile, either by passing them on the command line of make, or by setting them in your environment variables, however, if you set these specific variables, they will work on the implied rules. Of course, you can also use make's "-R "or"--no–builtin-variables "parameter to suppress the effect of the variable you have defined on the implied rule. For example, the first implied rule--the command to compile an implied rule for a C program is "$ (CC) –c $ (CFLAGS) $ (cppflags)". Make the default compile command "CC", if you redefine the variable "$ (cc)" to "GCC", the variable "$ (CFLAGS)" is redefined as "-G ", then, all commands in the implied rules will be" gcc–c-G $ (cppflags) "Looks like it's done. We can divide the variables used in the implied rules into two kinds: one is command-related, such as "CC", and the other is the close of the parameter phase, such as "CFLAGS". Here are the variables that are used in all implied rules:1, a variable about the command. AR function Library Wrapper. The default command is "AR". As assembly language compiler. The default command is " as". CC C language Compiler program. The default command is "CC". CXX C+ + language Compiler program. The default command is "g++". The CO extends the file program from the RCS file. The default command is "CO". The preprocessor for the CPP C program (output is a standard output device). The default command is "$ (CC) –E". FC Fortran and Ratfor compilers and pre-processing programs. The default command is "F77". GET The program that extends the file from the SCCs file. The default command is "Get". Lex Lex Method Parser program (for C or ratfor). The default command is "Lex". PC Pascal Language Compiler program. The default command is "PC". YACC YACC Grammar Analyzer (for C programs). The default command is "YACC". YACCR YACC Grammar Analyzer (for ratfor programs). The default command is "Yacc–r". Makeinfo Convert the Texinfo source file (. texi) to the info file program. The default command is "Makeinfo". Tex a program for creating Tex-DVI files from Tex source files. The default command is "Tex". Texi2dvi the program to create the Army Tex DVI file from the Texinfo source file. The default command is "Texi2dvi". WEAVE Convert Web to Tex program. The default command is "weave". Cweave convert C Web to Tex program. The default command is "Cweave". Tangle convert Web to Pascal language program. The default command is "Tangle". Ctangle convert C Web to C. The default command is "Ctangle". RM Delete File command. The default command is "Rm–f". 2, the variables below the variables for the command arguments are the arguments for the command above. If the default value is not specified, the default value is null. Arflags the parameters of the function Library Packager ar command. The default value is "rv". Asflags assembly language compiler parameters. (when obviously called ". S" or ". S "file). CFLAGS C language Compiler parameters. Cxxflags C++language compiler parameters. Coflags the RCS command parameter. Cppflags C preprocessor parameters. (The C and Fortran compilers are also used). Fflags Fortran language compiler parameters. GFLAGS SCCS "Get"program parameters. LDFLAGS the linker parameters. (e.g., "LD") lflags Lex Grammar Analyzer parameter. Pflags Pascal language compiler parameter. Rflags the Fortran compiler parameter of the RATFOR program. Yflags YACC Grammar Analyzer parameters. Iv. implied chain of rules sometimes, a goal may be a series of implicit rules to the effect. For example, a [. O] File generation may be preceded by the [. Y] File First YACC [. c] and then generated by the C compiler. We call this series of implicit rules "chain of implied rules". In the example above, if the file [. c] exists, then the implicit rule of C's compiler is called directly, if there is no [. c] File, but there is a [. y] File, then the implied rule of YACC is called, the [. c] File is generated, and then the implicit rule of C compilation is eventually generated [. O] ] file to reach the target. We call this [. c] File (or target) the intermediate target. In any case, make will try to automatically deduce all the methods of generating the target, regardless of the number of intermediate goals, it will persist in all the implied rules and the rules you write all together to analyze and strive to achieve the goal, so, sometimes it may make you feel strange, how my goal will be generated? Why is my makefile crazy? By default, for intermediate targets, it differs from the general target in two places: the first difference is that the intermediate rule is raised unless the intermediate target does not exist. The second difference is that, as long as the goal is successful, the resulting intermediate target file will be "rm-F "Delete. Typically, a file that is designated as a target or dependent by makefile cannot be used as an intermediary. However, you can clearly state that a file or target is an intermediary target, and you can use pseudo-target ". Intermediate "to enforce the declaration. (such as:. INTERMEDIATE:MID) You can also prevent make to automatically delete intermediate targets, to do this, you can use pseudo target ". Secondary "to enforce the declaration (for example:. SECONDARY:SEC). You can also put your goals in a pattern way to specify (for example:%O) into pseudo-target ". Precious "To save the intermediate file generated by the implied rule. In the chain of implied rules, it is forbidden to show the same target two or more times, or more than two times, which prevents infinite recursion when make is automatically deduced. Make optimizes some special implicit rules without generating intermediate files. For example, from the file "foo.c" to generate the target program "Foo", according to the truth, make will compile to generate intermediate file "FOO.O", and then link to "foo", but in practice, this action can be a "CC" command to complete (cc–o foo FOO.C), then the optimized rule will not generate intermediate files.
Makefile--implied rules