home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / GNU_C++ / LIB / MAKE3761.LZH / info / make.info-1 next >
Encoding:
GNU Info File  |  1998-07-20  |  50.6 KB  |  1,130 lines

  1. This is Info file make.info, produced by Makeinfo version 1.67 from the
  2. input file make.texinfo.
  3.  
  4. INFO-DIR-SECTION The GNU make utility
  5. START-INFO-DIR-ENTRY
  6. * GNU make: (make.info).           The GNU make utility.
  7. END-INFO-DIR-ENTRY
  8.  
  9.    This file documents the GNU Make utility, which determines
  10. automatically which pieces of a large program need to be recompiled,
  11. and issues the commands to recompile them.
  12.  
  13.    This is Edition 0.51, last updated 26 Aug 1997, of `The GNU Make
  14. Manual', for `make', Version 3.76 Beta.
  15.  
  16.    Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97     Free
  17. Software Foundation, Inc.
  18.  
  19.    Permission is granted to make and distribute verbatim copies of this
  20. manual provided the copyright notice and this permission notice are
  21. preserved on all copies.
  22.  
  23.    Permission is granted to copy and distribute modified versions of
  24. this manual under the conditions for verbatim copying, provided that
  25. the entire resulting derived work is distributed under the terms of a
  26. permission notice identical to this one.
  27.  
  28.    Permission is granted to copy and distribute translations of this
  29. manual into another language, under the above conditions for modified
  30. versions, except that this permission notice may be stated in a
  31. translation approved by the Free Software Foundation.
  32.  
  33. File: make.info,  Node: Top,  Next: Overview,  Up: (dir)
  34.  
  35. Make
  36. ****
  37.  
  38.    The GNU `make' utility automatically determines which pieces of a
  39. large program need to be recompiled, and issues the commands to
  40. recompile them.
  41.  
  42.    This is Edition 0.51 of the `GNU Make Manual', last updated 26 Aug
  43. 1997 for `make' Version 3.76 Beta.
  44.  
  45.    This manual describes `make' and contains the following chapters:
  46.  
  47. * Menu:
  48.  
  49. * Overview::                    Overview of `make'.
  50. * Introduction::                An introduction to `make'.
  51. * Makefiles::                   Makefiles tell `make' what to do.
  52. * Rules::                       Rules describe when a file must be remade.
  53. * Commands::                    Commands say how to remake a file.
  54. * Using Variables::             You can use variables to avoid repetition.
  55. * Conditionals::                Use or ignore parts of the makefile based
  56.                                  on the values of variables.
  57. * Functions::                   Many powerful ways to manipulate text.
  58. * make Invocation: Running.     How to invoke `make' on the command line.
  59. * Implicit Rules::              Use implicit rules to treat many files alike,
  60.                                  based on their file names.
  61. * Archives::                    How `make' can update library archives.
  62. * Features::                    Features GNU `make' has over other `make's.
  63. * Missing::                     What GNU `make' lacks from other `make's.
  64. * Makefile Conventions::        Conventions for makefiles in GNU programs.
  65. * Quick Reference::             A quick reference for experienced users.
  66. * Complex Makefile::            A real example of a straightforward,
  67.                                  but nontrivial, makefile.
  68. * Concept Index::               Index of Concepts
  69. * Name Index::                  Index of Functions, Variables, & Directives
  70.  
  71.  -- The Detailed Node Listing --
  72.  
  73. Overview of `make'
  74.  
  75. * Preparing::                   Preparing and Running Make
  76. * Reading::                     On Reading this Text
  77. * Bugs::                        Problems and Bugs
  78.  
  79. An Introduction to Makefiles
  80.  
  81. * Rule Introduction::           What a rule looks like.
  82. * Simple Makefile::             A Simple Makefile
  83. * How Make Works::              How `make' Processes This Makefile
  84. * Variables Simplify::          Variables Make Makefiles Simpler
  85. * make Deduces::                Letting `make' Deduce the Commands
  86. * Combine By Dependency::       Another Style of Makefile
  87. * Cleanup::                     Rules for Cleaning the Directory
  88.  
  89. Writing Makefiles
  90.  
  91. * Makefile Contents::           What makefiles contain.
  92. * Makefile Names::              How to name your makefile.
  93. * Include::                     How one makefile can use another makefile.
  94. * MAKEFILES Variable::          The environment can specify extra makefiles.
  95. * Remaking Makefiles::          How makefiles get remade.
  96. * Overriding Makefiles::        How to override part of one makefile
  97.                                  with another makefile.
  98.  
  99. Writing Rules
  100.  
  101. * Rule Example::                An example explained.
  102. * Rule Syntax::                 General syntax explained.
  103. * Wildcards::                   Using wildcard characters such as `*'.
  104. * Directory Search::            Searching other directories for source files.
  105. * Phony Targets::               Using a target that is not a real file's name.
  106. * Force Targets::               You can use a target without commands
  107.                                   or dependencies to mark other
  108.                                   targets as phony.
  109. * Empty Targets::               When only the date matters and the
  110.                                   files are empty.
  111. * Special Targets::             Targets with special built-in meanings.
  112. * Multiple Targets::            When to make use of several targets in a rule.
  113. * Multiple Rules::              How to use several rules with the same target.
  114. * Static Pattern::              Static pattern rules apply to multiple targets
  115.                                   and can vary the dependencies according to
  116.                                   the target name.
  117. * Double-Colon::                How to use a special kind of rule to allow
  118.                                   several independent rules for one target.
  119. * Automatic Dependencies::      How to automatically generate rules giving
  120.                                  dependencies from the source files themselves.
  121.  
  122. Using Wildcard Characters in File Names
  123.  
  124. * Wildcard Examples::           Several examples
  125. * Wildcard Pitfall::            Problems to avoid.
  126. * Wildcard Function::           How to cause wildcard expansion where
  127.                                   it does not normally take place.
  128.  
  129. Searching Directories for Dependencies
  130.  
  131. * General Search::              Specifying a search path that applies
  132.                                   to every dependency.
  133. * Selective Search::            Specifying a search path
  134.                                   for a specified class of names.
  135. * Search Algorithm::            When and how search paths are applied.
  136. * Commands/Search::             How to write shell commands that work together
  137.                                   with search paths.
  138. * Implicit/Search::             How search paths affect implicit rules.
  139. * Libraries/Search::            Directory search for link libraries.
  140.  
  141. Static Pattern Rules
  142.  
  143. * Static Usage::                The syntax of static pattern rules.
  144. * Static versus Implicit::      When are they better than implicit rules?
  145.  
  146. Writing the Commands in Rules
  147.  
  148. * Echoing::                     How to control when commands are echoed.
  149. * Execution::                   How commands are executed.
  150. * Parallel::                    How commands can be executed in parallel.
  151. * Errors::                      What happens after a command execution error.
  152. * Interrupts::                  What happens when a command is interrupted.
  153. * Recursion::                   Invoking `make' from makefiles.
  154. * Sequences::                   Defining canned sequences of commands.
  155. * Empty Commands::              Defining useful, do-nothing commands.
  156.  
  157. Recursive Use of `make'
  158.  
  159. * MAKE Variable::               The special effects of using `$(MAKE)'.
  160. * Variables/Recursion::         How to communicate variables to a sub-`make'.
  161. * Options/Recursion::           How to communicate options to a sub-`make'.
  162. * -w Option::                   How the `-w' or `--print-directory' option
  163.                                  helps debug use of recursive `make' commands.
  164.  
  165. How to Use Variables
  166.  
  167. * Reference::                   How to use the value of a variable.
  168. * Flavors::                     Variables come in two flavors.
  169. * Advanced::                    Advanced features for referencing a variable.
  170. * Values::                      All the ways variables get their values.
  171. * Setting::                     How to set a variable in the makefile.
  172. * Appending::                   How to append more text to the old value
  173.                                   of a variable.
  174. * Override Directive::          How to set a variable in the makefile even if
  175.                                   the user has set it with a command argument.
  176. * Defining::                    An alternate way to set a variable
  177.                                   to a verbatim string.
  178. * Environment::                 Variable values can come from the environment.
  179. * Automatic::                   Some special variables have predefined
  180.                                   meanings for use with implicit rules.
  181.  
  182. Advanced Features for Reference to Variables
  183.  
  184. * Substitution Refs::           Referencing a variable with
  185.                                   substitutions on the value.
  186. * Computed Names::              Computing the name of the variable to refer to.
  187.  
  188. Conditional Parts of Makefiles
  189.  
  190. * Conditional Example::         Example of a conditional
  191. * Conditional Syntax::          The syntax of conditionals.
  192. * Testing Flags::               Conditionals that test flags.
  193.  
  194. Functions for Transforming Text
  195.  
  196. * Syntax of Functions::         How to write a function call.
  197. * Text Functions::              General-purpose text manipulation functions.
  198. * File Name Functions::         Functions for manipulating file names.
  199. * Foreach Function::            Repeat some text with controlled variation.
  200. * Origin Function::             Find where a variable got its value.
  201. * Shell Function::              Substitute the output of a shell command.
  202.  
  203. How to Run `make'
  204.  
  205. * Makefile Arguments::          How to specify which makefile to use.
  206. * Goals::                       How to use goal arguments to specify which
  207.                                   parts of the makefile to use.
  208. * Instead of Execution::        How to use mode flags to specify what
  209.                                   kind of thing to do with the commands
  210.                                   in the makefile other than simply
  211.                                   execute them.
  212. * Avoiding Compilation::        How to avoid recompiling certain files.
  213. * Overriding::                  How to override a variable to specify
  214.                                   an alternate compiler and other things.
  215. * Testing::                     How to proceed past some errors, to
  216.                                   test compilation.
  217. * Options Summary::             Summary of Options
  218.  
  219. Using Implicit Rules
  220.  
  221. * Using Implicit::              How to use an existing implicit rule
  222.                                   to get the commands for updating a file.
  223. * Catalogue of Rules::          A list of built-in implicit rules.
  224. * Implicit Variables::          How to change what predefined rules do.
  225. * Chained Rules::               How to use a chain of implicit rules.
  226. * Pattern Rules::               How to define new implicit rules.
  227. * Last Resort::                 How to defining commands for rules
  228.                                   which cannot find any.
  229. * Suffix Rules::                The old-fashioned style of implicit rule.
  230. * Implicit Rule Search::        The precise algorithm for applying
  231.                                   implicit rules.
  232.  
  233. Defining and Redefining Pattern Rules
  234.  
  235. * Pattern Intro::               An introduction to pattern rules.
  236. * Pattern Examples::            Examples of pattern rules.
  237. * Automatic::                   How to use automatic variables in the
  238.                                   commands of implicit rules.
  239. * Pattern Match::               How patterns match.
  240. * Match-Anything Rules::        Precautions you should take prior to
  241.                                   defining rules that can match any
  242.                                   target file whatever.
  243. * Canceling Rules::             How to override or cancel built-in rules.
  244.  
  245. Using `make' to Update Archive Files
  246.  
  247. * Archive Members::             Archive members as targets.
  248. * Archive Update::              The implicit rule for archive member targets.
  249. * Archive Pitfalls::            Dangers to watch out for when using archives.
  250. * Archive Suffix Rules::        You can write a special kind of suffix rule
  251.                                   for updating archives.
  252.  
  253. Implicit Rule for Archive Member Targets
  254.  
  255. * Archive Symbols::             How to update archive symbol directories.
  256.  
  257. File: make.info,  Node: Overview,  Next: Introduction,  Prev: Top,  Up: Top
  258.  
  259. Overview of `make'
  260. ******************
  261.  
  262.    The `make' utility automatically determines which pieces of a large
  263. program need to be recompiled, and issues commands to recompile them.
  264. This manual describes GNU `make', which was implemented by Richard
  265. Stallman and Roland McGrath.  GNU `make' conforms to section 6.2 of
  266. `IEEE Standard 1003.2-1992' (POSIX.2).
  267.  
  268.    Our examples show C programs, since they are most common, but you
  269. can use `make' with any programming language whose compiler can be run
  270. with a shell command.  Indeed, `make' is not limited to programs.  You
  271. can use it to describe any task where some files must be updated
  272. automatically from others whenever the others change.
  273.  
  274. * Menu:
  275.  
  276. * Preparing::                   Preparing and Running Make
  277. * Reading::                     On Reading this Text
  278. * Bugs::                        Problems and Bugs
  279.  
  280. File: make.info,  Node: Preparing,  Next: Reading,  Up: Overview
  281.  
  282. Preparing and Running Make
  283. ==========================
  284.  
  285.    To prepare to use `make', you must write a file called the
  286. "makefile" that describes the relationships among files in your program
  287. and provides commands for updating each file.  In a program, typically,
  288. the executable file is updated from object files, which are in turn
  289. made by compiling source files.
  290.  
  291.    Once a suitable makefile exists, each time you change some source
  292. files, this simple shell command:
  293.  
  294.      make
  295.  
  296. suffices to perform all necessary recompilations.  The `make' program
  297. uses the makefile data base and the last-modification times of the
  298. files to decide which of the files need to be updated.  For each of
  299. those files, it issues the commands recorded in the data base.
  300.  
  301.    You can provide command line arguments to `make' to control which
  302. files should be recompiled, or how.  *Note How to Run `make': Running.
  303.  
  304. File: make.info,  Node: Reading,  Next: Bugs,  Prev: Preparing,  Up: Overview
  305.  
  306. How to Read This Manual
  307. =======================
  308.  
  309.    If you are new to `make', or are looking for a general introduction,
  310. read the first few sections of each chapter, skipping the later
  311. sections.  In each chapter, the first few sections contain introductory
  312. or general information and the later sections contain specialized or
  313. technical information.  The exception is the second chapter, *Note An
  314. Introduction to Makefiles: Introduction, all of which is introductory.
  315.  
  316.    If you are familiar with other `make' programs, see *Note Features
  317. of GNU `make': Features, which lists the enhancements GNU `make' has,
  318. and *Note Incompatibilities and Missing Features: Missing, which
  319. explains the few things GNU `make' lacks that others have.
  320.  
  321.    For a quick summary, see *Note Options Summary::, *Note Quick
  322. Reference::, and *Note Special Targets::.
  323.  
  324. File: make.info,  Node: Bugs,  Prev: Reading,  Up: Overview
  325.  
  326. Problems and Bugs
  327. =================
  328.  
  329.    If you have problems with GNU `make' or think you've found a bug,
  330. please report it to the developers; we cannot promise to do anything but
  331. we might well want to fix it.
  332.  
  333.    Before reporting a bug, make sure you've actually found a real bug.
  334. Carefully reread the documentation and see if it really says you can do
  335. what you're trying to do.  If it's not clear whether you should be able
  336. to do something or not, report that too; it's a bug in the
  337. documentation!
  338.  
  339.    Before reporting a bug or trying to fix it yourself, try to isolate
  340. it to the smallest possible makefile that reproduces the problem.  Then
  341. send us the makefile and the exact results `make' gave you.  Also say
  342. what you expected to occur; this will help us decide whether the
  343. problem was really in the documentation.
  344.  
  345.    Once you've got a precise problem, please send electronic mail either
  346. through the Internet or via UUCP:
  347.  
  348.      Internet address:
  349.          bug-gnu-utils@prep.ai.mit.edu
  350.      
  351.      UUCP path:
  352.          mit-eddie!prep.ai.mit.edu!bug-gnu-utils
  353.  
  354. Please include the version number of `make' you are using.  You can get
  355. this information with the command `make --version'.  Be sure also to
  356. include the type of machine and operating system you are using.  If
  357. possible, include the contents of the file `config.h' that is generated
  358. by the configuration process.
  359.  
  360. File: make.info,  Node: Introduction,  Next: Makefiles,  Prev: Overview,  Up: Top
  361.  
  362. An Introduction to Makefiles
  363. ****************************
  364.  
  365.    You need a file called a "makefile" to tell `make' what to do.  Most
  366. often, the makefile tells `make' how to compile and link a program.
  367.  
  368.    In this chapter, we will discuss a simple makefile that describes
  369. how to compile and link a text editor which consists of eight C source
  370. files and three header files.  The makefile can also tell `make' how to
  371. run miscellaneous commands when explicitly asked (for example, to remove
  372. certain files as a clean-up operation).  To see a more complex example
  373. of a makefile, see *Note Complex Makefile::.
  374.  
  375.    When `make' recompiles the editor, each changed C source file must
  376. be recompiled.  If a header file has changed, each C source file that
  377. includes the header file must be recompiled to be safe.  Each
  378. compilation produces an object file corresponding to the source file.
  379. Finally, if any source file has been recompiled, all the object files,
  380. whether newly made or saved from previous compilations, must be linked
  381. together to produce the new executable editor.
  382.  
  383. * Menu:
  384.  
  385. * Rule Introduction::           What a rule looks like.
  386. * Simple Makefile::             A Simple Makefile
  387. * How Make Works::              How `make' Processes This Makefile
  388. * Variables Simplify::          Variables Make Makefiles Simpler
  389. * make Deduces::                Letting `make' Deduce the Commands
  390. * Combine By Dependency::       Another Style of Makefile
  391. * Cleanup::                     Rules for Cleaning the Directory
  392.  
  393. File: make.info,  Node: Rule Introduction,  Next: Simple Makefile,  Up: Introduction
  394.  
  395. What a Rule Looks Like
  396. ======================
  397.  
  398.    A simple makefile consists of "rules" with the following shape:
  399.  
  400.      TARGET ... : DEPENDENCIES ...
  401.              COMMAND
  402.              ...
  403.              ...
  404.  
  405.    A "target" is usually the name of a file that is generated by a
  406. program; examples of targets are executable or object files.  A target
  407. can also be the name of an action to carry out, such as `clean' (*note
  408. Phony Targets::.).
  409.  
  410.    A "dependency" is a file that is used as input to create the target.
  411. A target often depends on several files.
  412.  
  413.    A "command" is an action that `make' carries out.  A rule may have
  414. more than one command, each on its own line.  *Please note:* you need
  415. to put a tab character at the beginning of every command line!  This is
  416. an obscurity that catches the unwary.
  417.  
  418.    Usually a command is in a rule with dependencies and serves to
  419. create a target file if any of the dependencies change.  However, the
  420. rule that specifies commands for the target need not have dependencies.
  421. For example, the rule containing the delete command associated with the
  422. target `clean' does not have dependencies.
  423.  
  424.    A "rule", then, explains how and when to remake certain files which
  425. are the targets of the particular rule.  `make' carries out the
  426. commands on the dependencies to create or update the target.  A rule
  427. can also explain how and when to carry out an action.  *Note Writing
  428. Rules: Rules.
  429.  
  430.    A makefile may contain other text besides rules, but a simple
  431. makefile need only contain rules.  Rules may look somewhat more
  432. complicated than shown in this template, but all fit the pattern more
  433. or less.
  434.  
  435. File: make.info,  Node: Simple Makefile,  Next: How Make Works,  Prev: Rule Introduction,  Up: Introduction
  436.  
  437. A Simple Makefile
  438. =================
  439.  
  440.    Here is a straightforward makefile that describes the way an
  441. executable file called `edit' depends on eight object files which, in
  442. turn, depend on eight C source and three header files.
  443.  
  444.    In this example, all the C files include `defs.h', but only those
  445. defining editing commands include `command.h', and only low level files
  446. that change the editor buffer include `buffer.h'.
  447.  
  448.      edit : main.o kbd.o command.o display.o \
  449.             insert.o search.o files.o utils.o
  450.              cc -o edit main.o kbd.o command.o display.o \
  451.                         insert.o search.o files.o utils.o
  452.      
  453.      main.o : main.c defs.h
  454.              cc -c main.c
  455.      kbd.o : kbd.c defs.h command.h
  456.              cc -c kbd.c
  457.      command.o : command.c defs.h command.h
  458.              cc -c command.c
  459.      display.o : display.c defs.h buffer.h
  460.              cc -c display.c
  461.      insert.o : insert.c defs.h buffer.h
  462.              cc -c insert.c
  463.      search.o : search.c defs.h buffer.h
  464.              cc -c search.c
  465.      files.o : files.c defs.h buffer.h command.h
  466.              cc -c files.c
  467.      utils.o : utils.c defs.h
  468.              cc -c utils.c
  469.      clean :
  470.              rm edit main.o kbd.o command.o display.o \
  471.                 insert.o search.o files.o utils.o
  472.  
  473. We split each long line into two lines using backslash-newline; this is
  474. like using one long line, but is easier to read.
  475.  
  476.    To use this makefile to create the executable file called `edit',
  477. type:
  478.  
  479.      make
  480.  
  481.    To use this makefile to delete the executable file and all the object
  482. files from the directory, type:
  483.  
  484.      make clean
  485.  
  486.    In the example makefile, the targets include the executable file
  487. `edit', and the object files `main.o' and `kbd.o'.  The dependencies
  488. are files such as `main.c' and `defs.h'.  In fact, each `.o' file is
  489. both a target and a dependency.  Commands include `cc -c main.c' and
  490. `cc -c kbd.c'.
  491.  
  492.    When a target is a file, it needs to be recompiled or relinked if any
  493. of its dependencies change.  In addition, any dependencies that are
  494. themselves automatically generated should be updated first.  In this
  495. example, `edit' depends on each of the eight object files; the object
  496. file `main.o' depends on the source file `main.c' and on the header
  497. file `defs.h'.
  498.  
  499.    A shell command follows each line that contains a target and
  500. dependencies.  These shell commands say how to update the target file.
  501. A tab character must come at the beginning of every command line to
  502. distinguish commands lines from other lines in the makefile.  (Bear in
  503. mind that `make' does not know anything about how the commands work.
  504. It is up to you to supply commands that will update the target file
  505. properly.  All `make' does is execute the commands in the rule you have
  506. specified when the target file needs to be updated.)
  507.  
  508.    The target `clean' is not a file, but merely the name of an action.
  509. Since you normally do not want to carry out the actions in this rule,
  510. `clean' is not a dependency of any other rule.  Consequently, `make'
  511. never does anything with it unless you tell it specifically.  Note that
  512. this rule not only is not a dependency, it also does not have any
  513. dependencies, so the only purpose of the rule is to run the specified
  514. commands.  Targets that do not refer to files but are just actions are
  515. called "phony targets".  *Note Phony Targets::, for information about
  516. this kind of target.  *Note Errors in Commands: Errors, to see how to
  517. cause `make' to ignore errors from `rm' or any other command.
  518.  
  519. File: make.info,  Node: How Make Works,  Next: Variables Simplify,  Prev: Simple Makefile,  Up: Introduction
  520.  
  521. How `make' Processes a Makefile
  522. ===============================
  523.  
  524.    By default, `make' starts with the first target (not targets whose
  525. names start with `.').  This is called the "default goal".  ("Goals"
  526. are the targets that `make' strives ultimately to update.  *Note
  527. Arguments to Specify the Goals: Goals.)
  528.  
  529.    In the simple example of the previous section, the default goal is to
  530. update the executable program `edit'; therefore, we put that rule first.
  531.  
  532.    Thus, when you give the command:
  533.  
  534.      make
  535.  
  536. `make' reads the makefile in the current directory and begins by
  537. processing the first rule.  In the example, this rule is for relinking
  538. `edit'; but before `make' can fully process this rule, it must process
  539. the rules for the files that `edit' depends on, which in this case are
  540. the object files.  Each of these files is processed according to its
  541. own rule.  These rules say to update each `.o' file by compiling its
  542. source file.  The recompilation must be done if the source file, or any
  543. of the header files named as dependencies, is more recent than the
  544. object file, or if the object file does not exist.
  545.  
  546.    The other rules are processed because their targets appear as
  547. dependencies of the goal.  If some other rule is not depended on by the
  548. goal (or anything it depends on, etc.), that rule is not processed,
  549. unless you tell `make' to do so (with a command such as `make clean').
  550.  
  551.    Before recompiling an object file, `make' considers updating its
  552. dependencies, the source file and header files.  This makefile does not
  553. specify anything to be done for them--the `.c' and `.h' files are not
  554. the targets of any rules--so `make' does nothing for these files.  But
  555. `make' would update automatically generated C programs, such as those
  556. made by Bison or Yacc, by their own rules at this time.
  557.  
  558.    After recompiling whichever object files need it, `make' decides
  559. whether to relink `edit'.  This must be done if the file `edit' does
  560. not exist, or if any of the object files are newer than it.  If an
  561. object file was just recompiled, it is now newer than `edit', so `edit'
  562. is relinked.
  563.  
  564.    Thus, if we change the file `insert.c' and run `make', `make' will
  565. compile that file to update `insert.o', and then link `edit'.  If we
  566. change the file `command.h' and run `make', `make' will recompile the
  567. object files `kbd.o', `command.o' and `files.o' and then link the file
  568. `edit'.
  569.  
  570. File: make.info,  Node: Variables Simplify,  Next: make Deduces,  Prev: How Make Works,  Up: Introduction
  571.  
  572. Variables Make Makefiles Simpler
  573. ================================
  574.  
  575.    In our example, we had to list all the object files twice in the
  576. rule for `edit' (repeated here):
  577.  
  578.      edit : main.o kbd.o command.o display.o \
  579.                    insert.o search.o files.o utils.o
  580.              cc -o edit main.o kbd.o command.o display.o \
  581.                         insert.o search.o files.o utils.o
  582.  
  583.    Such duplication is error-prone; if a new object file is added to the
  584. system, we might add it to one list and forget the other.  We can
  585. eliminate the risk and simplify the makefile by using a variable.
  586. "Variables" allow a text string to be defined once and substituted in
  587. multiple places later (*note How to Use Variables: Using Variables.).
  588.  
  589.    It is standard practice for every makefile to have a variable named
  590. `objects', `OBJECTS', `objs', `OBJS', `obj', or `OBJ' which is a list
  591. of all object file names.  We would define such a variable `objects'
  592. with a line like this in the makefile:
  593.  
  594.      objects = main.o kbd.o command.o display.o \
  595.                insert.o search.o files.o utils.o
  596.  
  597. Then, each place we want to put a list of the object file names, we can
  598. substitute the variable's value by writing `$(objects)' (*note How to
  599. Use Variables: Using Variables.).
  600.  
  601.    Here is how the complete simple makefile looks when you use a
  602. variable for the object files:
  603.  
  604.      objects = main.o kbd.o command.o display.o \
  605.                insert.o search.o files.o utils.o
  606.      
  607.      edit : $(objects)
  608.              cc -o edit $(objects)
  609.      main.o : main.c defs.h
  610.              cc -c main.c
  611.      kbd.o : kbd.c defs.h command.h
  612.              cc -c kbd.c
  613.      command.o : command.c defs.h command.h
  614.              cc -c command.c
  615.      display.o : display.c defs.h buffer.h
  616.              cc -c display.c
  617.      insert.o : insert.c defs.h buffer.h
  618.              cc -c insert.c
  619.      search.o : search.c defs.h buffer.h
  620.              cc -c search.c
  621.      files.o : files.c defs.h buffer.h command.h
  622.              cc -c files.c
  623.      utils.o : utils.c defs.h
  624.              cc -c utils.c
  625.      clean :
  626.              rm edit $(objects)
  627.  
  628. File: make.info,  Node: make Deduces,  Next: Combine By Dependency,  Prev: Variables Simplify,  Up: Introduction
  629.  
  630. Letting `make' Deduce the Commands
  631. ==================================
  632.  
  633.    It is not necessary to spell out the commands for compiling the
  634. individual C source files, because `make' can figure them out: it has an
  635. "implicit rule" for updating a `.o' file from a correspondingly named
  636. `.c' file using a `cc -c' command.  For example, it will use the
  637. command `cc -c main.c -o main.o' to compile `main.c' into `main.o'.  We
  638. can therefore omit the commands from the rules for the object files.
  639. *Note Using Implicit Rules: Implicit Rules.
  640.  
  641.    When a `.c' file is used automatically in this way, it is also
  642. automatically added to the list of dependencies.  We can therefore omit
  643. the `.c' files from the dependencies, provided we omit the commands.
  644.  
  645.    Here is the entire example, with both of these changes, and a
  646. variable `objects' as suggested above:
  647.  
  648.      objects = main.o kbd.o command.o display.o \
  649.                insert.o search.o files.o utils.o
  650.      
  651.      edit : $(objects)
  652.              cc -o edit $(objects)
  653.      
  654.      main.o : defs.h
  655.      kbd.o : defs.h command.h
  656.      command.o : defs.h command.h
  657.      display.o : defs.h buffer.h
  658.      insert.o : defs.h buffer.h
  659.      search.o : defs.h buffer.h
  660.      files.o : defs.h buffer.h command.h
  661.      utils.o : defs.h
  662.      
  663.      .PHONY : clean
  664.      clean :
  665.              -rm edit $(objects)
  666.  
  667. This is how we would write the makefile in actual practice.  (The
  668. complications associated with `clean' are described elsewhere.  See
  669. *Note Phony Targets::, and *Note Errors in Commands: Errors.)
  670.  
  671.    Because implicit rules are so convenient, they are important.  You
  672. will see them used frequently.
  673.  
  674. File: make.info,  Node: Combine By Dependency,  Next: Cleanup,  Prev: make Deduces,  Up: Introduction
  675.  
  676. Another Style of Makefile
  677. =========================
  678.  
  679.    When the objects of a makefile are created only by implicit rules, an
  680. alternative style of makefile is possible.  In this style of makefile,
  681. you group entries by their dependencies instead of by their targets.
  682. Here is what one looks like:
  683.  
  684.      objects = main.o kbd.o command.o display.o \
  685.                insert.o search.o files.o utils.o
  686.      
  687.      edit : $(objects)
  688.              cc -o edit $(objects)
  689.      
  690.      $(objects) : defs.h
  691.      kbd.o command.o files.o : command.h
  692.      display.o insert.o search.o files.o : buffer.h
  693.  
  694. Here `defs.h' is given as a dependency of all the object files;
  695. `command.h' and `buffer.h' are dependencies of the specific object
  696. files listed for them.
  697.  
  698.    Whether this is better is a matter of taste: it is more compact, but
  699. some people dislike it because they find it clearer to put all the
  700. information about each target in one place.
  701.  
  702. File: make.info,  Node: Cleanup,  Prev: Combine By Dependency,  Up: Introduction
  703.  
  704. Rules for Cleaning the Directory
  705. ================================
  706.  
  707.    Compiling a program is not the only thing you might want to write
  708. rules for.  Makefiles commonly tell how to do a few other things besides
  709. compiling a program: for example, how to delete all the object files
  710. and executables so that the directory is `clean'.
  711.  
  712.    Here is how we could write a `make' rule for cleaning our example
  713. editor:
  714.  
  715.      clean:
  716.              rm edit $(objects)
  717.  
  718.    In practice, we might want to write the rule in a somewhat more
  719. complicated manner to handle unanticipated situations.  We would do
  720. this:
  721.  
  722.      .PHONY : clean
  723.      clean :
  724.              -rm edit $(objects)
  725.  
  726. This prevents `make' from getting confused by an actual file called
  727. `clean' and causes it to continue in spite of errors from `rm'.  (See
  728. *Note Phony Targets::, and *Note Errors in Commands: Errors.)
  729.  
  730. A rule such as this should not be placed at the beginning of the
  731. makefile, because we do not want it to run by default!  Thus, in the
  732. example makefile, we want the rule for `edit', which recompiles the
  733. editor, to remain the default goal.
  734.  
  735.    Since `clean' is not a dependency of `edit', this rule will not run
  736. at all if we give the command `make' with no arguments.  In order to
  737. make the rule run, we have to type `make clean'.  *Note How to Run
  738. `make': Running.
  739.  
  740. File: make.info,  Node: Makefiles,  Next: Rules,  Prev: Introduction,  Up: Top
  741.  
  742. Writing Makefiles
  743. *****************
  744.  
  745.    The information that tells `make' how to recompile a system comes
  746. from reading a data base called the "makefile".
  747.  
  748. * Menu:
  749.  
  750. * Makefile Contents::           What makefiles contain.
  751. * Makefile Names::              How to name your makefile.
  752. * Include::                     How one makefile can use another makefile.
  753. * MAKEFILES Variable::          The environment can specify extra makefiles.
  754. * Remaking Makefiles::          How makefiles get remade.
  755. * Overriding Makefiles::        How to override part of one makefile
  756.                                  with another makefile.
  757.  
  758. File: make.info,  Node: Makefile Contents,  Next: Makefile Names,  Up: Makefiles
  759.  
  760. What Makefiles Contain
  761. ======================
  762.  
  763.    Makefiles contain five kinds of things: "explicit rules", "implicit
  764. rules", "variable definitions", "directives", and "comments".  Rules,
  765. variables, and directives are described at length in later chapters.
  766.  
  767.    * An "explicit rule" says when and how to remake one or more files,
  768.      called the rule's targets.  It lists the other files that the
  769.      targets "depend on", and may also give commands to use to create
  770.      or update the targets.  *Note Writing Rules: Rules.
  771.  
  772.    * An "implicit rule" says when and how to remake a class of files
  773.      based on their names.  It describes how a target may depend on a
  774.      file with a name similar to the target and gives commands to
  775.      create or update such a target.  *Note Using Implicit Rules:
  776.      Implicit Rules.
  777.  
  778.    * A "variable definition" is a line that specifies a text string
  779.      value for a variable that can be substituted into the text later.
  780.      The simple makefile example shows a variable definition for
  781.      `objects' as a list of all object files (*note Variables Make
  782.      Makefiles Simpler: Variables Simplify.).
  783.  
  784.    * A "directive" is a command for `make' to do something special while
  785.      reading the makefile.  These include:
  786.  
  787.         * Reading another makefile (*note Including Other Makefiles:
  788.           Include.).
  789.  
  790.         * Deciding (based on the values of variables) whether to use or
  791.           ignore a part of the makefile (*note Conditional Parts of
  792.           Makefiles: Conditionals.).
  793.  
  794.         * Defining a variable from a verbatim string containing
  795.           multiple lines (*note Defining Variables Verbatim: Defining.).
  796.  
  797.    * `#' in a line of a makefile starts a "comment".  It and the rest of
  798.      the line are ignored, except that a trailing backslash not escaped
  799.      by another backslash will continue the comment across multiple
  800.      lines.  Comments may appear on any of the lines in the makefile,
  801.      except within a `define' directive, and perhaps within commands
  802.      (where the shell decides what is a comment).  A line containing
  803.      just a comment (with perhaps spaces before it) is effectively
  804.      blank, and is ignored.
  805.  
  806. File: make.info,  Node: Makefile Names,  Next: Include,  Prev: Makefile Contents,  Up: Makefiles
  807.  
  808. What Name to Give Your Makefile
  809. ===============================
  810.  
  811.    By default, when `make' looks for the makefile, it tries the
  812. following names, in order: `GNUmakefile', `makefile' and `Makefile'.
  813.  
  814.    Normally you should call your makefile either `makefile' or
  815. `Makefile'.  (We recommend `Makefile' because it appears prominently
  816. near the beginning of a directory listing, right near other important
  817. files such as `README'.)  The first name checked, `GNUmakefile', is not
  818. recommended for most makefiles.  You should use this name if you have a
  819. makefile that is specific to GNU `make', and will not be understood by
  820. other versions of `make'.  Other `make' programs look for `makefile' and
  821. `Makefile', but not `GNUmakefile'.
  822.  
  823.    If `make' finds none of these names, it does not use any makefile.
  824. Then you must specify a goal with a command argument, and `make' will
  825. attempt to figure out how to remake it using only its built-in implicit
  826. rules.  *Note Using Implicit Rules: Implicit Rules.
  827.  
  828.    If you want to use a nonstandard name for your makefile, you can
  829. specify the makefile name with the `-f' or `--file' option.  The
  830. arguments `-f NAME' or `--file=NAME' tell `make' to read the file NAME
  831. as the makefile.  If you use more than one `-f' or `--file' option, you
  832. can specify several makefiles.  All the makefiles are effectively
  833. concatenated in the order specified.  The default makefile names
  834. `GNUmakefile', `makefile' and `Makefile' are not checked automatically
  835. if you specify `-f' or `--file'.
  836.  
  837. File: make.info,  Node: Include,  Next: MAKEFILES Variable,  Prev: Makefile Names,  Up: Makefiles
  838.  
  839. Including Other Makefiles
  840. =========================
  841.  
  842.    The `include' directive tells `make' to suspend reading the current
  843. makefile and read one or more other makefiles before continuing.  The
  844. directive is a line in the makefile that looks like this:
  845.  
  846.      include FILENAMES...
  847.  
  848. FILENAMES can contain shell file name patterns.
  849.  
  850.    Extra spaces are allowed and ignored at the beginning of the line,
  851. but a tab is not allowed.  (If the line begins with a tab, it will be
  852. considered a command line.)  Whitespace is required between `include'
  853. and the file names, and between file names; extra whitespace is ignored
  854. there and at the end of the directive.  A comment starting with `#' is
  855. allowed at the end of the line.  If the file names contain any variable
  856. or function references, they are expanded.  *Note How to Use Variables:
  857. Using Variables.
  858.  
  859.    For example, if you have three `.mk' files, `a.mk', `b.mk', and
  860. `c.mk', and `$(bar)' expands to `bish bash', then the following
  861. expression
  862.  
  863.      include foo *.mk $(bar)
  864.  
  865.    is equivalent to
  866.  
  867.      include foo a.mk b.mk c.mk bish bash
  868.  
  869.    When `make' processes an `include' directive, it suspends reading of
  870. the containing makefile and reads from each listed file in turn.  When
  871. that is finished, `make' resumes reading the makefile in which the
  872. directive appears.
  873.  
  874.    One occasion for using `include' directives is when several programs,
  875. handled by individual makefiles in various directories, need to use a
  876. common set of variable definitions (*note Setting Variables: Setting.)
  877. or pattern rules (*note Defining and Redefining Pattern Rules: Pattern
  878. Rules.).
  879.  
  880.    Another such occasion is when you want to generate dependencies from
  881. source files automatically; the dependencies can be put in a file that
  882. is included by the main makefile.  This practice is generally cleaner
  883. than that of somehow appending the dependencies to the end of the main
  884. makefile as has been traditionally done with other versions of `make'.
  885. *Note Automatic Dependencies::.
  886.  
  887.    If the specified name does not start with a slash, and the file is
  888. not found in the current directory, several other directories are
  889. searched.  First, any directories you have specified with the `-I' or
  890. `--include-dir' option are searched (*note Summary of Options: Options
  891. Summary.).  Then the following directories (if they exist) are
  892. searched, in this order: `PREFIX/include' (normally `/usr/local/include'
  893. (1)) `/usr/gnu/include', `/usr/local/include', `/usr/include'.
  894.  
  895.    If an included makefile cannot be found in any of these directories,
  896. a warning message is generated, but it is not an immediately fatal
  897. error; processing of the makefile containing the `include' continues.
  898. Once it has finished reading makefiles, `make' will try to remake any
  899. that are out of date or don't exist.  *Note How Makefiles Are Remade:
  900. Remaking Makefiles.  Only after it has tried to find a way to remake a
  901. makefile and failed, will `make' diagnose the missing makefile as a
  902. fatal error.
  903.  
  904.    If you want `make' to simply ignore a makefile which does not exist
  905. and cannot be remade, with no error message, use the `-include'
  906. directive instead of `include', like this:
  907.  
  908.      -include FILENAMES...
  909.  
  910.    This is acts like `include' in every way except that there is no
  911. error (not even a warning) if any of the FILENAMES do not exist.  For
  912. compatibility with some other `make' implementations, `sinclude' is
  913. another name for `-include'.
  914.  
  915.    ---------- Footnotes ----------
  916.  
  917.    (1)  GNU Make compiled for MS-DOS and MS-Windows behaves as if
  918. PREFIX has been defined to be the root of the DJGPP tree hierarchy.
  919.  
  920. File: make.info,  Node: MAKEFILES Variable,  Next: Remaking Makefiles,  Prev: Include,  Up: Makefiles
  921.  
  922. The Variable `MAKEFILES'
  923. ========================
  924.  
  925.    If the environment variable `MAKEFILES' is defined, `make' considers
  926. its value as a list of names (separated by whitespace) of additional
  927. makefiles to be read before the others.  This works much like the
  928. `include' directive: various directories are searched for those files
  929. (*note Including Other Makefiles: Include.).  In addition, the default
  930. goal is never taken from one of these makefiles and it is not an error
  931. if the files listed in `MAKEFILES' are not found.
  932.  
  933.    The main use of `MAKEFILES' is in communication between recursive
  934. invocations of `make' (*note Recursive Use of `make': Recursion.).  It
  935. usually is not desirable to set the environment variable before a
  936. top-level invocation of `make', because it is usually better not to
  937. mess with a makefile from outside.  However, if you are running `make'
  938. without a specific makefile, a makefile in `MAKEFILES' can do useful
  939. things to help the built-in implicit rules work better, such as
  940. defining search paths (*note Directory Search::.).
  941.  
  942.    Some users are tempted to set `MAKEFILES' in the environment
  943. automatically on login, and program makefiles to expect this to be done.
  944. This is a very bad idea, because such makefiles will fail to work if
  945. run by anyone else.  It is much better to write explicit `include'
  946. directives in the makefiles.  *Note Including Other Makefiles: Include.
  947.  
  948. File: make.info,  Node: Remaking Makefiles,  Next: Overriding Makefiles,  Prev: MAKEFILES Variable,  Up: Makefiles
  949.  
  950. How Makefiles Are Remade
  951. ========================
  952.  
  953.    Sometimes makefiles can be remade from other files, such as RCS or
  954. SCCS files.  If a makefile can be remade from other files, you probably
  955. want `make' to get an up-to-date version of the makefile to read in.
  956.  
  957.    To this end, after reading in all makefiles, `make' will consider
  958. each as a goal target and attempt to update it.  If a makefile has a
  959. rule which says how to update it (found either in that very makefile or
  960. in another one) or if an implicit rule applies to it (*note Using
  961. Implicit Rules: Implicit Rules.), it will be updated if necessary.
  962. After all makefiles have been checked, if any have actually been
  963. changed, `make' starts with a clean slate and reads all the makefiles
  964. over again.  (It will also attempt to update each of them over again,
  965. but normally this will not change them again, since they are already up
  966. to date.)
  967.  
  968.    If the makefiles specify a double-colon rule to remake a file with
  969. commands but no dependencies, that file will always be remade (*note
  970. Double-Colon::.).  In the case of makefiles, a makefile that has a
  971. double-colon rule with commands but no dependencies will be remade every
  972. time `make' is run, and then again after `make' starts over and reads
  973. the makefiles in again.  This would cause an infinite loop: `make'
  974. would constantly remake the makefile, and never do anything else.  So,
  975. to avoid this, `make' will *not* attempt to remake makefiles which are
  976. specified as double-colon targets but have no dependencies.
  977.  
  978.    If you do not specify any makefiles to be read with `-f' or `--file'
  979. options, `make' will try the default makefile names; *note What Name to
  980. Give Your Makefile: Makefile Names..  Unlike makefiles explicitly
  981. requested with `-f' or `--file' options, `make' is not certain that
  982. these makefiles should exist.  However, if a default makefile does not
  983. exist but can be created by running `make' rules, you probably want the
  984. rules to be run so that the makefile can be used.
  985.  
  986.    Therefore, if none of the default makefiles exists, `make' will try
  987. to make each of them in the same order in which they are searched for
  988. (*note What Name to Give Your Makefile: Makefile Names.) until it
  989. succeeds in making one, or it runs out of names to try.  Note that it
  990. is not an error if `make' cannot find or make any makefile; a makefile
  991. is not always necessary.
  992.  
  993.    When you use the `-t' or `--touch' option (*note Instead of
  994. Executing the Commands: Instead of Execution.), you would not want to
  995. use an out-of-date makefile to decide which targets to touch.  So the
  996. `-t' option has no effect on updating makefiles; they are really
  997. updated even if `-t' is specified.  Likewise, `-q' (or `--question')
  998. and `-n' (or `--just-print') do not prevent updating of makefiles,
  999. because an out-of-date makefile would result in the wrong output for
  1000. other targets.  Thus, `make -f mfile -n foo' will update `mfile', read
  1001. it in, and then print the commands to update `foo' and its dependencies
  1002. without running them.  The commands printed for `foo' will be those
  1003. specified in the updated contents of `mfile'.
  1004.  
  1005.    However, on occasion you might actually wish to prevent updating of
  1006. even the makefiles.  You can do this by specifying the makefiles as
  1007. goals in the command line as well as specifying them as makefiles.
  1008. When the makefile name is specified explicitly as a goal, the options
  1009. `-t' and so on do apply to them.
  1010.  
  1011.    Thus, `make -f mfile -n mfile foo' would read the makefile `mfile',
  1012. print the commands needed to update it without actually running them,
  1013. and then print the commands needed to update `foo' without running
  1014. them.  The commands for `foo' will be those specified by the existing
  1015. contents of `mfile'.
  1016.  
  1017. File: make.info,  Node: Overriding Makefiles,  Prev: Remaking Makefiles,  Up: Makefiles
  1018.  
  1019. Overriding Part of Another Makefile
  1020. ===================================
  1021.  
  1022.    Sometimes it is useful to have a makefile that is mostly just like
  1023. another makefile.  You can often use the `include' directive to include
  1024. one in the other, and add more targets or variable definitions.
  1025. However, if the two makefiles give different commands for the same
  1026. target, `make' will not let you just do this.  But there is another way.
  1027.  
  1028.    In the containing makefile (the one that wants to include the other),
  1029. you can use a match-anything pattern rule to say that to remake any
  1030. target that cannot be made from the information in the containing
  1031. makefile, `make' should look in another makefile.  *Note Pattern
  1032. Rules::, for more information on pattern rules.
  1033.  
  1034.    For example, if you have a makefile called `Makefile' that says how
  1035. to make the target `foo' (and other targets), you can write a makefile
  1036. called `GNUmakefile' that contains:
  1037.  
  1038.      foo:
  1039.              frobnicate > foo
  1040.      
  1041.      %: force
  1042.              @$(MAKE) -f Makefile $@
  1043.      force: ;
  1044.  
  1045.    If you say `make foo', `make' will find `GNUmakefile', read it, and
  1046. see that to make `foo', it needs to run the command `frobnicate > foo'.
  1047. If you say `make bar', `make' will find no way to make `bar' in
  1048. `GNUmakefile', so it will use the commands from the pattern rule: `make
  1049. -f Makefile bar'.  If `Makefile' provides a rule for updating `bar',
  1050. `make' will apply the rule.  And likewise for any other target that
  1051. `GNUmakefile' does not say how to make.
  1052.  
  1053.    The way this works is that the pattern rule has a pattern of just
  1054. `%', so it matches any target whatever.  The rule specifies a
  1055. dependency `force', to guarantee that the commands will be run even if
  1056. the target file already exists.  We give `force' target empty commands
  1057. to prevent `make' from searching for an implicit rule to build
  1058. it--otherwise it would apply the same match-anything rule to `force'
  1059. itself and create a dependency loop!
  1060.  
  1061. File: make.info,  Node: Rules,  Next: Commands,  Prev: Makefiles,  Up: Top
  1062.  
  1063. Writing Rules
  1064. *************
  1065.  
  1066.    A "rule" appears in the makefile and says when and how to remake
  1067. certain files, called the rule's "targets" (most often only one per
  1068. rule).  It lists the other files that are the "dependencies" of the
  1069. target, and "commands" to use to create or update the target.
  1070.  
  1071.    The order of rules is not significant, except for determining the
  1072. "default goal": the target for `make' to consider, if you do not
  1073. otherwise specify one.  The default goal is the target of the first
  1074. rule in the first makefile.  If the first rule has multiple targets,
  1075. only the first target is taken as the default.  There are two
  1076. exceptions: a target starting with a period is not a default unless it
  1077. contains one or more slashes, `/', as well; and, a target that defines
  1078. a pattern rule has no effect on the default goal.  (*Note Defining and
  1079. Redefining Pattern Rules: Pattern Rules.)
  1080.  
  1081.    Therefore, we usually write the makefile so that the first rule is
  1082. the one for compiling the entire program or all the programs described
  1083. by the makefile (often with a target called `all').  *Note Arguments to
  1084. Specify the Goals: Goals.
  1085.  
  1086. * Menu:
  1087.  
  1088. * Rule Example::                An example explained.
  1089. * Rule Syntax::                 General syntax explained.
  1090. * Wildcards::                   Using wildcard characters such as `*'.
  1091. * Directory Search::            Searching other directories for source files.
  1092. * Phony Targets::               Using a target that is not a real file's name.
  1093. * Force Targets::               You can use a target without commands
  1094.                                   or dependencies to mark other
  1095.                                   targets as phony.
  1096. * Empty Targets::               When only the date matters and the
  1097.                                   files are empty.
  1098. * Special Targets::             Targets with special built-in meanings.
  1099. * Multiple Targets::            When to make use of several targets in a rule.
  1100. * Multiple Rules::              How to use several rules with the same target.
  1101. * Static Pattern::              Static pattern rules apply to multiple targets
  1102.                                   and can vary the dependencies according to
  1103.                                   the target name.
  1104. * Double-Colon::                How to use a special kind of rule to allow
  1105.                                   several independent rules for one target.
  1106. * Automatic Dependencies::      How to automatically generate rules giving
  1107.                                  dependencies from the source files themselves.
  1108.  
  1109.