makefile notes - implicit rules

Implicit rules are predefined rules that make

Cancel implicit rules

Make options "-r" or "--no-builtin-rules

List of implicit rules

Implicit rules compiled C programs.
  • Dependent destination "<n> .o" target automatically derived as "<n> .c", and generates a command which is "(CPPFLAGS) $ (CFLAGS)"
Implicit rule to compile C ++ programs.
  • Dependency "<n> .o" target as the target automatically derive "<n> .cc" or "<n> .C", and generates a command which is "(CPPFLAGS) $ (CFLAGS)". (Recommended ".cc" as a suffix C ++ source files, rather than ".C")
Pascal compiler implicit rules of procedure.
  • Dependency "<n> .o" target as the target automatically derive "<n> .p", and generates a command which is "(PFLAGS)".
Compile Fortran / implicit rules Ratfor program.
  • "<N> .o" target dependent destination is automatically derived as "<n> .r" or "<n> .F" or "<n> .f", and generates a command which is:
    • “.f” “(FFLAGS)”
    • “.F” “(FFLAGS) $(CPPFLAGS)”
    • “.f” “(FFLAGS) $(RFLAGS)”
Pretreatment implicit rules Fortran / Ratfor program.
  • Dependent destination "<n> .f" target automatically derived as "<n> .r" or "<n> .F". This rule simply convert Ratfor or Fortran program to a pretreatment standard Fortran program. Ordered its use are:
    • “.F” “(CPPFLAGS) $(FFLAGS)”
    • “.r” “(FFLAGS) $(RFLAGS)”
Modula-2 compiler implicit rules of procedure.
  • Dependent destination "<n> .sym" target automatically derived as "<n> .def", and generates a command which is: "(M2FLAGS) $ (DEFFLAGS)".
  • Dependent destination "<no>" target automatically derived as "<n> .mod", and generates a command which is: "(M2FLAGS) $ (MODFLAGS)".
Compilation and assembly implicit rules pretreatment.
  • Dependent destination "<n> .o" target automatically derived as "<n> .s", compiled default product "as", and generates a command which is: "(ASFLAGS)".
  • Dependent destination "<n> .s" target automatically derived as "<n> .S", default C precompiler "cpp", and generates a command which is: "(ASFLAGS)".
Implicit rules link Object file.
  • "<N>" target depends on the "<n> .o", is run by running the C compiler generates a linker (usually "ld"), which generates a command is: "(LDFLAGS) <n> .o ( LDLIBS) ". This rule works only for a valid source file, but also on more Object files (generated by a different source files) are also effective.
The implicit rules of Yacc C program.
  • "<N> .c" dependency file is automatically derived as "ny" (Yacc generated file), generates a command which is: "(YFALGS)". ( "Yacc" is a parser, please see the details on the relevant information)
The implicit rules of Lex C program.

"<N> .c" dependency file is automatically derived as "nl" (Lex generated file), generates a command which is: "(LFALGS)". (Details on the "Lex", please see the relevant information)

Lex Ratfor implicit rules of procedure.
  • "<N> .r" dependency file is automatically derived as "nl" (Lex generated file), generates a command which is: "(LFALGS)".
From a C program, Yacc Lex file or files created implicit rules Lint library.
  • "<N> .ln" (lint generated file) dependent files are automatically derived as "nc", which command is generated: "(LINTFALGS) $ (CPPFLAGS) -i". For "<n> .y" and "<n> .l" The same rules.

Implicit variables used by the rule

In the implicit rules of command, basically used some variables set in advance. You can change your makefile in the value of these variables, or pass these values ​​make the command line, or set these values ​​in your environment variables, no matter how, as long as these particular set of variables, then it will act on implicit rules. Of course, you can also make use of the "-R" or "--no-builtin-variables" parameter to cancel the effect of variable you are defining implicit rules.

For example, the first implicit rule - command implicit rules compiled C program is "(CFLAGS) (CC)" re-defined as "gcc", the variable "(CPPFLAGS)" looks to be executed.

We can use variables implicit rules is divided into two: one is related commands, such as "CC"; phase one is related parameters, such as "CFLAGS". Here are the variables of all implicit rules will be used:

  • Variables on command.

  • AR
    library Packager. Default command is "ar".

  • AS
    assembly language compiler. The default order is "as".

  • CC
    C language compiler. Default command is "cc".

  • CXX
    C ++ language compiler. Default command is "g ++".

  • CO
    extension file program file from the RCS. Default command is "co".

  • The CPP
    C preprocessor program (output standard output device). Default command is "$ (CC) -E".

  • The FC
    the Fortran compiler and Ratfor and preprocessor. Default command is "f77".

  • GET
    program file extension from the SCCS file. The default order is "get".

  • LEX
    Lex method analyzer program (for the C or Ratfor). Default command is "lex".

  • PC
    Pascal language compiler. Default command is "pc".

  • YACC
    Yacc grammar analyzer (for C programs). Default command is "yacc".

  • YACCR
    Yacc grammar analyzer (for in Ratfor program). Default command is "yacc -r".

  • MAKEINFO
    convert Texinfo source file (.texi) Info files to the program. Default command is "makeinfo".

  • TEX
    create TeX DVI files from TeX source file program. Default command is "tex".

  • TEXI2DVI
    create programs army TeX DVI files from Texinfo source file. Default command is "texi2dvi".

  • WEAVE
    convert the Web program to TeX. Default command is "weave".

  • CWEAVE
    convert C Web into TeX program. Default command is "cweave".

  • TANGLE
    converter Web to Pascal languages. Default command is "tangle".

  • CTANGLE
    converted to C. C Web Default command is "ctangle".

  • RM
    delete file command. Default command is "rm -f".

  • Variable parameters on the command

These variables are the following parameters related to the above command. If you do not specify a default value, then its default values ​​are empty.

  • ARFLAGS
    library packed program parameters AR command. The default value is "rv".
  • ASFLAGS
    assembly language compiler arguments. (When you call ".s" or ".S" files obviously).
  • CFLAGS
    C compiler parameters.
  • CXXFLAGS
    C ++ compiler parameters.
  • COFLAGS
    RCS command parameters.
  • The CPPFLAGS
    C preprocessor parameters. (C and Fortran compilers will be used).
  • FFLAGS
    Fortran compiler parameters.
  • GFLAGS
    SCCS "GET" program parameters.
  • LDFLAGS
    linker parameters. (Eg: "ld")
  • LFLAGS
    Lex grammar parameter analyzer.
  • Pflags
    Pascal language compiler parameters.
  • RFLAGS
    Fortran compiler argument Ratfor program.
  • YFLAGS
    Yacc grammar parameter analyzer.

Implicit rules chain

Sometimes, a goal might be the role of a series of implicit rules. For example, a [.o] file generation may be the first to be Yacc [.y] files into the first [.c], then the compiler generated C. We put a series of implicit rules is called "implicit rule chain."

In the above example, if the file [.c] exist, then directly call the implicit rules of the C compiler, if not [.c] file, but there is a [.y] file, then the implicit rule Yacc It will be called to generate [.c] file, and then call the C compiler's implicit rules ultimately generated by the [.c] [.o] file, to achieve the goal.

We call this [.c] file (or target), called intermediate target.

By default, for the intermediate target, its general objectives and there are two places is different: The first difference is that unless the middle of the target does not exist, the rules will lead to the middle. The second difference is that, as long as the target successfully generated, then produce the final goal during the middle of the target file will be generated with "rm -f" delete.

Typically, a makefile is designated as a target or target-dependent files can not be regarded as an intermediary. However, you can clearly explain a file or intermediate target goal is, you can use the pseudo-objective ".INTERMEDIATE" to force a statement. (Eg: .INTERMEDIATE: mid)

You can also make stop automatically delete the intermediate target, to do this, you can use the pseudo-objective ".SECONDARY" to force statement (such as: .SECONDARY: sec). You can put your goal, in the manner specified mode (e.g.,:.% O) to pseudo target ".PRECIOUS" is dependent destination to save intermediate file is generated by implicit rules.

In the "implicit rule chain", prohibiting the same target appears two or more times, this way, you can prevent infinite recursion when make automatic derivation.

Make optimizes some special implicit rules, without generating intermediate files. E.g., from the file "foo.c" generated object program "foo", stands to reason, the make file compiles to generate an intermediate "foo.o", then link to the "foo", but in reality, this action can be a "cc" command completion (cc -o foo foo.c), then optimized rule will not generate intermediate files.

Reference material

Linux makefile tutorial is very detailed and easy to understand


ps: If you have questions and would like to welcome the king of mutual encouragement exchange


Reproduced in: https: //www.jianshu.com/p/755f27ecad9e

Guess you like

Origin blog.csdn.net/weixin_33860528/article/details/91306912