close
這是讀"Managing Projects with GNU Make"的筆記,make真的是非常的有趣呀。

1.1 Targets and Prerequisites
  • A rule consists of three parts: the target, its prerequisites, and the command(s) to perform: One or more targets appear to the left of the colon and zero or more prerequisites can appear to the right of the colon.
target1 target2 target3: prereq1 prereq2
[tab]commands1
[tab]commands1

  • The target is the file or thing that must be made. The prerequisites or dependents are those files that must exist before the target can be successfully created. And the commands are those shell commands that will create the target from the prerequisites, commands have to be preceded by a tab character.
  • When make is asked to evaluate a rule, it begins by finding the files indicated by the prerequisites and target. If any of the prerequisites has an associated rule, make attempts to update those first. Next, the target file is considered. If any prerequisite is newer than the target, the target is remade by executing the commands.
  • Each command line is passed to the shell and is executed in its own subshell.
1.2 Dependency Checking
  • A makefle
count_words: count_words.o lexer.o –lfl
           gcc count_words.o lexer.o -lfl -ocount_words
count_words.o: count_words.c
           gcc -c count_words.c
lexer.o: lexer.c
           gcc -c lexer.c
lexer.c: lexer.l
           flex -t lexer.l > lexer.c
  • make will check first prerequisites are count_word.o, lexer.o, and -lfl, then find count_word.o comes from second rule, execute second rule, make then check second prerequisite lexer.o should be satisfied from the third rule, then again find third prerequisite lexer.c should be satisfied from the fourth rule.
  • The target of one rule can be referenced as a prerequisite in another rule, the set of targets and prerequisites form a chain or graph of dependencies (short for "dependency graph").
1.3 Minimizing Rebuilds
  • make will check if the target exists and its timestamp (mtime) is newer than all its prerequisites, then this target will not be build again.
  • atime stands for latest read time, ctime stands for latest mode change time, mtime stands for last write time.
1.4 Invoking make
  • $ make [target] /* If the makefile (makefile, Makefile, or GNUMakefile) resides in the user's current directory */
  • When make is executed, it will read the description file and identify the target to be updated. If the target or any of its prerequisite files are out of date (or missing) the shell commands in the rule's command script will be executed one at a time. After the commands are run make assumes the target is up to date and moves on to the next target or exits.
1.5 Basic Makefile Syntax
  • Makefiles are usually structured top-down.
  • Each command must begin with a tab character. This (obscure) syntax tells make that the characters that follow the tab are to be passed to a subshell for execution.
  • The comment character for make is the hash or pound sign, all text from the pound sign to the end of line is ignored.
  • Long lines can be continued using the standard Unix escape character backslash (\).

arrow
arrow
    全站熱搜

    nix 發表在 痞客邦 留言(0) 人氣()