1 \input texinfo @c -*- Texinfo -*-
7 @set RCSID $Id: make.texi,v 1.65 2010/07/06 06:37:42 psmith Exp $
9 @settitle GNU @code{make}
10 @setchapternewpage odd
11 @c Combine the variable and function indices:
13 @c Combine the program and concept indices:
15 @c FSF publishers: format makebook.texi instead of using this file directly.
16 @c ISBN confirmed by Jasimin Huang <jasimin@fsf.org> on 25 Mar 2009
17 @set ISBN 1-882114-83-3
21 This file documents the GNU @code{make} utility, which determines
22 automatically which pieces of a large program need to be recompiled,
23 and issues the commands to recompile them.
25 This is Edition @value{EDITION}, last updated @value{UPDATED},
26 of @cite{The GNU Make Manual}, for GNU @code{make} version @value{VERSION}.
28 Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
29 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
30 Free Software Foundation, Inc.
33 Permission is granted to copy, distribute and/or modify this document
34 under the terms of the GNU Free Documentation License, Version 1.2 or
35 any later version published by the Free Software Foundation; with no
36 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
37 and with the Back-Cover Texts as in (a) below. A copy of the
38 license is included in the section entitled ``GNU Free Documentation
41 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
42 modify this GNU manual. Buying copies from the FSF supports it in
43 developing GNU and promoting software freedom.''
49 @c ISPELL CHECK: done, 10 June 1993 --roland
50 @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
53 @dircategory Software development
55 * Make: (make). Remake files automatically.
59 @shorttitlepage GNU Make
63 @subtitle A Program for Directing Recompilation
64 @subtitle GNU @code{make} Version @value{VERSION}
65 @subtitle @value{UPDATED-MONTH}
66 @author Richard M. Stallman, Roland McGrath, Paul D. Smith
68 @vskip 0pt plus 1filll
71 Published by the Free Software Foundation @*
72 51 Franklin St. -- Fifth Floor @*
73 Boston, MA 02110-1301 USA @*
76 Cover art by Etienne Suvasa.
83 @node Top, Overview, (dir), (dir)
90 * Overview:: Overview of @code{make}.
91 * Introduction:: An introduction to @code{make}.
92 * Makefiles:: Makefiles tell @code{make} what to do.
93 * Rules:: Rules describe when a file must be remade.
94 * Recipes:: Recipes say how to remake a file.
95 * Using Variables:: You can use variables to avoid repetition.
96 * Conditionals:: Use or ignore parts of the makefile based
97 on the values of variables.
98 * Functions:: Many powerful ways to manipulate text.
99 * Invoking make: Running. How to invoke @code{make} on the command line.
100 * Implicit Rules:: Use implicit rules to treat many files alike,
101 based on their file names.
102 * Archives:: How @code{make} can update library archives.
103 * Features:: Features GNU @code{make} has over other @code{make}s.
104 * Missing:: What GNU @code{make} lacks from other @code{make}s.
105 * Makefile Conventions:: Conventions for writing makefiles for
107 * Quick Reference:: A quick reference for experienced users.
108 * Error Messages:: A list of common errors generated by @code{make}.
109 * Complex Makefile:: A real example of a straightforward,
110 but nontrivial, makefile.
112 * GNU Free Documentation License:: License for copying this manual
113 * Concept Index:: Index of Concepts
114 * Name Index:: Index of Functions, Variables, & Directives
117 --- The Detailed Node Listing ---
119 Overview of @code{make}
121 * Preparing:: Preparing and running make
122 * Reading:: On reading this text
123 * Bugs:: Problems and bugs
125 An Introduction to Makefiles
127 * Rule Introduction:: What a rule looks like.
128 * Simple Makefile:: A simple makefile
129 * How Make Works:: How @code{make} processes this makefile
130 * Variables Simplify:: Variables make makefiles simpler
131 * make Deduces:: Letting @code{make} deduce the recipe
132 * Combine By Prerequisite:: Another style of makefile
133 * Cleanup:: Rules for cleaning the directory
137 * Makefile Contents:: What makefiles contain.
138 * Makefile Names:: How to name your makefile.
139 * Include:: How one makefile can use another makefile.
140 * MAKEFILES Variable:: The environment can specify extra makefiles.
141 * Remaking Makefiles:: How makefiles get remade.
142 * Overriding Makefiles:: How to override part of one makefile
143 with another makefile.
144 * Reading Makefiles:: How makefiles are parsed.
145 * Secondary Expansion:: How and when secondary expansion is performed.
149 * Rule Example:: An example explained.
150 * Rule Syntax:: General syntax explained.
151 * Prerequisite Types:: There are two types of prerequisites.
152 * Wildcards:: Using wildcard characters such as `*'.
153 * Directory Search:: Searching other directories for source files.
154 * Phony Targets:: Using a target that is not a real file's name.
155 * Force Targets:: You can use a target without a recipe
156 or prerequisites to mark other targets
158 * Empty Targets:: When only the date matters and the
160 * Special Targets:: Targets with special built-in meanings.
161 * Multiple Targets:: When to make use of several targets in a rule.
162 * Multiple Rules:: How to use several rules with the same target.
163 * Static Pattern:: Static pattern rules apply to multiple targets
164 and can vary the prerequisites according to
166 * Double-Colon:: How to use a special kind of rule to allow
167 several independent rules for one target.
168 * Automatic Prerequisites:: How to automatically generate rules giving
169 prerequisites from source files themselves.
171 Using Wildcard Characters in File Names
173 * Wildcard Examples:: Several examples
174 * Wildcard Pitfall:: Problems to avoid.
175 * Wildcard Function:: How to cause wildcard expansion where
176 it does not normally take place.
178 Searching Directories for Prerequisites
180 * General Search:: Specifying a search path that applies
181 to every prerequisite.
182 * Selective Search:: Specifying a search path
183 for a specified class of names.
184 * Search Algorithm:: When and how search paths are applied.
185 * Recipes/Search:: How to write recipes that work together
187 * Implicit/Search:: How search paths affect implicit rules.
188 * Libraries/Search:: Directory search for link libraries.
192 * Static Usage:: The syntax of static pattern rules.
193 * Static versus Implicit:: When are they better than implicit rules?
195 Writing Recipes in Rules
197 * Recipe Syntax:: Recipe syntax features and pitfalls.
198 * Echoing:: How to control when recipes are echoed.
199 * Execution:: How recipes are executed.
200 * Parallel:: How recipes can be executed in parallel.
201 * Errors:: What happens after a recipe execution error.
202 * Interrupts:: What happens when a recipe is interrupted.
203 * Recursion:: Invoking @code{make} from makefiles.
204 * Canned Recipes:: Defining canned recipes.
205 * Empty Recipes:: Defining useful, do-nothing recipes.
209 * Splitting Lines:: Breaking long recipe lines for readability.
210 * Variables in Recipes:: Using @code{make} variables in recipes.
214 * Choosing the Shell:: How @code{make} chooses the shell used
217 Recursive Use of @code{make}
219 * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
220 * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
221 * Options/Recursion:: How to communicate options to a sub-@code{make}.
222 * -w Option:: How the @samp{-w} or @samp{--print-directory} option
223 helps debug use of recursive @code{make} commands.
227 * Reference:: How to use the value of a variable.
228 * Flavors:: Variables come in two flavors.
229 * Advanced:: Advanced features for referencing a variable.
230 * Values:: All the ways variables get their values.
231 * Setting:: How to set a variable in the makefile.
232 * Appending:: How to append more text to the old value
234 * Override Directive:: How to set a variable in the makefile even if
235 the user has set it with a command argument.
236 * Multi-Line:: An alternate way to set a variable
237 to a multi-line string.
238 * Environment:: Variable values can come from the environment.
239 * Target-specific:: Variable values can be defined on a per-target
241 * Pattern-specific:: Target-specific variable values can be applied
242 to a group of targets that match a pattern.
243 * Suppressing Inheritance:: Suppress inheritance of variables.
244 * Special Variables:: Variables with special meaning or behavior.
246 Advanced Features for Reference to Variables
248 * Substitution Refs:: Referencing a variable with
249 substitutions on the value.
250 * Computed Names:: Computing the name of the variable to refer to.
252 Conditional Parts of Makefiles
254 * Conditional Example:: Example of a conditional
255 * Conditional Syntax:: The syntax of conditionals.
256 * Testing Flags:: Conditionals that test flags.
258 Functions for Transforming Text
260 * Syntax of Functions:: How to write a function call.
261 * Text Functions:: General-purpose text manipulation functions.
262 * File Name Functions:: Functions for manipulating file names.
263 * Conditional Functions:: Functions that implement conditions.
264 * Foreach Function:: Repeat some text with controlled variation.
265 * Call Function:: Expand a user-defined function.
266 * Value Function:: Return the un-expanded value of a variable.
267 * Eval Function:: Evaluate the arguments as makefile syntax.
268 * Origin Function:: Find where a variable got its value.
269 * Flavor Function:: Find out the flavor of a variable.
270 * Shell Function:: Substitute the output of a shell command.
271 * Make Control Functions:: Functions that control how make runs.
273 How to Run @code{make}
275 * Makefile Arguments:: How to specify which makefile to use.
276 * Goals:: How to use goal arguments to specify which
277 parts of the makefile to use.
278 * Instead of Execution:: How to use mode flags to specify what
279 kind of thing to do with the recipes
280 in the makefile other than simply
282 * Avoiding Compilation:: How to avoid recompiling certain files.
283 * Overriding:: How to override a variable to specify
284 an alternate compiler and other things.
285 * Testing:: How to proceed past some errors, to
287 * Options Summary:: Summary of Options
291 * Using Implicit:: How to use an existing implicit rule
292 to get the recipe for updating a file.
293 * Catalogue of Rules:: A list of built-in implicit rules.
294 * Implicit Variables:: How to change what predefined rules do.
295 * Chained Rules:: How to use a chain of implicit rules.
296 * Pattern Rules:: How to define new implicit rules.
297 * Last Resort:: How to define a recipe for rules which
299 * Suffix Rules:: The old-fashioned style of implicit rule.
300 * Implicit Rule Search:: The precise algorithm for applying
303 Defining and Redefining Pattern Rules
305 * Pattern Intro:: An introduction to pattern rules.
306 * Pattern Examples:: Examples of pattern rules.
307 * Automatic Variables:: How to use automatic variables in the
308 recipe of implicit rules.
309 * Pattern Match:: How patterns match.
310 * Match-Anything Rules:: Precautions you should take prior to
311 defining rules that can match any
312 target file whatever.
313 * Canceling Rules:: How to override or cancel built-in rules.
315 Using @code{make} to Update Archive Files
317 * Archive Members:: Archive members as targets.
318 * Archive Update:: The implicit rule for archive member targets.
319 * Archive Pitfalls:: Dangers to watch out for when using archives.
320 * Archive Suffix Rules:: You can write a special kind of suffix rule
321 for updating archives.
323 Implicit Rule for Archive Member Targets
325 * Archive Symbols:: How to update archive symbol directories.
330 @node Overview, Introduction, Top, Top
331 @comment node-name, next, previous, up
332 @chapter Overview of @code{make}
334 The @code{make} utility automatically determines which pieces of a large
335 program need to be recompiled, and issues commands to recompile them.
336 This manual describes GNU @code{make}, which was implemented by Richard
337 Stallman and Roland McGrath. Development since Version 3.76 has been
338 handled by Paul D. Smith.
340 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
341 1003.2-1992} (POSIX.2).
343 @cindex IEEE Standard 1003.2
344 @cindex standards conformance
346 Our examples show C programs, since they are most common, but you can use
347 @code{make} with any programming language whose compiler can be run with a
348 shell command. Indeed, @code{make} is not limited to programs. You can
349 use it to describe any task where some files must be updated automatically
350 from others whenever the others change.
353 * Preparing:: Preparing and Running Make
354 * Reading:: On Reading this Text
355 * Bugs:: Problems and Bugs
358 @node Preparing, Reading, Overview, Overview
360 @heading Preparing and Running Make
363 To prepare to use @code{make}, you must write a file called
364 the @dfn{makefile} that describes the relationships among files
365 in your program and provides commands for updating each file.
366 In a program, typically, the executable file is updated from object
367 files, which are in turn made by compiling source files.@refill
369 Once a suitable makefile exists, each time you change some source files,
370 this simple shell command:
377 suffices to perform all necessary recompilations. The @code{make} program
378 uses the makefile data base and the last-modification times of the files to
379 decide which of the files need to be updated. For each of those files, it
380 issues the recipes recorded in the data base.
382 You can provide command line arguments to @code{make} to control which
383 files should be recompiled, or how. @xref{Running, ,How to Run
386 @node Reading, Bugs, Preparing, Overview
387 @section How to Read This Manual
389 If you are new to @code{make}, or are looking for a general
390 introduction, read the first few sections of each chapter, skipping the
391 later sections. In each chapter, the first few sections contain
392 introductory or general information and the later sections contain
393 specialized or technical information.
395 The exception is the second chapter, @ref{Introduction, ,An
396 Introduction to Makefiles}, all of which is introductory.
399 The exception is @ref{Introduction, ,An Introduction to Makefiles},
400 all of which is introductory.
403 If you are familiar with other @code{make} programs, see @ref{Features,
404 ,Features of GNU @code{make}}, which lists the enhancements GNU
405 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
406 Features}, which explains the few things GNU @code{make} lacks that
409 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
410 and @ref{Special Targets}.
412 @node Bugs, , Reading, Overview
413 @section Problems and Bugs
414 @cindex reporting bugs
415 @cindex bugs, reporting
416 @cindex problems and bugs, reporting
418 If you have problems with GNU @code{make} or think you've found a bug,
419 please report it to the developers; we cannot promise to do anything but
420 we might well want to fix it.
422 Before reporting a bug, make sure you've actually found a real bug.
423 Carefully reread the documentation and see if it really says you can do
424 what you're trying to do. If it's not clear whether you should be able
425 to do something or not, report that too; it's a bug in the
428 Before reporting a bug or trying to fix it yourself, try to isolate it
429 to the smallest possible makefile that reproduces the problem. Then
430 send us the makefile and the exact results @code{make} gave you,
431 including any error or warning messages. Please don't paraphrase
432 these messages: it's best to cut and paste them into your report.
433 When generating this small makefile, be sure to not use any non-free
434 or unusual tools in your recipes: you can almost always emulate what
435 such a tool would do with simple shell commands. Finally, be sure to
436 explain what you expected to occur; this will help us decide whether
437 the problem was really in the documentation.
439 Once you have a precise problem you can report it in one of two ways.
440 Either send electronic mail to:
447 or use our Web-based project management tool, at:
450 http://savannah.gnu.org/projects/make/
454 In addition to the information above, please be careful to include the
455 version number of @code{make} you are using. You can get this
456 information with the command @samp{make --version}. Be sure also to
457 include the type of machine and operating system you are using. One
458 way to obtain this information is by looking at the final lines of
459 output from the command @samp{make --help}.
461 @node Introduction, Makefiles, Overview, Top
462 @comment node-name, next, previous, up
463 @chapter An Introduction to Makefiles
465 You need a file called a @dfn{makefile} to tell @code{make} what to do.
466 Most often, the makefile tells @code{make} how to compile and link a
470 In this chapter, we will discuss a simple makefile that describes how to
471 compile and link a text editor which consists of eight C source files
472 and three header files. The makefile can also tell @code{make} how to
473 run miscellaneous commands when explicitly asked (for example, to remove
474 certain files as a clean-up operation). To see a more complex example
475 of a makefile, see @ref{Complex Makefile}.
477 When @code{make} recompiles the editor, each changed C source file
478 must be recompiled. If a header file has changed, each C source file
479 that includes the header file must be recompiled to be safe. Each
480 compilation produces an object file corresponding to the source file.
481 Finally, if any source file has been recompiled, all the object files,
482 whether newly made or saved from previous compilations, must be linked
483 together to produce the new executable editor.
484 @cindex recompilation
488 * Rule Introduction:: What a rule looks like.
489 * Simple Makefile:: A Simple Makefile
490 * How Make Works:: How @code{make} Processes This Makefile
491 * Variables Simplify:: Variables Make Makefiles Simpler
492 * make Deduces:: Letting @code{make} Deduce the Recipes
493 * Combine By Prerequisite:: Another Style of Makefile
494 * Cleanup:: Rules for Cleaning the Directory
497 @node Rule Introduction, Simple Makefile, Introduction, Introduction
498 @comment node-name, next, previous, up
499 @section What a Rule Looks Like
500 @cindex rule, introduction to
501 @cindex makefile rule parts
502 @cindex parts of makefile rule
504 A simple makefile consists of ``rules'' with the following shape:
506 @cindex targets, introduction to
507 @cindex prerequisites, introduction to
508 @cindex recipes, introduction to
511 @var{target} @dots{} : @var{prerequisites} @dots{}
518 A @dfn{target} is usually the name of a file that is generated by a
519 program; examples of targets are executable or object files. A target
520 can also be the name of an action to carry out, such as @samp{clean}
521 (@pxref{Phony Targets}).
523 A @dfn{prerequisite} is a file that is used as input to create the
524 target. A target often depends on several files.
526 @cindex tabs in rules
527 A @dfn{recipe} is an action that @code{make} carries out.
528 A recipe may have more than one command, each on its own line.
529 @strong{Please note:} you need to put a tab character at the beginning of
530 every command line! This is an obscurity that catches the unwary. If
531 you prefer to prefix your recipes with a character other than tab,
532 you can set the @code{.CMDPREFIX} variable to an alternate character
533 (@pxref{Special Variables}).
535 Usually a recipe is in a rule with prerequisites and serves to create a
536 target file if any of the prerequisites change. However, the rule that
537 specifies a recipe for the target need not have prerequisites. For
538 example, the rule containing the delete command associated with the
539 target @samp{clean} does not have prerequisites.
541 A @dfn{rule}, then, explains how and when to remake certain files
542 which are the targets of the particular rule. @code{make} carries out
543 the recipe on the prerequisites to create or update the target. A
544 rule can also explain how and when to carry out an action.
545 @xref{Rules, , Writing Rules}.
547 A makefile may contain other text besides rules, but a simple makefile
548 need only contain rules. Rules may look somewhat more complicated
549 than shown in this template, but all fit the pattern more or less.
551 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
552 @section A Simple Makefile
553 @cindex simple makefile
554 @cindex makefile, simple
556 Here is a straightforward makefile that describes the way an
557 executable file called @code{edit} depends on eight object files
558 which, in turn, depend on eight C source and three header files.
560 In this example, all the C files include @file{defs.h}, but only those
561 defining editing commands include @file{command.h}, and only low
562 level files that change the editor buffer include @file{buffer.h}.
566 edit : main.o kbd.o command.o display.o \
567 insert.o search.o files.o utils.o
568 cc -o edit main.o kbd.o command.o display.o \
569 insert.o search.o files.o utils.o
571 main.o : main.c defs.h
573 kbd.o : kbd.c defs.h command.h
575 command.o : command.c defs.h command.h
577 display.o : display.c defs.h buffer.h
579 insert.o : insert.c defs.h buffer.h
581 search.o : search.c defs.h buffer.h
583 files.o : files.c defs.h buffer.h command.h
585 utils.o : utils.c defs.h
588 rm edit main.o kbd.o command.o display.o \
589 insert.o search.o files.o utils.o
594 We split each long line into two lines using backslash-newline; this is
595 like using one long line, but is easier to read.
596 @cindex continuation lines
597 @cindex @code{\} (backslash), for continuation lines
598 @cindex backslash (@code{\}), for continuation lines
599 @cindex quoting newline, in makefile
600 @cindex newline, quoting, in makefile
602 To use this makefile to create the executable file called @file{edit},
609 To use this makefile to delete the executable file and all the object
610 files from the directory, type:
616 In the example makefile, the targets include the executable file
617 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The
618 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
619 In fact, each @samp{.o} file is both a target and a prerequisite.
620 Recipes include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
622 When a target is a file, it needs to be recompiled or relinked if any
623 of its prerequisites change. In addition, any prerequisites that are
624 themselves automatically generated should be updated first. In this
625 example, @file{edit} depends on each of the eight object files; the
626 object file @file{main.o} depends on the source file @file{main.c} and
627 on the header file @file{defs.h}.
629 A recipe may follow each line that contains a target and
630 prerequisites. These recipes say how to update the target file. A
631 tab character (or whatever character is specified by the
632 @code{.CMDPREFIX} variable; @pxref{Special Variables}) must come at
633 the beginning of every line in the recipe to distinguish recipes from
634 other lines in the makefile. (Bear in mind that @code{make} does not
635 know anything about how the recipes work. It is up to you to supply
636 recipes that will update the target file properly. All @code{make}
637 does is execute the commands in the recipe you have specified when the
638 target file needs to be updated.)@refill
641 The target @samp{clean} is not a file, but merely the name of an
642 action. Since you normally do not want to carry out the actions in
643 this rule, @samp{clean} is not a prerequisite of any other rule.
644 Consequently, @code{make} never does anything with it unless you tell
645 it specifically. Note that this rule not only is not a prerequisite,
646 it also does not have any prerequisites, so the only purpose of the
647 rule is to run the specified recipe. Targets that do not refer to
648 files but are just actions are called @dfn{phony targets}.
649 @xref{Phony Targets}, for information about this kind of target.
650 @xref{Errors, , Errors in Recipes}, to see how to cause @code{make}
651 to ignore errors from @code{rm} or any other command.
652 @cindex @code{clean} target
653 @cindex @code{rm} (shell command)
655 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
656 @comment node-name, next, previous, up
657 @section How @code{make} Processes a Makefile
658 @cindex processing a makefile
659 @cindex makefile, how @code{make} processes
661 By default, @code{make} starts with the first target (not targets whose
662 names start with @samp{.}). This is called the @dfn{default goal}.
663 (@dfn{Goals} are the targets that @code{make} strives ultimately to
664 update. You can override this behavior using the command line
665 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
666 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
667 Other Special Variables}).
669 @cindex goal, default
672 In the simple example of the previous section, the default goal is to
673 update the executable program @file{edit}; therefore, we put that rule
676 Thus, when you give the command:
683 @code{make} reads the makefile in the current directory and begins by
684 processing the first rule. In the example, this rule is for relinking
685 @file{edit}; but before @code{make} can fully process this rule, it
686 must process the rules for the files that @file{edit} depends on,
687 which in this case are the object files. Each of these files is
688 processed according to its own rule. These rules say to update each
689 @samp{.o} file by compiling its source file. The recompilation must
690 be done if the source file, or any of the header files named as
691 prerequisites, is more recent than the object file, or if the object
694 The other rules are processed because their targets appear as
695 prerequisites of the goal. If some other rule is not depended on by the
696 goal (or anything it depends on, etc.), that rule is not processed,
697 unless you tell @code{make} to do so (with a command such as
698 @w{@code{make clean}}).
700 Before recompiling an object file, @code{make} considers updating its
701 prerequisites, the source file and header files. This makefile does not
702 specify anything to be done for them---the @samp{.c} and @samp{.h} files
703 are not the targets of any rules---so @code{make} does nothing for these
704 files. But @code{make} would update automatically generated C programs,
705 such as those made by Bison or Yacc, by their own rules at this time.
707 After recompiling whichever object files need it, @code{make} decides
708 whether to relink @file{edit}. This must be done if the file
709 @file{edit} does not exist, or if any of the object files are newer than
710 it. If an object file was just recompiled, it is now newer than
711 @file{edit}, so @file{edit} is relinked.
714 Thus, if we change the file @file{insert.c} and run @code{make},
715 @code{make} will compile that file to update @file{insert.o}, and then
716 link @file{edit}. If we change the file @file{command.h} and run
717 @code{make}, @code{make} will recompile the object files @file{kbd.o},
718 @file{command.o} and @file{files.o} and then link the file @file{edit}.
720 @node Variables Simplify, make Deduces, How Make Works, Introduction
721 @section Variables Make Makefiles Simpler
723 @cindex simplifying with variables
725 In our example, we had to list all the object files twice in the rule for
726 @file{edit} (repeated here):
730 edit : main.o kbd.o command.o display.o \
731 insert.o search.o files.o utils.o
732 cc -o edit main.o kbd.o command.o display.o \
733 insert.o search.o files.o utils.o
737 @cindex @code{objects}
738 Such duplication is error-prone; if a new object file is added to the
739 system, we might add it to one list and forget the other. We can eliminate
740 the risk and simplify the makefile by using a variable. @dfn{Variables}
741 allow a text string to be defined once and substituted in multiple places
742 later (@pxref{Using Variables, ,How to Use Variables}).
744 @cindex @code{OBJECTS}
749 It is standard practice for every makefile to have a variable named
750 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
751 or @code{OBJ} which is a list of all object file names. We would
752 define such a variable @code{objects} with a line like this in the
757 objects = main.o kbd.o command.o display.o \
758 insert.o search.o files.o utils.o
763 Then, each place we want to put a list of the object file names, we can
764 substitute the variable's value by writing @samp{$(objects)}
765 (@pxref{Using Variables, ,How to Use Variables}).
767 Here is how the complete simple makefile looks when you use a variable
768 for the object files:
772 objects = main.o kbd.o command.o display.o \
773 insert.o search.o files.o utils.o
776 cc -o edit $(objects)
777 main.o : main.c defs.h
779 kbd.o : kbd.c defs.h command.h
781 command.o : command.c defs.h command.h
783 display.o : display.c defs.h buffer.h
785 insert.o : insert.c defs.h buffer.h
787 search.o : search.c defs.h buffer.h
789 files.o : files.c defs.h buffer.h command.h
791 utils.o : utils.c defs.h
798 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
799 @section Letting @code{make} Deduce the Recipes
800 @cindex deducing recipes (implicit rules)
801 @cindex implicit rule, introduction to
802 @cindex rule, implicit, introduction to
804 It is not necessary to spell out the recipes for compiling the individual
805 C source files, because @code{make} can figure them out: it has an
806 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
807 named @samp{.c} file using a @samp{cc -c} command. For example, it will
808 use the recipe @samp{cc -c main.c -o main.o} to compile @file{main.c} into
809 @file{main.o}. We can therefore omit the recipes from the rules for the
810 object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill
812 When a @samp{.c} file is used automatically in this way, it is also
813 automatically added to the list of prerequisites. We can therefore omit
814 the @samp{.c} files from the prerequisites, provided we omit the recipe.
816 Here is the entire example, with both of these changes, and a variable
817 @code{objects} as suggested above:
821 objects = main.o kbd.o command.o display.o \
822 insert.o search.o files.o utils.o
825 cc -o edit $(objects)
828 kbd.o : defs.h command.h
829 command.o : defs.h command.h
830 display.o : defs.h buffer.h
831 insert.o : defs.h buffer.h
832 search.o : defs.h buffer.h
833 files.o : defs.h buffer.h command.h
843 This is how we would write the makefile in actual practice. (The
844 complications associated with @samp{clean} are described elsewhere.
845 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Recipes}.)
847 Because implicit rules are so convenient, they are important. You
848 will see them used frequently.@refill
850 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
851 @section Another Style of Makefile
852 @cindex combining rules by prerequisite
854 When the objects of a makefile are created only by implicit rules, an
855 alternative style of makefile is possible. In this style of makefile,
856 you group entries by their prerequisites instead of by their targets.
857 Here is what one looks like:
861 objects = main.o kbd.o command.o display.o \
862 insert.o search.o files.o utils.o
865 cc -o edit $(objects)
868 kbd.o command.o files.o : command.h
869 display.o insert.o search.o files.o : buffer.h
874 Here @file{defs.h} is given as a prerequisite of all the object files;
875 @file{command.h} and @file{buffer.h} are prerequisites of the specific
876 object files listed for them.
878 Whether this is better is a matter of taste: it is more compact, but some
879 people dislike it because they find it clearer to put all the information
880 about each target in one place.
882 @node Cleanup, , Combine By Prerequisite, Introduction
883 @section Rules for Cleaning the Directory
885 @cindex removing, to clean up
887 Compiling a program is not the only thing you might want to write rules
888 for. Makefiles commonly tell how to do a few other things besides
889 compiling a program: for example, how to delete all the object files
890 and executables so that the directory is @samp{clean}.
892 @cindex @code{clean} target
894 could write a @code{make} rule for cleaning our example editor:
903 In practice, we might want to write the rule in a somewhat more
904 complicated manner to handle unanticipated situations. We would do this:
915 This prevents @code{make} from getting confused by an actual file
916 called @file{clean} and causes it to continue in spite of errors from
917 @code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
921 A rule such as this should not be placed at the beginning of the
922 makefile, because we do not want it to run by default! Thus, in the
923 example makefile, we want the rule for @code{edit}, which recompiles
924 the editor, to remain the default goal.
926 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
927 run at all if we give the command @samp{make} with no arguments. In
928 order to make the rule run, we have to type @samp{make clean}.
929 @xref{Running, ,How to Run @code{make}}.
931 @node Makefiles, Rules, Introduction, Top
932 @chapter Writing Makefiles
934 @cindex makefile, how to write
935 The information that tells @code{make} how to recompile a system comes from
936 reading a data base called the @dfn{makefile}.
939 * Makefile Contents:: What makefiles contain.
940 * Makefile Names:: How to name your makefile.
941 * Include:: How one makefile can use another makefile.
942 * MAKEFILES Variable:: The environment can specify extra makefiles.
943 * Remaking Makefiles:: How makefiles get remade.
944 * Overriding Makefiles:: How to override part of one makefile
945 with another makefile.
946 * Reading Makefiles:: How makefiles are parsed.
947 * Secondary Expansion:: How and when secondary expansion is performed.
950 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
951 @section What Makefiles Contain
953 Makefiles contain five kinds of things: @dfn{explicit rules},
954 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
955 and @dfn{comments}. Rules, variables, and directives are described at
956 length in later chapters.@refill
959 @cindex rule, explicit, definition of
960 @cindex explicit rule, definition of
962 An @dfn{explicit rule} says when and how to remake one or more files,
963 called the rule's @dfn{targets}. It lists the other files that the
964 targets depend on, called the @dfn{prerequisites} of the target, and
965 may also give a recipe to use to create or update the targets.
966 @xref{Rules, ,Writing Rules}.
968 @cindex rule, implicit, definition of
969 @cindex implicit rule, definition of
971 An @dfn{implicit rule} says when and how to remake a class of files
972 based on their names. It describes how a target may depend on a file
973 with a name similar to the target and gives a recipe to create or
974 update such a target. @xref{Implicit Rules, ,Using Implicit Rules}.
976 @cindex variable definition
978 A @dfn{variable definition} is a line that specifies a text string
979 value for a variable that can be substituted into the text later. The
980 simple makefile example shows a variable definition for @code{objects}
981 as a list of all object files (@pxref{Variables Simplify, , Variables
982 Make Makefiles Simpler}).
986 A @dfn{directive} is an instruction for @code{make} to do something
987 special while reading the makefile. These include:
991 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
994 Deciding (based on the values of variables) whether to use or
995 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
998 Defining a variable from a verbatim string containing multiple lines
999 (@pxref{Multi-Line, ,Defining Multi-Line Variables}).
1002 @cindex comments, in makefile
1003 @cindex @code{#} (comments), in makefile
1005 @samp{#} in a line of a makefile starts a @dfn{comment}. It and the
1006 rest of the line are ignored, except that a trailing backslash not
1007 escaped by another backslash will continue the comment across multiple
1008 lines. A line containing just a comment (with perhaps spaces before
1009 it) is effectively blank, and is ignored. If you want a literal
1010 @code{#}, escape it with a backslash (e.g., @code{\#}). Comments may
1011 appear on any line in the makefile, although they are treated
1012 specially in certain situations.
1014 You cannot use comments within variable references or function calls:
1015 any instance of @code{#} will be treated literally (rather than as the
1016 start of a comment) inside a variable reference or function call.
1018 Comments within a recipe are passed to the shell, just as with any
1019 other recipe text. The shell decides how to interpret it: whether or
1020 not this is a comment is up to the shell.
1022 Within a @code{define} directive, comments are not ignored during the
1023 definition of the variable, but rather kept intact in the value of the
1024 variable. When the variable is expanded they will either be treated
1025 as @code{make} comments or as recipe text, depending on the context in
1026 which the variable is evaluated.
1029 @node Makefile Names, Include, Makefile Contents, Makefiles
1030 @section What Name to Give Your Makefile
1031 @cindex makefile name
1032 @cindex name of makefile
1033 @cindex default makefile name
1034 @cindex file name of makefile
1036 @c following paragraph rewritten to avoid overfull hbox
1037 By default, when @code{make} looks for the makefile, it tries the
1038 following names, in order: @file{GNUmakefile}, @file{makefile}
1039 and @file{Makefile}.@refill
1044 @cindex @code{README}
1045 Normally you should call your makefile either @file{makefile} or
1046 @file{Makefile}. (We recommend @file{Makefile} because it appears
1047 prominently near the beginning of a directory listing, right near other
1048 important files such as @file{README}.) The first name checked,
1049 @file{GNUmakefile}, is not recommended for most makefiles. You should
1050 use this name if you have a makefile that is specific to GNU
1051 @code{make}, and will not be understood by other versions of
1052 @code{make}. Other @code{make} programs look for @file{makefile} and
1053 @file{Makefile}, but not @file{GNUmakefile}.
1055 If @code{make} finds none of these names, it does not use any makefile.
1056 Then you must specify a goal with a command argument, and @code{make}
1057 will attempt to figure out how to remake it using only its built-in
1058 implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}.
1061 @cindex @code{--file}
1062 @cindex @code{--makefile}
1063 If you want to use a nonstandard name for your makefile, you can specify
1064 the makefile name with the @samp{-f} or @samp{--file} option. The
1065 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1066 @code{make} to read the file @var{name} as the makefile. If you use
1067 more than one @samp{-f} or @samp{--file} option, you can specify several
1068 makefiles. All the makefiles are effectively concatenated in the order
1069 specified. The default makefile names @file{GNUmakefile},
1070 @file{makefile} and @file{Makefile} are not checked automatically if you
1071 specify @samp{-f} or @samp{--file}.@refill
1072 @cindex specifying makefile name
1073 @cindex makefile name, how to specify
1074 @cindex name of makefile, how to specify
1075 @cindex file name of makefile, how to specify
1077 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1078 @section Including Other Makefiles
1079 @cindex including other makefiles
1080 @cindex makefile, including
1083 The @code{include} directive tells @code{make} to suspend reading the
1084 current makefile and read one or more other makefiles before continuing.
1085 The directive is a line in the makefile that looks like this:
1088 include @var{filenames}@dots{}
1092 @var{filenames} can contain shell file name patterns. If
1093 @var{filenames} is empty, nothing is included and no error is printed.
1094 @cindex shell file name pattern (in @code{include})
1095 @cindex shell wildcards (in @code{include})
1096 @cindex wildcard, in @code{include}
1098 Extra spaces are allowed and ignored at the beginning of the line, but
1099 the first character must not be a tab (or the value of
1100 @code{.CMDPREFIX})---if the line begins with a tab, it will be
1101 considered a recipe line. Whitespace is required between
1102 @code{include} and the file names, and between file names; extra
1103 whitespace is ignored there and at the end of the directive. A
1104 comment starting with @samp{#} is allowed at the end of the line. If
1105 the file names contain any variable or function references, they are
1106 expanded. @xref{Using Variables, ,How to Use Variables}.
1108 For example, if you have three @file{.mk} files, @file{a.mk},
1109 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1110 @code{bish bash}, then the following expression
1113 include foo *.mk $(bar)
1119 include foo a.mk b.mk c.mk bish bash
1122 When @code{make} processes an @code{include} directive, it suspends
1123 reading of the containing makefile and reads from each listed file in
1124 turn. When that is finished, @code{make} resumes reading the
1125 makefile in which the directive appears.
1127 One occasion for using @code{include} directives is when several programs,
1128 handled by individual makefiles in various directories, need to use a
1129 common set of variable definitions
1130 (@pxref{Setting, ,Setting Variables}) or pattern rules
1131 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1133 Another such occasion is when you want to generate prerequisites from
1134 source files automatically; the prerequisites can be put in a file that
1135 is included by the main makefile. This practice is generally cleaner
1136 than that of somehow appending the prerequisites to the end of the main
1137 makefile as has been traditionally done with other versions of
1138 @code{make}. @xref{Automatic Prerequisites}.
1139 @cindex prerequisites, automatic generation
1140 @cindex automatic generation of prerequisites
1141 @cindex generating prerequisites automatically
1144 @cindex @code{--include-dir}
1145 @cindex included makefiles, default directories
1146 @cindex default directories for included makefiles
1147 @findex /usr/gnu/include
1148 @findex /usr/local/include
1149 @findex /usr/include
1150 If the specified name does not start with a slash, and the file is not
1151 found in the current directory, several other directories are searched.
1152 First, any directories you have specified with the @samp{-I} or
1153 @samp{--include-dir} option are searched
1154 (@pxref{Options Summary, ,Summary of Options}).
1155 Then the following directories (if they exist)
1156 are searched, in this order:
1157 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1158 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1159 @var{prefix} has been defined to be the root of the DJGPP tree
1161 @file{/usr/gnu/include},
1162 @file{/usr/local/include}, @file{/usr/include}.
1164 If an included makefile cannot be found in any of these directories, a
1165 warning message is generated, but it is not an immediately fatal error;
1166 processing of the makefile containing the @code{include} continues.
1167 Once it has finished reading makefiles, @code{make} will try to remake
1168 any that are out of date or don't exist.
1169 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1170 Only after it has tried to find a way to remake a makefile and failed,
1171 will @code{make} diagnose the missing makefile as a fatal error.
1173 If you want @code{make} to simply ignore a makefile which does not exist
1174 or cannot be remade, with no error message, use the @w{@code{-include}}
1175 directive instead of @code{include}, like this:
1178 -include @var{filenames}@dots{}
1181 This acts like @code{include} in every way except that there is no
1182 error (not even a warning) if any of the @var{filenames} (or any
1183 prerequisites of any of the @var{filenames}) do not exist or cannot be
1186 For compatibility with some other @code{make} implementations,
1187 @code{sinclude} is another name for @w{@code{-include}}.
1189 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1190 @section The Variable @code{MAKEFILES}
1191 @cindex makefile, and @code{MAKEFILES} variable
1192 @cindex including (@code{MAKEFILES} variable)
1195 If the environment variable @code{MAKEFILES} is defined, @code{make}
1196 considers its value as a list of names (separated by whitespace) of
1197 additional makefiles to be read before the others. This works much
1198 like the @code{include} directive: various directories are searched
1199 for those files (@pxref{Include, ,Including Other Makefiles}). In
1200 addition, the default goal is never taken from one of these makefiles
1201 (or any makefile included by them) and it is not an error if the files
1202 listed in @code{MAKEFILES} are not found.@refill
1204 @cindex recursion, and @code{MAKEFILES} variable
1205 The main use of @code{MAKEFILES} is in communication between recursive
1206 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1207 @code{make}}). It usually is not desirable to set the environment
1208 variable before a top-level invocation of @code{make}, because it is
1209 usually better not to mess with a makefile from outside. However, if
1210 you are running @code{make} without a specific makefile, a makefile in
1211 @code{MAKEFILES} can do useful things to help the built-in implicit
1212 rules work better, such as defining search paths (@pxref{Directory Search}).
1214 Some users are tempted to set @code{MAKEFILES} in the environment
1215 automatically on login, and program makefiles to expect this to be done.
1216 This is a very bad idea, because such makefiles will fail to work if run by
1217 anyone else. It is much better to write explicit @code{include} directives
1218 in the makefiles. @xref{Include, , Including Other Makefiles}.
1220 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1221 @section How Makefiles Are Remade
1223 @cindex updating makefiles
1224 @cindex remaking makefiles
1225 @cindex makefile, remaking of
1226 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1227 files. If a makefile can be remade from other files, you probably want
1228 @code{make} to get an up-to-date version of the makefile to read in.
1230 To this end, after reading in all makefiles, @code{make} will consider
1231 each as a goal target and attempt to update it. If a makefile has a
1232 rule which says how to update it (found either in that very makefile or
1233 in another one) or if an implicit rule applies to it (@pxref{Implicit
1234 Rules, ,Using Implicit Rules}), it will be updated if necessary. After
1235 all makefiles have been checked, if any have actually been changed,
1236 @code{make} starts with a clean slate and reads all the makefiles over
1237 again. (It will also attempt to update each of them over again, but
1238 normally this will not change them again, since they are already up to
1241 If you know that one or more of your makefiles cannot be remade and
1242 you want to keep @code{make} from performing an implicit rule search
1243 on them, perhaps for efficiency reasons, you can use any normal method
1244 of preventing implicit rule lookup to do so. For example, you can
1245 write an explicit rule with the makefile as the target, and an empty
1246 recipe (@pxref{Empty Recipes, ,Using Empty Recipes}).
1248 If the makefiles specify a double-colon rule to remake a file with
1249 a recipe but no prerequisites, that file will always be remade
1250 (@pxref{Double-Colon}). In the case of makefiles, a makefile that has a
1251 double-colon rule with a recipe but no prerequisites will be remade every
1252 time @code{make} is run, and then again after @code{make} starts over
1253 and reads the makefiles in again. This would cause an infinite loop:
1254 @code{make} would constantly remake the makefile, and never do anything
1255 else. So, to avoid this, @code{make} will @strong{not} attempt to
1256 remake makefiles which are specified as targets of a double-colon rule
1257 with a recipe but no prerequisites.@refill
1259 If you do not specify any makefiles to be read with @samp{-f} or
1260 @samp{--file} options, @code{make} will try the default makefile names;
1261 @pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike
1262 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1263 @code{make} is not certain that these makefiles should exist. However,
1264 if a default makefile does not exist but can be created by running
1265 @code{make} rules, you probably want the rules to be run so that the
1266 makefile can be used.
1268 Therefore, if none of the default makefiles exists, @code{make} will try
1269 to make each of them in the same order in which they are searched for
1270 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1271 until it succeeds in making one, or it runs out of names to try. Note
1272 that it is not an error if @code{make} cannot find or make any makefile;
1273 a makefile is not always necessary.@refill
1275 When you use the @samp{-t} or @samp{--touch} option
1276 (@pxref{Instead of Execution, ,Instead of Executing Recipes}),
1277 you would not want to use an out-of-date makefile to decide which
1278 targets to touch. So the @samp{-t} option has no effect on updating
1279 makefiles; they are really updated even if @samp{-t} is specified.
1280 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1281 @samp{--just-print}) do not prevent updating of makefiles, because an
1282 out-of-date makefile would result in the wrong output for other targets.
1283 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1284 and then print the recipe to update @file{foo} and its prerequisites
1285 without running it. The recipe printed for @file{foo} will be the one
1286 specified in the updated contents of @file{mfile}.
1288 However, on occasion you might actually wish to prevent updating of even
1289 the makefiles. You can do this by specifying the makefiles as goals in
1290 the command line as well as specifying them as makefiles. When the
1291 makefile name is specified explicitly as a goal, the options @samp{-t}
1292 and so on do apply to them.
1294 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1295 @file{mfile}, print the recipe needed to update it without actually
1296 running it, and then print the recipe needed to update @file{foo}
1297 without running that. The recipe for @file{foo} will be the one
1298 specified by the existing contents of @file{mfile}.
1300 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1301 @section Overriding Part of Another Makefile
1303 @cindex overriding makefiles
1304 @cindex makefile, overriding
1305 Sometimes it is useful to have a makefile that is mostly just like
1306 another makefile. You can often use the @samp{include} directive to
1307 include one in the other, and add more targets or variable definitions.
1308 However, it is illegal for two makefiles to give different recipes for
1309 the same target. But there is another way.
1311 @cindex match-anything rule, used to override
1312 In the containing makefile (the one that wants to include the other),
1313 you can use a match-anything pattern rule to say that to remake any
1314 target that cannot be made from the information in the containing
1315 makefile, @code{make} should look in another makefile.
1316 @xref{Pattern Rules}, for more information on pattern rules.
1318 For example, if you have a makefile called @file{Makefile} that says how
1319 to make the target @samp{foo} (and other targets), you can write a
1320 makefile called @file{GNUmakefile} that contains:
1327 @@$(MAKE) -f Makefile $@@
1331 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1332 read it, and see that to make @file{foo}, it needs to run the recipe
1333 @samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will
1334 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1335 recipe from the pattern rule: @samp{make -f Makefile bar}. If
1336 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1337 will apply the rule. And likewise for any other target that
1338 @file{GNUmakefile} does not say how to make.
1340 The way this works is that the pattern rule has a pattern of just
1341 @samp{%}, so it matches any target whatever. The rule specifies a
1342 prerequisite @file{force}, to guarantee that the recipe will be run even
1343 if the target file already exists. We give the @file{force} target an
1344 empty recipe to prevent @code{make} from searching for an implicit rule to
1345 build it---otherwise it would apply the same match-anything rule to
1346 @file{force} itself and create a prerequisite loop!
1348 @node Reading Makefiles, Secondary Expansion, Overriding Makefiles, Makefiles
1349 @section How @code{make} Reads a Makefile
1350 @cindex reading makefiles
1351 @cindex makefile, parsing
1353 GNU @code{make} does its work in two distinct phases. During the first
1354 phase it reads all the makefiles, included makefiles, etc. and
1355 internalizes all the variables and their values, implicit and explicit
1356 rules, and constructs a dependency graph of all the targets and their
1357 prerequisites. During the second phase, @code{make} uses these internal
1358 structures to determine what targets will need to be rebuilt and to
1359 invoke the rules necessary to do so.
1361 It's important to understand this two-phase approach because it has a
1362 direct impact on how variable and function expansion happens; this is
1363 often a source of some confusion when writing makefiles. Here we will
1364 present a summary of the phases in which expansion happens for different
1365 constructs within the makefile. We say that expansion is
1366 @dfn{immediate} if it happens during the first phase: in this case
1367 @code{make} will expand any variables or functions in that section of a
1368 construct as the makefile is parsed. We say that expansion is
1369 @dfn{deferred} if expansion is not performed immediately. Expansion of
1370 a deferred construct is not performed until either the construct appears
1371 later in an immediate context, or until the second phase.
1373 You may not be familiar with some of these constructs yet. You can
1374 reference this section as you become familiar with them, in later
1377 @subheading Variable Assignment
1378 @cindex +=, expansion
1379 @cindex =, expansion
1380 @cindex ?=, expansion
1381 @cindex +=, expansion
1382 @cindex define, expansion
1384 Variable definitions are parsed as follows:
1387 @var{immediate} = @var{deferred}
1388 @var{immediate} ?= @var{deferred}
1389 @var{immediate} := @var{immediate}
1390 @var{immediate} += @var{deferred} or @var{immediate}
1392 define @var{immediate}
1396 define @var{immediate} =
1400 define @var{immediate} ?=
1404 define @var{immediate} :=
1408 define @var{immediate} +=
1409 @var{deferred} or @var{immediate}
1413 For the append operator, @samp{+=}, the right-hand side is considered
1414 immediate if the variable was previously set as a simple variable
1415 (@samp{:=}), and deferred otherwise.
1417 @subheading Conditional Directives
1418 @cindex ifdef, expansion
1419 @cindex ifeq, expansion
1420 @cindex ifndef, expansion
1421 @cindex ifneq, expansion
1423 Conditional directives are parsed immediately. This means, for
1424 example, that automatic variables cannot be used in conditional
1425 directives, as automatic variables are not set until the recipe for
1426 that rule is invoked. If you need to use automatic variables in a
1427 conditional directive you @emph{must} move the condition into the
1428 recipe and use shell conditional syntax instead.
1430 @subheading Rule Definition
1431 @cindex target, expansion
1432 @cindex prerequisite, expansion
1433 @cindex implicit rule, expansion
1434 @cindex pattern rule, expansion
1435 @cindex explicit rule, expansion
1437 A rule is always expanded the same way, regardless of the form:
1440 @var{immediate} : @var{immediate} ; @var{deferred}
1444 That is, the target and prerequisite sections are expanded immediately,
1445 and the recipe used to construct the target is always deferred. This
1446 general rule is true for explicit rules, pattern rules, suffix rules,
1447 static pattern rules, and simple prerequisite definitions.
1449 @node Secondary Expansion, , Reading Makefiles, Makefiles
1450 @section Secondary Expansion
1451 @cindex secondary expansion
1452 @cindex expansion, secondary
1454 @findex .SECONDEXPANSION
1455 In the previous section we learned that GNU @code{make} works in two
1456 distinct phases: a read-in phase and a target-update phase
1457 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}). GNU
1458 make also has the ability to enable a @emph{second expansion} of the
1459 prerequisites (only) for some or all targets defined in the makefile.
1460 In order for this second expansion to occur, the special target
1461 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1462 list that makes use of this feature.
1464 If that special target is defined then in between the two phases
1465 mentioned above, right at the end of the read-in phase, all the
1466 prerequisites of the targets defined after the special target are
1467 expanded a @emph{second time}. In most circumstances this secondary
1468 expansion will have no effect, since all variable and function
1469 references will have been expanded during the initial parsing of the
1470 makefiles. In order to take advantage of the secondary expansion
1471 phase of the parser, then, it's necessary to @emph{escape} the
1472 variable or function reference in the makefile. In this case the
1473 first expansion merely un-escapes the reference but doesn't expand it,
1474 and expansion is left to the secondary expansion phase. For example,
1475 consider this makefile:
1481 myfile: $(ONEVAR) $$(TWOVAR)
1484 After the first expansion phase the prerequisites list of the
1485 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1486 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1487 while the second (escaped) variable reference is simply unescaped,
1488 without being recognized as a variable reference. Now during the
1489 secondary expansion the first word is expanded again but since it
1490 contains no variable or function references it remains the static
1491 value @file{onefile}, while the second word is now a normal reference
1492 to the variable @var{TWOVAR}, which is expanded to the value
1493 @file{twofile}. The final result is that there are two prerequisites,
1494 @file{onefile} and @file{twofile}.
1496 Obviously, this is not a very interesting case since the same result
1497 could more easily have been achieved simply by having both variables
1498 appear, unescaped, in the prerequisites list. One difference becomes
1499 apparent if the variables are reset; consider this example:
1509 Here the prerequisite of @file{onefile} will be expanded immediately,
1510 and resolve to the value @file{top}, while the prerequisite of
1511 @file{twofile} will not be full expanded until the secondary expansion
1512 and yield a value of @file{bottom}.
1514 This is marginally more exciting, but the true power of this feature
1515 only becomes apparent when you discover that secondary expansions
1516 always take place within the scope of the automatic variables for that
1517 target. This means that you can use variables such as @code{$@@},
1518 @code{$*}, etc. during the second expansion and they will have their
1519 expected values, just as in the recipe. All you have to do is defer
1520 the expansion by escaping the @code{$}. Also, secondary expansion
1521 occurs for both explicit and implicit (pattern) rules. Knowing this,
1522 the possible uses for this feature increase dramatically. For
1527 main_OBJS := main.o try.o test.o
1528 lib_OBJS := lib.o api.o
1530 main lib: $$($$@@_OBJS)
1533 Here, after the initial expansion the prerequisites of both the
1534 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}. During
1535 the secondary expansion, the @code{$@@} variable is set to the name of
1536 the target and so the expansion for the @file{main} target will yield
1537 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1538 secondary expansion for the @file{lib} target will yield
1539 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1541 You can also mix in functions here, as long as they are properly escaped:
1544 main_SRCS := main.c try.c test.c
1545 lib_SRCS := lib.c api.c
1548 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1551 This version allows users to specify source files rather than object
1552 files, but gives the same resulting prerequisites list as the previous
1555 Evaluation of automatic variables during the secondary expansion
1556 phase, especially of the target name variable @code{$$@@}, behaves
1557 similarly to evaluation within recipes. However, there are some
1558 subtle differences and ``corner cases'' which come into play for the
1559 different types of rule definitions that @code{make} understands. The
1560 subtleties of using the different automatic variables are described
1563 @subheading Secondary Expansion of Explicit Rules
1564 @cindex secondary expansion and explicit rules
1565 @cindex explicit rules, secondary expansion of
1567 During the secondary expansion of explicit rules, @code{$$@@} and
1568 @code{$$%} evaluate, respectively, to the file name of the target and,
1569 when the target is an archive member, the target member name. The
1570 @code{$$<} variable evaluates to the first prerequisite in the first
1571 rule for this target. @code{$$^} and @code{$$+} evaluate to the list
1572 of all prerequisites of rules @emph{that have already appeared} for
1573 the same target (@code{$$+} with repetitions and @code{$$^}
1574 without). The following example will help illustrate these behaviors:
1579 foo: foo.1 bar.1 $$< $$^ $$+ # line #1
1581 foo: foo.2 bar.2 $$< $$^ $$+ # line #2
1583 foo: foo.3 bar.3 $$< $$^ $$+ # line #3
1586 In the first prerequisite list, all three variables (@code{$$<},
1587 @code{$$^}, and @code{$$+}) expand to the empty string. In the
1588 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1589 @code{foo.1 bar.1} respectively. In the third they will have values
1590 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1591 foo.2 bar.2 foo.1 foo.1 bar.1 foo.1 bar.1} respectively.
1593 Rules undergo secondary expansion in makefile order, except that
1594 the rule with the recipe is always evaluated last.
1596 The variables @code{$$?} and @code{$$*} are not available and expand
1597 to the empty string.
1599 @subheading Secondary Expansion of Static Pattern Rules
1600 @cindex secondary expansion and static pattern rules
1601 @cindex static pattern rules, secondary expansion of
1603 Rules for secondary expansion of static pattern rules are identical to
1604 those for explicit rules, above, with one exception: for static
1605 pattern rules the @code{$$*} variable is set to the pattern stem. As
1606 with explicit rules, @code{$$?} is not available and expands to the
1609 @subheading Secondary Expansion of Implicit Rules
1610 @cindex secondary expansion and implicit rules
1611 @cindex implicit rules, secondary expansion of
1613 As @code{make} searches for an implicit rule, it substitutes the stem
1614 and then performs secondary expansion for every rule with a matching
1615 target pattern. The value of the automatic variables is derived in
1616 the same fashion as for static pattern rules. As an example:
1625 %oo: $$< $$^ $$+ $$*
1628 When the implicit rule is tried for target @file{foo}, @code{$$<}
1629 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1630 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1633 Note that the directory prefix (D), as described in @ref{Implicit Rule
1634 Search, ,Implicit Rule Search Algorithm}, is appended (after
1635 expansion) to all the patterns in the prerequisites list. As an
1643 %.o: $$(addsuffix /%.c,foo bar) foo.h
1646 The prerequisite list after the secondary expansion and directory
1647 prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1648 foo.h}. If you are not interested in this reconstruction, you can use
1649 @code{$$*} instead of @code{%} in the prerequisites list.
1651 @node Rules, Recipes, Makefiles, Top
1652 @chapter Writing Rules
1653 @cindex writing rules
1654 @cindex rule, how to write
1656 @cindex prerequisite
1658 A @dfn{rule} appears in the makefile and says when and how to remake
1659 certain files, called the rule's @dfn{targets} (most often only one per rule).
1660 It lists the other files that are the @dfn{prerequisites} of the target, and
1661 the @dfn{recipe} to use to create or update the target.
1663 @cindex default goal
1664 @cindex goal, default
1665 The order of rules is not significant, except for determining the
1666 @dfn{default goal}: the target for @code{make} to consider, if you do
1667 not otherwise specify one. The default goal is the target of the first
1668 rule in the first makefile. If the first rule has multiple targets,
1669 only the first target is taken as the default. There are two
1670 exceptions: a target starting with a period is not a default unless it
1671 contains one or more slashes, @samp{/}, as well; and, a target that
1672 defines a pattern rule has no effect on the default goal.
1673 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1675 Therefore, we usually write the makefile so that the first rule is the
1676 one for compiling the entire program or all the programs described by
1677 the makefile (often with a target called @samp{all}).
1678 @xref{Goals, ,Arguments to Specify the Goals}.
1681 * Rule Example:: An example explained.
1682 * Rule Syntax:: General syntax explained.
1683 * Prerequisite Types:: There are two types of prerequisites.
1684 * Wildcards:: Using wildcard characters such as `*'.
1685 * Directory Search:: Searching other directories for source files.
1686 * Phony Targets:: Using a target that is not a real file's name.
1687 * Force Targets:: You can use a target without recipes
1688 or prerequisites to mark other targets
1690 * Empty Targets:: When only the date matters and the
1692 * Special Targets:: Targets with special built-in meanings.
1693 * Multiple Targets:: When to make use of several targets in a rule.
1694 * Multiple Rules:: How to use several rules with the same target.
1695 * Static Pattern:: Static pattern rules apply to multiple targets
1696 and can vary the prerequisites according to
1698 * Double-Colon:: How to use a special kind of rule to allow
1699 several independent rules for one target.
1700 * Automatic Prerequisites:: How to automatically generate rules giving
1701 prerequisites from source files themselves.
1705 @node Rule Example, Rule Syntax, Rules, Rules
1706 @section Rule Example
1708 Here is an example of a rule:
1711 foo.o : foo.c defs.h # module for twiddling the frobs
1715 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1716 @file{defs.h}. It has one command in the recipe: @samp{cc -c -g foo.c}.
1717 The recipe starts with a tab to identify it as a recipe.
1719 This rule says two things:
1723 How to decide whether @file{foo.o} is out of date: it is out of date
1724 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1725 more recent than it.
1728 How to update the file @file{foo.o}: by running @code{cc} as stated.
1729 The recipe does not explicitly mention @file{defs.h}, but we presume
1730 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1731 added to the prerequisites.
1735 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1736 @section Rule Syntax
1739 @cindex syntax of rules
1740 In general, a rule looks like this:
1743 @var{targets} : @var{prerequisites}
1752 @var{targets} : @var{prerequisites} ; @var{recipe}
1758 @cindex rule targets
1759 The @var{targets} are file names, separated by spaces. Wildcard
1760 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1761 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1762 represents member @var{m} in archive file @var{a}
1763 (@pxref{Archive Members, ,Archive Members as Targets}).
1764 Usually there is only one
1765 target per rule, but occasionally there is a reason to have more
1766 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1769 @cindex tab character (in commands)
1770 The @var{recipe} lines start with a tab character (or the first
1771 character in the value of the @code{.CMDPREFIX} variable;
1772 @pxref{Special Variables}). The first recipe line may appear on the line
1773 after the prerequisites, with a tab character, or may appear on the
1774 same line, with a semicolon. Either way, the effect is the same.
1775 There are other differences in the syntax of recipes.
1776 @xref{Recipes, ,Writing Recipes in Rules}.
1778 @cindex dollar sign (@code{$}), in rules
1779 @cindex @code{$}, in rules
1780 @cindex rules, and @code{$}
1781 Because dollar signs are used to start @code{make} variable
1782 references, if you really want a dollar sign in a target or
1783 prerequisite you must write two of them, @samp{$$} (@pxref{Using
1784 Variables, ,How to Use Variables}). If you have enabled secondary
1785 expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1786 sign in the prerequisites list, you must actually write @emph{four}
1787 dollar signs (@samp{$$$$}).
1789 You may split a long line by inserting a backslash followed by a
1790 newline, but this is not required, as @code{make} places no limit on
1791 the length of a line in a makefile.
1793 A rule tells @code{make} two things: when the targets are out of date,
1794 and how to update them when necessary.
1796 @cindex prerequisites
1797 @cindex rule prerequisites
1798 The criterion for being out of date is specified in terms of the
1799 @var{prerequisites}, which consist of file names separated by spaces.
1800 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1801 A target is out of date if it does not exist or if it is older than any
1802 of the prerequisites (by comparison of last-modification times). The
1803 idea is that the contents of the target file are computed based on
1804 information in the prerequisites, so if any of the prerequisites changes,
1805 the contents of the existing target file are no longer necessarily
1808 How to update is specified by a @var{recipe}. This is one or more
1809 lines to be executed by the shell (normally @samp{sh}), but with some
1810 extra features (@pxref{Recipes, ,Writing Recipes in Rules}).
1812 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1813 @comment node-name, next, previous, up
1814 @section Types of Prerequisites
1815 @cindex prerequisite types
1816 @cindex types of prerequisites
1818 @cindex prerequisites, normal
1819 @cindex normal prerequisites
1820 @cindex prerequisites, order-only
1821 @cindex order-only prerequisites
1822 There are actually two different types of prerequisites understood by
1823 GNU @code{make}: normal prerequisites such as described in the
1824 previous section, and @dfn{order-only} prerequisites. A normal
1825 prerequisite makes two statements: first, it imposes an order of
1826 execution of recipes: any recipes necessary to build any of a
1827 target's prerequisites will be fully executed before any recipe
1828 necessary to build the target. Second, it imposes a dependency
1829 relationship: if any prerequisite is newer than the target, then the
1830 target is considered out-of-date and must be rebuilt.
1832 Normally, this is exactly what you want: if a target's prerequisite is
1833 updated, then the target should also be updated.
1835 Occasionally, however, you have a situation where you want to impose a
1836 specific ordering on the rules to be invoked @emph{without} forcing
1837 the target to be updated if one of those rules is executed. In that
1838 case, you want to define @dfn{order-only} prerequisites. Order-only
1839 prerequisites can be specified by placing a pipe symbol (@code{|})
1840 in the prerequisites list: any prerequisites to the left of the pipe
1841 symbol are normal; any prerequisites to the right are order-only:
1844 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1847 The normal prerequisites section may of course be empty. Also, you
1848 may still declare multiple lines of prerequisites for the same target:
1849 they are appended appropriately (normal prerequisites are appended to
1850 the list of normal prerequisites; order-only prerequisites are
1851 appended to the list of order-only prerequisites). Note that if you
1852 declare the same file to be both a normal and an order-only
1853 prerequisite, the normal prerequisite takes precedence (since they
1854 have a strict superset of the behavior of an order-only prerequisite).
1856 Consider an example where your targets are to be placed in a separate
1857 directory, and that directory might not exist before @code{make} is
1858 run. In this situation, you want the directory to be created before
1859 any targets are placed into it but, because the timestamps on
1860 directories change whenever a file is added, removed, or renamed, we
1861 certainly don't want to rebuild all the targets whenever the
1862 directory's timestamp changes. One way to manage this is with
1863 order-only prerequisites: make the directory an order-only
1864 prerequisite on all the targets:
1868 OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
1871 $(COMPILE.c) $(OUTPUT_OPTION) $<
1875 $(OBJS): | $(OBJDIR)
1881 Now the rule to create the @file{objdir} directory will be run, if
1882 needed, before any @samp{.o} is built, but no @samp{.o} will be built
1883 because the @file{objdir} directory timestamp changed.
1885 @node Wildcards, Directory Search, Prerequisite Types, Rules
1886 @section Using Wildcard Characters in File Names
1888 @cindex file name with wildcards
1889 @cindex globbing (wildcards)
1891 @cindex @code{*} (wildcard character)
1892 @cindex @code{?} (wildcard character)
1893 @cindex @code{[@dots{}]} (wildcard characters)
1894 A single file name can specify many files using @dfn{wildcard characters}.
1895 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1896 @samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c}
1897 specifies a list of all the files (in the working directory) whose names
1898 end in @samp{.c}.@refill
1900 @cindex @code{~} (tilde)
1901 @cindex tilde (@code{~})
1902 @cindex home directory
1903 The character @samp{~} at the beginning of a file name also has special
1904 significance. If alone, or followed by a slash, it represents your home
1905 directory. For example @file{~/bin} expands to @file{/home/you/bin}.
1906 If the @samp{~} is followed by a word, the string represents the home
1907 directory of the user named by that word. For example @file{~john/bin}
1908 expands to @file{/home/john/bin}. On systems which don't have a home
1909 directory for each user (such as MS-DOS or MS-Windows), this
1910 functionality can be simulated by setting the environment variable
1913 Wildcard expansion is performed by @code{make} automatically in
1914 targets and in prerequisites. In recipes, the shell is responsible
1915 for wildcard expansion. In other contexts, wildcard expansion happens
1916 only if you request it explicitly with the @code{wildcard} function.
1918 The special significance of a wildcard character can be turned off by
1919 preceding it with a backslash. Thus, @file{foo\*bar} would refer to a
1920 specific file whose name consists of @samp{foo}, an asterisk, and
1924 * Wildcard Examples:: Several examples
1925 * Wildcard Pitfall:: Problems to avoid.
1926 * Wildcard Function:: How to cause wildcard expansion where
1927 it does not normally take place.
1930 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
1931 @subsection Wildcard Examples
1933 Wildcards can be used in the recipe of a rule, where they are expanded
1934 by the shell. For example, here is a rule to delete all the object files:
1942 @cindex @code{rm} (shell command)
1944 Wildcards are also useful in the prerequisites of a rule. With the
1945 following rule in the makefile, @samp{make print} will print all the
1946 @samp{.c} files that have changed since the last time you printed them:
1954 @cindex @code{print} target
1955 @cindex @code{lpr} (shell command)
1956 @cindex @code{touch} (shell command)
1958 This rule uses @file{print} as an empty target file; see @ref{Empty
1959 Targets, ,Empty Target Files to Record Events}. (The automatic variable
1960 @samp{$?} is used to print only those files that have changed; see
1961 @ref{Automatic Variables}.)@refill
1963 Wildcard expansion does not happen when you define a variable. Thus, if
1971 then the value of the variable @code{objects} is the actual string
1972 @samp{*.o}. However, if you use the value of @code{objects} in a
1973 target or prerequisite, wildcard expansion will take place there. If
1974 you use the value of @code{objects} in a recipe, the shell may perform
1975 wildcard expansion when the recipe runs. To set @code{objects} to the
1976 expansion, instead use:
1979 objects := $(wildcard *.o)
1983 @xref{Wildcard Function}.
1985 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1986 @subsection Pitfalls of Using Wildcards
1987 @cindex wildcard pitfalls
1988 @cindex pitfalls of wildcards
1989 @cindex mistakes with wildcards
1990 @cindex errors with wildcards
1991 @cindex problems with wildcards
1993 Now here is an example of a naive way of using wildcard expansion, that
1994 does not do what you would intend. Suppose you would like to say that the
1995 executable file @file{foo} is made from all the object files in the
1996 directory, and you write this:
2002 cc -o foo $(CFLAGS) $(objects)
2006 The value of @code{objects} is the actual string @samp{*.o}. Wildcard
2007 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2008 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2011 But what if you delete all the @samp{.o} files? When a wildcard matches
2012 no files, it is left as it is, so then @file{foo} will depend on the
2013 oddly-named file @file{*.o}. Since no such file is likely to exist,
2014 @code{make} will give you an error saying it cannot figure out how to
2015 make @file{*.o}. This is not what you want!
2017 Actually it is possible to obtain the desired result with wildcard
2018 expansion, but you need more sophisticated techniques, including the
2019 @code{wildcard} function and string substitution.
2021 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2024 These are described in the following section.
2027 @cindex wildcards and MS-DOS/MS-Windows backslashes
2028 @cindex backslashes in pathnames and wildcard expansion
2030 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2031 separate directories in pathnames, like so:
2037 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2038 @file{c:} part is the so-called drive letter). When @code{make} runs on
2039 these systems, it supports backslashes as well as the Unix-style forward
2040 slashes in pathnames. However, this support does @emph{not} include the
2041 wildcard expansion, where backslash is a quote character. Therefore,
2042 you @emph{must} use Unix-style slashes in these cases.
2045 @node Wildcard Function, , Wildcard Pitfall, Wildcards
2046 @subsection The Function @code{wildcard}
2049 Wildcard expansion happens automatically in rules. But wildcard expansion
2050 does not normally take place when a variable is set, or inside the
2051 arguments of a function. If you want to do wildcard expansion in such
2052 places, you need to use the @code{wildcard} function, like this:
2055 $(wildcard @var{pattern}@dots{})
2059 This string, used anywhere in a makefile, is replaced by a
2060 space-separated list of names of existing files that match one of the
2061 given file name patterns. If no existing file name matches a pattern,
2062 then that pattern is omitted from the output of the @code{wildcard}
2063 function. Note that this is different from how unmatched wildcards
2064 behave in rules, where they are used verbatim rather than ignored
2065 (@pxref{Wildcard Pitfall}).
2067 One use of the @code{wildcard} function is to get a list of all the C source
2068 files in a directory, like this:
2074 We can change the list of C source files into a list of object files by
2075 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2078 $(patsubst %.c,%.o,$(wildcard *.c))
2082 (Here we have used another function, @code{patsubst}.
2083 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2085 Thus, a makefile to compile all C source files in the directory and then
2086 link them together could be written as follows:
2089 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2092 cc -o foo $(objects)
2096 (This takes advantage of the implicit rule for compiling C programs, so
2097 there is no need to write explicit rules for compiling the files.
2098 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2099 @samp{:=}, which is a variant of @samp{=}.)
2101 @node Directory Search, Phony Targets, Wildcards, Rules
2102 @section Searching Directories for Prerequisites
2106 @cindex search path for prerequisites (@code{VPATH})
2107 @cindex directory search (@code{VPATH})
2109 For large systems, it is often desirable to put sources in a separate
2110 directory from the binaries. The @dfn{directory search} features of
2111 @code{make} facilitate this by searching several directories
2112 automatically to find a prerequisite. When you redistribute the files
2113 among directories, you do not need to change the individual rules,
2114 just the search paths.
2117 * General Search:: Specifying a search path that applies
2118 to every prerequisite.
2119 * Selective Search:: Specifying a search path
2120 for a specified class of names.
2121 * Search Algorithm:: When and how search paths are applied.
2122 * Recipes/Search:: How to write recipes that work together
2124 * Implicit/Search:: How search paths affect implicit rules.
2125 * Libraries/Search:: Directory search for link libraries.
2128 @node General Search, Selective Search, Directory Search, Directory Search
2129 @subsection @code{VPATH}: Search Path for All Prerequisites
2132 The value of the @code{make} variable @code{VPATH} specifies a list of
2133 directories that @code{make} should search. Most often, the
2134 directories are expected to contain prerequisite files that are not in the
2135 current directory; however, @code{make} uses @code{VPATH} as a search
2136 list for both prerequisites and targets of rules.
2138 Thus, if a file that is listed as a target or prerequisite does not exist
2139 in the current directory, @code{make} searches the directories listed in
2140 @code{VPATH} for a file with that name. If a file is found in one of
2141 them, that file may become the prerequisite (see below). Rules may then
2142 specify the names of files in the prerequisite list as if they all
2143 existed in the current directory. @xref{Recipes/Search, ,Writing Recipes with Directory Search}.
2145 In the @code{VPATH} variable, directory names are separated by colons or
2146 blanks. The order in which directories are listed is the order followed
2147 by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons
2148 are used as separators of directory names in @code{VPATH}, since the
2149 colon can be used in the pathname itself, after the drive letter.)
2154 VPATH = src:../headers
2158 specifies a path containing two directories, @file{src} and
2159 @file{../headers}, which @code{make} searches in that order.
2161 With this value of @code{VPATH}, the following rule,
2168 is interpreted as if it were written like this:
2175 assuming the file @file{foo.c} does not exist in the current directory but
2176 is found in the directory @file{src}.
2178 @node Selective Search, Search Algorithm, General Search, Directory Search
2179 @subsection The @code{vpath} Directive
2182 Similar to the @code{VPATH} variable, but more selective, is the
2183 @code{vpath} directive (note lower case), which allows you to specify a
2184 search path for a particular class of file names: those that match a
2185 particular pattern. Thus you can supply certain search directories for
2186 one class of file names and other directories (or none) for other file
2189 There are three forms of the @code{vpath} directive:
2192 @item vpath @var{pattern} @var{directories}
2193 Specify the search path @var{directories} for file names that match
2196 The search path, @var{directories}, is a list of directories to be
2197 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2198 blanks, just like the search path used in the @code{VPATH} variable.
2200 @item vpath @var{pattern}
2201 Clear out the search path associated with @var{pattern}.
2203 @c Extra blank line makes sure this gets two lines.
2206 Clear all search paths previously specified with @code{vpath} directives.
2209 A @code{vpath} pattern is a string containing a @samp{%} character. The
2210 string must match the file name of a prerequisite that is being searched
2211 for, the @samp{%} character matching any sequence of zero or more
2212 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2213 Redefining Pattern Rules}). For example, @code{%.h} matches files that
2214 end in @code{.h}. (If there is no @samp{%}, the pattern must match the
2215 prerequisite exactly, which is not useful very often.)
2217 @cindex @code{%}, quoting in @code{vpath}
2218 @cindex @code{%}, quoting with @code{\} (backslash)
2219 @cindex @code{\} (backslash), to quote @code{%}
2220 @cindex backslash (@code{\}), to quote @code{%}
2221 @cindex quoting @code{%}, in @code{vpath}
2222 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2223 with preceding backslashes (@samp{\}). Backslashes that would otherwise
2224 quote @samp{%} characters can be quoted with more backslashes.
2225 Backslashes that quote @samp{%} characters or other backslashes are
2226 removed from the pattern before it is compared to file names. Backslashes
2227 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2229 When a prerequisite fails to exist in the current directory, if the
2230 @var{pattern} in a @code{vpath} directive matches the name of the
2231 prerequisite file, then the @var{directories} in that directive are searched
2232 just like (and before) the directories in the @code{VPATH} variable.
2237 vpath %.h ../headers
2241 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2242 in the directory @file{../headers} if the file is not found in the current
2245 If several @code{vpath} patterns match the prerequisite file's name, then
2246 @code{make} processes each matching @code{vpath} directive one by one,
2247 searching all the directories mentioned in each directive. @code{make}
2248 handles multiple @code{vpath} directives in the order in which they
2249 appear in the makefile; multiple directives with the same pattern are
2250 independent of each other.
2264 will look for a file ending in @samp{.c} in @file{foo}, then
2265 @file{blish}, then @file{bar}, while
2275 will look for a file ending in @samp{.c} in @file{foo}, then
2276 @file{bar}, then @file{blish}.
2278 @node Search Algorithm, Recipes/Search, Selective Search, Directory Search
2279 @subsection How Directory Searches are Performed
2280 @cindex algorithm for directory search
2281 @cindex directory search algorithm
2283 When a prerequisite is found through directory search, regardless of type
2284 (general or selective), the pathname located may not be the one that
2285 @code{make} actually provides you in the prerequisite list. Sometimes
2286 the path discovered through directory search is thrown away.
2288 The algorithm @code{make} uses to decide whether to keep or abandon a
2289 path found via directory search is as follows:
2293 If a target file does not exist at the path specified in the makefile,
2294 directory search is performed.
2297 If the directory search is successful, that path is kept and this file
2298 is tentatively stored as the target.
2301 All prerequisites of this target are examined using this same method.
2304 After processing the prerequisites, the target may or may not need to be
2309 If the target does @emph{not} need to be rebuilt, the path to the file
2310 found during directory search is used for any prerequisite lists which
2311 contain this target. In short, if @code{make} doesn't need to rebuild
2312 the target then you use the path found via directory search.
2315 If the target @emph{does} need to be rebuilt (is out-of-date), the
2316 pathname found during directory search is @emph{thrown away}, and the
2317 target is rebuilt using the file name specified in the makefile. In
2318 short, if @code{make} must rebuild, then the target is rebuilt locally,
2319 not in the directory found via directory search.
2323 This algorithm may seem complex, but in practice it is quite often
2324 exactly what you want.
2326 @cindex traditional directory search (GPATH)
2327 @cindex directory search, traditional (GPATH)
2328 Other versions of @code{make} use a simpler algorithm: if the file does
2329 not exist, and it is found via directory search, then that pathname is
2330 always used whether or not the target needs to be built. Thus, if the
2331 target is rebuilt it is created at the pathname discovered during
2335 If, in fact, this is the behavior you want for some or all of your
2336 directories, you can use the @code{GPATH} variable to indicate this to
2339 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2340 space- or colon-delimited list of pathnames). If an out-of-date target
2341 is found by directory search in a directory that also appears in
2342 @code{GPATH}, then that pathname is not thrown away. The target is
2343 rebuilt using the expanded path.
2345 @node Recipes/Search, Implicit/Search, Search Algorithm, Directory Search
2346 @subsection Writing Recipes with Directory Search
2347 @cindex recipes, and directory search
2348 @cindex directory search (@code{VPATH}), and recipes
2350 When a prerequisite is found in another directory through directory search,
2351 this cannot change the recipe of the rule; they will execute as written.
2352 Therefore, you must write the recipe with care so that it will look for
2353 the prerequisite in the directory where @code{make} finds it.
2355 This is done with the @dfn{automatic variables} such as @samp{$^}
2356 (@pxref{Automatic Variables}).
2357 For instance, the value of @samp{$^} is a
2358 list of all the prerequisites of the rule, including the names of
2359 the directories in which they were found, and the value of
2360 @samp{$@@} is the target. Thus:@refill
2364 cc -c $(CFLAGS) $^ -o $@@
2368 (The variable @code{CFLAGS} exists so you can specify flags for C
2369 compilation by implicit rules; we use it here for consistency so it will
2370 affect all C compilations uniformly;
2371 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2373 Often the prerequisites include header files as well, which you do not
2374 want to mention in the recipe. The automatic variable @samp{$<} is
2375 just the first prerequisite:
2378 VPATH = src:../headers
2379 foo.o : foo.c defs.h hack.h
2380 cc -c $(CFLAGS) $< -o $@@
2383 @node Implicit/Search, Libraries/Search, Recipes/Search, Directory Search
2384 @subsection Directory Search and Implicit Rules
2385 @cindex @code{VPATH}, and implicit rules
2386 @cindex directory search (@code{VPATH}), and implicit rules
2387 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2388 @cindex implicit rule, and directory search
2389 @cindex implicit rule, and @code{VPATH}
2390 @cindex rule, implicit, and directory search
2391 @cindex rule, implicit, and @code{VPATH}
2393 The search through the directories specified in @code{VPATH} or with
2394 @code{vpath} also happens during consideration of implicit rules
2395 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2397 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2398 considers implicit rules, such as the built-in rule to compile
2399 @file{foo.c} if that file exists. If such a file is lacking in the
2400 current directory, the appropriate directories are searched for it. If
2401 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2402 directories, the implicit rule for C compilation is applied.
2404 The recipes of implicit rules normally use automatic variables as a
2405 matter of necessity; consequently they will use the file names found by
2406 directory search with no extra effort.
2408 @node Libraries/Search, , Implicit/Search, Directory Search
2409 @subsection Directory Search for Link Libraries
2410 @cindex link libraries, and directory search
2411 @cindex libraries for linking, directory search
2412 @cindex directory search (@code{VPATH}), and link libraries
2413 @cindex @code{VPATH}, and link libraries
2414 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2415 @cindex @code{-l} (library search)
2416 @cindex link libraries, patterns matching
2417 @cindex @code{.LIBPATTERNS}, and link libraries
2418 @vindex .LIBPATTERNS
2420 Directory search applies in a special way to libraries used with the
2421 linker. This special feature comes into play when you write a prerequisite
2422 whose name is of the form @samp{-l@var{name}}. (You can tell something
2423 strange is going on here because the prerequisite is normally the name of a
2424 file, and the @emph{file name} of a library generally looks like
2425 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2427 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2428 handles it specially by searching for the file @file{lib@var{name}.so},
2429 and, if it is not found, for the file @file{lib@var{name}.a} in the current
2430 directory, in directories specified by matching @code{vpath}
2431 search paths and the @code{VPATH} search path, and then in the
2432 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2433 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2434 @code{make} behave as if @var{prefix} is defined to be the root of the
2435 DJGPP installation tree).
2437 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2438 system (and no @file{/usr/lib/libcurses.so} file), then
2442 foo : foo.c -lcurses
2448 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2449 be executed when @file{foo} is older than @file{foo.c} or than
2450 @file{/usr/lib/libcurses.a}.@refill
2452 Although the default set of files to be searched for is
2453 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2454 via the @code{.LIBPATTERNS} variable. Each word in the value of this
2455 variable is a pattern string. When a prerequisite like
2456 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2457 each pattern in the list with @var{name} and perform the above directory
2458 searches using each library filename.
2460 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2461 which provides the default behavior described above.
2463 You can turn off link library expansion completely by setting this
2464 variable to an empty value.
2466 @node Phony Targets, Force Targets, Directory Search, Rules
2467 @section Phony Targets
2468 @cindex phony targets
2469 @cindex targets, phony
2470 @cindex targets without a file
2472 A phony target is one that is not really the name of a file; rather it
2473 is just a name for a recipe to be executed when you make an explicit
2474 request. There are two reasons to use a phony target: to avoid a
2475 conflict with a file of the same name, and to improve performance.
2477 If you write a rule whose recipe will not create the target file, the
2478 recipe will be executed every time the target comes up for remaking.
2489 Because the @code{rm} command does not create a file named @file{clean},
2490 probably no such file will ever exist. Therefore, the @code{rm} command
2491 will be executed every time you say @samp{make clean}.
2492 @cindex @code{rm} (shell command)
2495 The phony target will cease to work if anything ever does create a file
2496 named @file{clean} in this directory. Since it has no prerequisites, the
2497 file @file{clean} would inevitably be considered up to date, and its
2498 recipe would not be executed. To avoid this problem, you can explicitly
2499 declare the target to be phony, using the special target @code{.PHONY}
2500 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2507 Once this is done, @samp{make clean} will run the recipe regardless of
2508 whether there is a file named @file{clean}.
2510 Since it knows that phony targets do not name actual files that could be
2511 remade from other files, @code{make} skips the implicit rule search for
2512 phony targets (@pxref{Implicit Rules}). This is why declaring a target
2513 phony is good for performance, even if you are not worried about the
2514 actual file existing.
2516 Thus, you first write the line that states that @code{clean} is a
2517 phony target, then you write the rule, like this:
2527 Another example of the usefulness of phony targets is in conjunction
2528 with recursive invocations of @code{make} (for more information, see
2529 @ref{Recursion, ,Recursive Use of @code{make}}). In this case the
2530 makefile will often contain a variable which lists a number of
2531 subdirectories to be built. One way to handle this is with one rule
2532 whose recipe is a shell loop over the subdirectories, like this:
2536 SUBDIRS = foo bar baz
2539 for dir in $(SUBDIRS); do \
2545 There are problems with this method, however. First, any error
2546 detected in a submake is ignored by this rule, so it will continue
2547 to build the rest of the directories even when one fails. This can be
2548 overcome by adding shell commands to note the error and exit, but then
2549 it will do so even if @code{make} is invoked with the @code{-k}
2550 option, which is unfortunate. Second, and perhaps more importantly,
2551 you cannot take advantage of @code{make}'s ability to build targets in
2552 parallel (@pxref{Parallel, ,Parallel Execution}), since there is only
2555 By declaring the subdirectories as phony targets (you must do this as
2556 the subdirectory obviously always exists; otherwise it won't be built)
2557 you can remove these problems:
2561 SUBDIRS = foo bar baz
2563 .PHONY: subdirs $(SUBDIRS)
2574 Here we've also declared that the @file{foo} subdirectory cannot be
2575 built until after the @file{baz} subdirectory is complete; this kind of
2576 relationship declaration is particularly important when attempting
2579 A phony target should not be a prerequisite of a real target file; if it
2580 is, its recipe will be run every time @code{make} goes to update that
2581 file. As long as a phony target is never a prerequisite of a real
2582 target, the phony target recipe will be executed only when the phony
2583 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2586 Phony targets can have prerequisites. When one directory contains multiple
2587 programs, it is most convenient to describe all of the programs in one
2588 makefile @file{./Makefile}. Since the target remade by default will be the
2589 first one in the makefile, it is common to make this a phony target named
2590 @samp{all} and give it, as prerequisites, all the individual programs. For
2594 all : prog1 prog2 prog3
2597 prog1 : prog1.o utils.o
2598 cc -o prog1 prog1.o utils.o
2603 prog3 : prog3.o sort.o utils.o
2604 cc -o prog3 prog3.o sort.o utils.o
2608 Now you can say just @samp{make} to remake all three programs, or
2609 specify as arguments the ones to remake (as in @samp{make prog1
2610 prog3}). Phoniness is not inherited: the prerequisites of a phony
2611 target are not themselves phony, unless explicitly declared to be so.
2613 When one phony target is a prerequisite of another, it serves as a subroutine
2614 of the other. For example, here @samp{make cleanall} will delete the
2615 object files, the difference files, and the file @file{program}:
2618 .PHONY: cleanall cleanobj cleandiff
2620 cleanall : cleanobj cleandiff
2630 @node Force Targets, Empty Targets, Phony Targets, Rules
2631 @section Rules without Recipes or Prerequisites
2632 @cindex force targets
2633 @cindex targets, force
2634 @cindex @code{FORCE}
2635 @cindex rule, no recipe or prerequisites
2637 If a rule has no prerequisites or recipe, and the target of the rule
2638 is a nonexistent file, then @code{make} imagines this target to have
2639 been updated whenever its rule is run. This implies that all targets
2640 depending on this one will always have their recipe run.
2642 An example will illustrate this:
2652 Here the target @samp{FORCE} satisfies the special conditions, so the
2653 target @file{clean} that depends on it is forced to run its recipe.
2654 There is nothing special about the name @samp{FORCE}, but that is one
2655 name commonly used this way.
2657 As you can see, using @samp{FORCE} this way has the same results as using
2658 @samp{.PHONY: clean}.
2660 Using @samp{.PHONY} is more explicit and more efficient. However,
2661 other versions of @code{make} do not support @samp{.PHONY}; thus
2662 @samp{FORCE} appears in many makefiles. @xref{Phony Targets}.
2664 @node Empty Targets, Special Targets, Force Targets, Rules
2665 @section Empty Target Files to Record Events
2666 @cindex empty targets
2667 @cindex targets, empty
2668 @cindex recording events with empty targets
2670 The @dfn{empty target} is a variant of the phony target; it is used to hold
2671 recipes for an action that you request explicitly from time to time.
2672 Unlike a phony target, this target file can really exist; but the file's
2673 contents do not matter, and usually are empty.
2675 The purpose of the empty target file is to record, with its
2676 last-modification time, when the rule's recipe was last executed. It
2677 does so because one of the commands in the recipe is a @code{touch}
2678 command to update the target file.
2680 The empty target file should have some prerequisites (otherwise it
2681 doesn't make sense). When you ask to remake the empty target, the
2682 recipe is executed if any prerequisite is more recent than the target;
2683 in other words, if a prerequisite has changed since the last time you
2684 remade the target. Here is an example:
2691 @cindex @code{print} target
2692 @cindex @code{lpr} (shell command)
2693 @cindex @code{touch} (shell command)
2696 With this rule, @samp{make print} will execute the @code{lpr} command if
2697 either source file has changed since the last @samp{make print}. The
2698 automatic variable @samp{$?} is used to print only those files that have
2699 changed (@pxref{Automatic Variables}).
2701 @node Special Targets, Multiple Targets, Empty Targets, Rules
2702 @section Special Built-in Target Names
2703 @cindex special targets
2704 @cindex built-in special targets
2705 @cindex targets, built-in special
2707 Certain names have special meanings if they appear as targets.
2713 The prerequisites of the special target @code{.PHONY} are considered to
2714 be phony targets. When it is time to consider such a target,
2715 @code{make} will run its recipe unconditionally, regardless of
2716 whether a file with that name exists or what its last-modification
2717 time is. @xref{Phony Targets, ,Phony Targets}.
2722 The prerequisites of the special target @code{.SUFFIXES} are the list
2723 of suffixes to be used in checking for suffix rules.
2724 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2729 The recipe specified for @code{.DEFAULT} is used for any target for
2730 which no rules are found (either explicit rules or implicit rules).
2731 @xref{Last Resort}. If a @code{.DEFAULT} recipe is specified, every
2732 file mentioned as a prerequisite, but not as a target in a rule, will have
2733 that recipe executed on its behalf. @xref{Implicit Rule Search,
2734 ,Implicit Rule Search Algorithm}.
2738 @cindex precious targets
2739 @cindex preserving with @code{.PRECIOUS}
2741 The targets which @code{.PRECIOUS} depends on are given the following
2742 special treatment: if @code{make} is killed or interrupted during the
2743 execution of their recipes, the target is not deleted.
2744 @xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the
2745 target is an intermediate file, it will not be deleted after it is no
2746 longer needed, as is normally done. @xref{Chained Rules, ,Chains of
2747 Implicit Rules}. In this latter respect it overlaps with the
2748 @code{.SECONDARY} special target.
2750 You can also list the target pattern of an implicit rule (such as
2751 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2752 to preserve intermediate files created by rules whose target patterns
2753 match that file's name.
2755 @findex .INTERMEDIATE
2757 @cindex intermediate targets, explicit
2759 The targets which @code{.INTERMEDIATE} depends on are treated as
2760 intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}.
2761 @code{.INTERMEDIATE} with no prerequisites has no effect.
2765 @cindex secondary targets
2766 @cindex preserving with @code{.SECONDARY}
2768 The targets which @code{.SECONDARY} depends on are treated as
2769 intermediate files, except that they are never automatically deleted.
2770 @xref{Chained Rules, ,Chains of Implicit Rules}.
2772 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2773 as secondary (i.e., no target is removed because it is considered
2776 @findex .SECONDEXPANSION
2777 @item .SECONDEXPANSION
2779 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2780 makefile, then all prerequisite lists defined @emph{after} it appears
2781 will be expanded a second time after all makefiles have been read in.
2782 @xref{Secondary Expansion, ,Secondary Expansion}.
2784 @findex .DELETE_ON_ERROR
2785 @item .DELETE_ON_ERROR
2786 @cindex removing targets on failure
2788 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2789 makefile, then @code{make} will delete the target of a rule if it has
2790 changed and its recipe exits with a nonzero exit status, just as it
2791 does when it receives a signal. @xref{Errors, ,Errors in Recipes}.
2796 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2797 ignore errors in execution of the recipe for those particular files.
2798 The recipe for @code{.IGNORE} (if any) is ignored.
2800 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2801 ignore errors in execution of recipes for all files. This usage of
2802 @samp{.IGNORE} is supported only for historical compatibility. Since
2803 this affects every recipe in the makefile, it is not very useful; we
2804 recommend you use the more selective ways to ignore errors in specific
2805 recipes. @xref{Errors, ,Errors in Recipes}.
2807 @findex .LOW_RESOLUTION_TIME
2808 @item .LOW_RESOLUTION_TIME
2810 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2811 @command{make} assumes that these files are created by commands that
2812 generate low resolution time stamps. The recipe for the
2813 @code{.LOW_RESOLUTION_TIME} target are ignored.
2815 The high resolution file time stamps of many modern file systems
2816 lessen the chance of @command{make} incorrectly concluding that a file
2817 is up to date. Unfortunately, some hosts do not provide a way to set a
2818 high resolution file time stamp, so commands like @samp{cp -p} that
2819 explicitly set a file's time stamp must discard its subsecond part.
2820 If a file is created by such a command, you should list it as a
2821 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make}
2822 does not mistakenly conclude that the file is out of date. For
2827 .LOW_RESOLUTION_TIME: dst
2833 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2834 stamp, @file{dst} is typically slightly older than @file{src} even when
2835 it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes
2836 @command{make} to consider @file{dst} to be up to date if its time stamp
2837 is at the start of the same second that @file{src}'s time stamp is in.
2839 Due to a limitation of the archive format, archive member time stamps
2840 are always low resolution. You need not list archive members as
2841 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2847 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2848 not print the recipe used to remake those particular files before
2849 executing them. The recipe for @code{.SILENT} is ignored.
2851 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2852 to print any recipes before executing them. This usage of
2853 @samp{.SILENT} is supported only for historical compatibility. We
2854 recommend you use the more selective ways to silence specific recipes.
2855 @xref{Echoing, ,Recipe Echoing}. If you want to silence all recipes
2856 for a particular run of @code{make}, use the @samp{-s} or
2857 @w{@samp{--silent}} option (@pxref{Options Summary}).
2859 @findex .EXPORT_ALL_VARIABLES
2860 @item .EXPORT_ALL_VARIABLES
2862 Simply by being mentioned as a target, this tells @code{make} to
2863 export all variables to child processes by default.
2864 @xref{Variables/Recursion, ,Communicating Variables to a
2867 @findex .NOTPARALLEL
2869 @cindex parallel execution, overriding
2871 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation
2872 of @code{make} will be run serially, even if the @samp{-j} option is
2873 given. Any recursively invoked @code{make} command will still run
2874 recipes in parallel (unless its makefile also contains this target).
2875 Any prerequisites on this target are ignored.
2879 @cindex POSIX-conforming mode, setting
2881 If @code{.POSIX} is mentioned as a target, then the makefile will be
2882 parsed and run in POSIX-conforming mode. This does @emph{not} mean
2883 that only POSIX-conforming makefiles will be accepted: all advanced
2884 GNU @code{make} features are still available. Rather, this target
2885 causes @code{make} to behave as required by POSIX in those areas
2886 where @code{make}'s default behavior differs.
2888 In particular, if this target is mentioned then recipes will be
2889 invoked as if the shell had been passed the @code{-e} flag: the first
2890 failing command in a recipe will cause the recipe to fail immediately.
2893 Any defined implicit rule suffix also counts as a special target if it
2894 appears as a target, and so does the concatenation of two suffixes, such
2895 as @samp{.c.o}. These targets are suffix rules, an obsolete way of
2896 defining implicit rules (but a way still widely used). In principle, any
2897 target name could be special in this way if you break it in two and add
2898 both pieces to the suffix list. In practice, suffixes normally begin with
2899 @samp{.}, so these special target names also begin with @samp{.}.
2900 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2902 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2903 @section Multiple Targets in a Rule
2904 @cindex multiple targets
2905 @cindex several targets in a rule
2906 @cindex targets, multiple
2907 @cindex rule, with multiple targets
2909 A rule with multiple targets is equivalent to writing many rules, each with
2910 one target, and all identical aside from that. The same recipe applies to
2911 all the targets, but its effect may vary because you can substitute the
2912 actual target name into the recipe using @samp{$@@}. The rule contributes
2913 the same prerequisites to all the targets also.
2915 This is useful in two cases.
2919 You want just prerequisites, no recipe. For example:
2922 kbd.o command.o files.o: command.h
2926 gives an additional prerequisite to each of the three object files
2930 Similar recipes work for all the targets. The recipes do not need
2931 to be absolutely identical, since the automatic variable @samp{$@@}
2932 can be used to substitute the particular target to be remade into the
2933 commands (@pxref{Automatic Variables}). For example:
2937 bigoutput littleoutput : text.g
2938 generate text.g -$(subst output,,$@@) > $@@
2948 generate text.g -big > bigoutput
2949 littleoutput : text.g
2950 generate text.g -little > littleoutput
2954 Here we assume the hypothetical program @code{generate} makes two
2955 types of output, one if given @samp{-big} and one if given
2957 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2958 for an explanation of the @code{subst} function.
2961 Suppose you would like to vary the prerequisites according to the
2962 target, much as the variable @samp{$@@} allows you to vary the recipe.
2963 You cannot do this with multiple targets in an ordinary rule, but you
2964 can do it with a @dfn{static pattern rule}. @xref{Static Pattern,
2965 ,Static Pattern Rules}.
2967 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2968 @section Multiple Rules for One Target
2969 @cindex multiple rules for one target
2970 @cindex several rules for one target
2971 @cindex rule, multiple for one target
2972 @cindex target, multiple rules for one
2974 One file can be the target of several rules. All the prerequisites
2975 mentioned in all the rules are merged into one list of prerequisites for
2976 the target. If the target is older than any prerequisite from any rule,
2977 the recipe is executed.
2979 There can only be one recipe to be executed for a file. If more than
2980 one rule gives a recipe for the same file, @code{make} uses the last
2981 one given and prints an error message. (As a special case, if the
2982 file's name begins with a dot, no error message is printed. This odd
2983 behavior is only for compatibility with other implementations of
2984 @code{make}@dots{} you should avoid using it). Occasionally it is
2985 useful to have the same target invoke multiple recipes which are
2986 defined in different parts of your makefile; you can use
2987 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
2989 An extra rule with just prerequisites can be used to give a few extra
2990 prerequisites to many files at once. For example, makefiles often
2991 have a variable, such as @code{objects}, containing a list of all the
2992 compiler output files in the system being made. An easy way to say
2993 that all of them must be recompiled if @file{config.h} changes is to
2994 write the following:
2997 objects = foo.o bar.o
2999 bar.o : defs.h test.h
3000 $(objects) : config.h
3003 This could be inserted or taken out without changing the rules that really
3004 specify how to make the object files, making it a convenient form to use if
3005 you wish to add the additional prerequisite intermittently.
3007 Another wrinkle is that the additional prerequisites could be
3008 specified with a variable that you set with a command line argument to
3009 @code{make} (@pxref{Overriding, ,Overriding Variables}). For example,
3014 $(objects) : $(extradeps)
3019 means that the command @samp{make extradeps=foo.h} will consider
3020 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3023 If none of the explicit rules for a target has a recipe, then @code{make}
3024 searches for an applicable implicit rule to find one
3025 @pxref{Implicit Rules, ,Using Implicit Rules}).
3027 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3028 @section Static Pattern Rules
3029 @cindex static pattern rule
3030 @cindex rule, static pattern
3031 @cindex pattern rules, static (not implicit)
3032 @cindex varying prerequisites
3033 @cindex prerequisites, varying (static pattern)
3035 @dfn{Static pattern rules} are rules which specify multiple targets and
3036 construct the prerequisite names for each target based on the target name.
3037 They are more general than ordinary rules with multiple targets because the
3038 targets do not have to have identical prerequisites. Their prerequisites must
3039 be @emph{analogous}, but not necessarily @emph{identical}.
3042 * Static Usage:: The syntax of static pattern rules.
3043 * Static versus Implicit:: When are they better than implicit rules?
3046 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3047 @subsection Syntax of Static Pattern Rules
3048 @cindex static pattern rule, syntax of
3049 @cindex pattern rules, static, syntax of
3051 Here is the syntax of a static pattern rule:
3054 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3060 The @var{targets} list specifies the targets that the rule applies to.
3061 The targets can contain wildcard characters, just like the targets of
3062 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3065 @cindex target pattern, static (not implicit)
3067 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3068 prerequisites of each target. Each target is matched against the
3069 @var{target-pattern} to extract a part of the target name, called the
3070 @dfn{stem}. This stem is substituted into each of the @var{prereq-patterns}
3071 to make the prerequisite names (one from each @var{prereq-pattern}).
3073 Each pattern normally contains the character @samp{%} just once. When the
3074 @var{target-pattern} matches a target, the @samp{%} can match any part of
3075 the target name; this part is called the @dfn{stem}. The rest of the
3076 pattern must match exactly. For example, the target @file{foo.o} matches
3077 the pattern @samp{%.o}, with @samp{foo} as the stem. The targets
3078 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3080 @cindex prerequisite pattern, static (not implicit)
3081 The prerequisite names for each target are made by substituting the stem
3082 for the @samp{%} in each prerequisite pattern. For example, if one
3083 prerequisite pattern is @file{%.c}, then substitution of the stem
3084 @samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate
3085 to write a prerequisite pattern that does not contain @samp{%}; then this
3086 prerequisite is the same for all targets.
3088 @cindex @code{%}, quoting in static pattern
3089 @cindex @code{%}, quoting with @code{\} (backslash)
3090 @cindex @code{\} (backslash), to quote @code{%}
3091 @cindex backslash (@code{\}), to quote @code{%}
3092 @cindex quoting @code{%}, in static pattern
3093 @samp{%} characters in pattern rules can be quoted with preceding
3094 backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%}
3095 characters can be quoted with more backslashes. Backslashes that quote
3096 @samp{%} characters or other backslashes are removed from the pattern
3097 before it is compared to file names or has a stem substituted into it.
3098 Backslashes that are not in danger of quoting @samp{%} characters go
3099 unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has
3100 @samp{the%weird\} preceding the operative @samp{%} character, and
3101 @samp{pattern\\} following it. The final two backslashes are left alone
3102 because they cannot affect any @samp{%} character.@refill
3104 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3105 from the corresponding @file{.c} file:
3109 objects = foo.o bar.o
3113 $(objects): %.o: %.c
3114 $(CC) -c $(CFLAGS) $< -o $@@
3119 Here @samp{$<} is the automatic variable that holds the name of the
3120 prerequisite and @samp{$@@} is the automatic variable that holds the name
3121 of the target; see @ref{Automatic Variables}.
3123 Each target specified must match the target pattern; a warning is issued
3124 for each target that does not. If you have a list of files, only some of
3125 which will match the pattern, you can use the @code{filter} function to
3126 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3129 files = foo.elc bar.o lose.o
3131 $(filter %.o,$(files)): %.o: %.c
3132 $(CC) -c $(CFLAGS) $< -o $@@
3133 $(filter %.elc,$(files)): %.elc: %.el
3134 emacs -f batch-byte-compile $<
3138 In this example the result of @samp{$(filter %.o,$(files))} is
3139 @file{bar.o lose.o}, and the first static pattern rule causes each of
3140 these object files to be updated by compiling the corresponding C source
3141 file. The result of @w{@samp{$(filter %.elc,$(files))}} is
3142 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3144 Another example shows how to use @code{$*} in static pattern rules:
3145 @vindex $*@r{, and static pattern}
3149 bigoutput littleoutput : %output : text.g
3150 generate text.g -$* > $@@
3155 When the @code{generate} command is run, @code{$*} will expand to the
3156 stem, either @samp{big} or @samp{little}.
3158 @node Static versus Implicit, , Static Usage, Static Pattern
3159 @subsection Static Pattern Rules versus Implicit Rules
3160 @cindex rule, static pattern versus implicit
3161 @cindex static pattern rule, versus implicit
3163 A static pattern rule has much in common with an implicit rule defined as a
3164 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3165 Both have a pattern for the target and patterns for constructing the
3166 names of prerequisites. The difference is in how @code{make} decides
3167 @emph{when} the rule applies.
3169 An implicit rule @emph{can} apply to any target that matches its pattern,
3170 but it @emph{does} apply only when the target has no recipe otherwise
3171 specified, and only when the prerequisites can be found. If more than one
3172 implicit rule appears applicable, only one applies; the choice depends on
3175 By contrast, a static pattern rule applies to the precise list of targets
3176 that you specify in the rule. It cannot apply to any other target and it
3177 invariably does apply to each of the targets specified. If two conflicting
3178 rules apply, and both have recipes, that's an error.
3180 The static pattern rule can be better than an implicit rule for these
3185 You may wish to override the usual implicit rule for a few
3186 files whose names cannot be categorized syntactically but
3187 can be given in an explicit list.
3190 If you cannot be sure of the precise contents of the directories
3191 you are using, you may not be sure which other irrelevant files
3192 might lead @code{make} to use the wrong implicit rule. The choice
3193 might depend on the order in which the implicit rule search is done.
3194 With static pattern rules, there is no uncertainty: each rule applies
3195 to precisely the targets specified.
3198 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3199 @section Double-Colon Rules
3200 @cindex double-colon rules
3201 @cindex rule, double-colon (@code{::})
3202 @cindex multiple rules for one target (@code{::})
3203 @cindex @code{::} rules (double-colon)
3205 @dfn{Double-colon} rules are explicit rules written with @samp{::}
3206 instead of @samp{:} after the target names. They are handled
3207 differently from ordinary rules when the same target appears in more
3208 than one rule. Pattern rules with double-colons have an entirely
3209 different meaning (@pxref{Match-Anything Rules}).
3211 When a target appears in multiple rules, all the rules must be the same
3212 type: all ordinary, or all double-colon. If they are double-colon, each
3213 of them is independent of the others. Each double-colon rule's recipe
3214 is executed if the target is older than any prerequisites of that rule.
3215 If there are no prerequisites for that rule, its recipe is always
3216 executed (even if the target already exists). This can result in
3217 executing none, any, or all of the double-colon rules.
3219 Double-colon rules with the same target are in fact completely separate
3220 from one another. Each double-colon rule is processed individually, just
3221 as rules with different targets are processed.
3223 The double-colon rules for a target are executed in the order they appear
3224 in the makefile. However, the cases where double-colon rules really make
3225 sense are those where the order of executing the recipes would not matter.
3227 Double-colon rules are somewhat obscure and not often very useful; they
3228 provide a mechanism for cases in which the method used to update a target
3229 differs depending on which prerequisite files caused the update, and such
3232 Each double-colon rule should specify a recipe; if it does not, an
3233 implicit rule will be used if one applies.
3234 @xref{Implicit Rules, ,Using Implicit Rules}.
3236 @node Automatic Prerequisites, , Double-Colon, Rules
3237 @section Generating Prerequisites Automatically
3238 @cindex prerequisites, automatic generation
3239 @cindex automatic generation of prerequisites
3240 @cindex generating prerequisites automatically
3242 In the makefile for a program, many of the rules you need to write often
3243 say only that some object file depends on some header
3244 file. For example, if @file{main.c} uses @file{defs.h} via an
3245 @code{#include}, you would write:
3252 You need this rule so that @code{make} knows that it must remake
3253 @file{main.o} whenever @file{defs.h} changes. You can see that for a
3254 large program you would have to write dozens of such rules in your
3255 makefile. And, you must always be very careful to update the makefile
3256 every time you add or remove an @code{#include}.
3257 @cindex @code{#include}
3259 @cindex @code{-M} (to compiler)
3260 To avoid this hassle, most modern C compilers can write these rules for
3261 you, by looking at the @code{#include} lines in the source files.
3262 Usually this is done with the @samp{-M} option to the compiler.
3263 For example, the command:
3270 generates the output:
3273 main.o : main.c defs.h
3277 Thus you no longer have to write all those rules yourself.
3278 The compiler will do it for you.
3280 Note that such a prerequisite constitutes mentioning @file{main.o} in a
3281 makefile, so it can never be considered an intermediate file by implicit
3282 rule search. This means that @code{make} won't ever remove the file
3283 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3285 @cindex @code{make depend}
3286 With old @code{make} programs, it was traditional practice to use this
3287 compiler feature to generate prerequisites on demand with a command like
3288 @samp{make depend}. That command would create a file @file{depend}
3289 containing all the automatically-generated prerequisites; then the
3290 makefile could use @code{include} to read them in (@pxref{Include}).
3292 In GNU @code{make}, the feature of remaking makefiles makes this
3293 practice obsolete---you need never tell @code{make} explicitly to
3294 regenerate the prerequisites, because it always regenerates any makefile
3295 that is out of date. @xref{Remaking Makefiles}.
3297 The practice we recommend for automatic prerequisite generation is to have
3298 one makefile corresponding to each source file. For each source file
3299 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3300 what files the object file @file{@var{name}.o} depends on. That way
3301 only the source files that have changed need to be rescanned to produce
3302 the new prerequisites.
3304 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3305 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3310 @@set -e; rm -f $@@; \
3311 $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3312 sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3318 @xref{Pattern Rules}, for information on defining pattern rules. The
3319 @samp{-e} flag to the shell causes it to exit immediately if the
3320 @code{$(CC)} command (or any other command) fails (exits with a
3322 @cindex @code{-e} (shell flag)
3324 @cindex @code{-MM} (to GNU compiler)
3325 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3326 of @samp{-M}. This omits prerequisites on system header files.
3327 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3328 gcc.info, Using GNU CC}, for details.
3330 @cindex @code{sed} (shell command)
3331 The purpose of the @code{sed} command is to translate (for example):
3334 main.o : main.c defs.h
3341 main.o main.d : main.c defs.h
3346 This makes each @samp{.d} file depend on all the source and header files
3347 that the corresponding @samp{.o} file depends on. @code{make} then
3348 knows it must regenerate the prerequisites whenever any of the source or
3349 header files changes.
3351 Once you've defined the rule to remake the @samp{.d} files,
3352 you then use the @code{include} directive to read them all in.
3353 @xref{Include}. For example:
3357 sources = foo.c bar.c
3359 include $(sources:.c=.d)
3364 (This example uses a substitution variable reference to translate the
3365 list of source files @samp{foo.c bar.c} into a list of prerequisite
3366 makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full
3367 information on substitution references.) Since the @samp{.d} files are
3368 makefiles like any others, @code{make} will remake them as necessary
3369 with no further work from you. @xref{Remaking Makefiles}.
3371 Note that the @samp{.d} files contain target definitions; you should
3372 be sure to place the @code{include} directive @emph{after} the first,
3373 default goal in your makefiles or run the risk of having a random
3374 object file become the default goal.
3375 @xref{How Make Works}.
3377 @node Recipes, Using Variables, Rules, Top
3378 @chapter Writing Recipes in Rules
3380 @cindex recipes, how to write
3381 @cindex writing recipes
3383 The recipe of a rule consists of one or more shell command lines to
3384 be executed, one at a time, in the order they appear. Typically, the
3385 result of executing these commands is that the target of the rule is
3388 Users use many different shell programs, but recipes in makefiles are
3389 always interpreted by @file{/bin/sh} unless the makefile specifies
3390 otherwise. @xref{Execution, ,Command Execution}.
3393 * Recipe Syntax:: Recipe syntax features and pitfalls.
3394 * Echoing:: How to control when recipes are echoed.
3395 * Execution:: How recipes are executed.
3396 * Parallel:: How recipes can be executed in parallel.
3397 * Errors:: What happens after a recipe execution error.
3398 * Interrupts:: What happens when a recipe is interrupted.
3399 * Recursion:: Invoking @code{make} from makefiles.
3400 * Canned Recipes:: Defining canned recipes.
3401 * Empty Recipes:: Defining useful, do-nothing recipes.
3404 @node Recipe Syntax, Echoing, Recipes, Recipes
3405 @section Recipe Syntax
3406 @cindex recipe syntax
3407 @cindex syntax of recipe
3409 Makefiles have the unusual property that there are really two distinct
3410 syntaxes in one file. Most of the makefile uses @code{make} syntax
3411 (@pxref{Makefiles, ,Writing Makefiles}). However, recipes are meant
3412 to be interpreted by the shell and so they are written using shell
3413 syntax. The @code{make} program does not try to understand shell
3414 syntax: it performs only a very few specific translations on the
3415 content of the recipe before handing it to the shell.
3417 Each line in the recipe must start with a tab (or the first character
3418 in the value of the @code{.CMDPREFIX} variable; @pxref{Special
3419 Variables}), except that the first recipe line may be attached to the
3420 target-and-prerequisites line with a semicolon in between. @emph{Any}
3421 line in the makefile that begins with a tab and appears in a ``rule
3422 context'' (that is, after a rule has been started until another rule
3423 or variable definition) will be considered part of a recipe for that
3424 rule. Blank lines and lines of just comments may appear among the
3425 recipe lines; they are ignored.
3427 Some consequences of these rules include:
3431 A blank line that begins with a tab is not blank: it's an empty
3432 recipe (@pxref{Empty Recipes}).
3434 @cindex comments, in recipes
3435 @cindex recipes, comments in
3436 @cindex @code{#} (comments), in recipes
3438 A comment in a recipe is not a @code{make} comment; it will be
3439 passed to the shell as-is. Whether the shell treats it as a comment
3440 or not depends on your shell.
3443 A variable definition in a ``rule context'' which is indented by a tab
3444 as the first character on the line, will be considered part of a
3445 recipe, not a @code{make} variable definition, and passed to the
3449 A conditional expression (@code{ifdef}, @code{ifeq},
3450 etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3451 context'' which is indented by a tab as the first character on the
3452 line, will be considered part of a recipe and be passed to the shell.
3457 * Splitting Lines:: Breaking long recipe lines for readability.
3458 * Variables in Recipes:: Using @code{make} variables in recipes.
3461 @node Splitting Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax
3462 @subsection Splitting Recipe Lines
3463 @cindex recipes, splitting
3464 @cindex splitting recipes
3465 @cindex recipes, backslash (@code{\}) in
3466 @cindex recipes, quoting newlines in
3467 @cindex backslash (@code{\}), in recipes
3468 @cindex @code{\} (backslash), in recipes
3469 @cindex quoting newline, in recipes
3470 @cindex newline, quoting, in recipes
3472 One of the few ways in which @code{make} does interpret recipes is
3473 checking for a backslash just before the newline. As in normal
3474 makefile syntax, a single logical recipe line can be split into
3475 multiple physical lines in the makefile by placing a backslash before
3476 each newline. A sequence of lines like this is considered a single
3477 recipe line, and one instance of the shell will be invoked to run it.
3479 However, in contrast to how they are treated in other places in a
3480 makefile, backslash-newline pairs are @emph{not} removed from the
3481 recipe. Both the backslash and the newline characters are preserved
3482 and passed to the shell. How the backslash-newline is interpreted
3483 depends on your shell. If the first character of the next line after
3484 the backslash-newline is the recipe prefix character (a tab by
3485 default; @pxref{Special Variables}), then that character (and only
3486 that character) is removed. Whitespace is never added to the recipe.
3488 For example, the recipe for the all target in this makefile:
3505 consists of four separate shell commands where the output is:
3516 As a more complex example, this makefile:
3520 all : ; @@echo 'hello \
3521 world' ; echo "hello \
3527 will invoke one shell with a command of:
3532 world' ; echo "hello \
3538 which, according to shell quoting rules, will yield the following output:
3549 Notice how the backslash/newline pair was removed inside the string
3550 quoted with double quotes (@code{"@dots{}"}), but not from the string
3551 quoted with single quotes (@code{'@dots{}'}). This is the way the
3552 default shell (@file{/bin/sh}) handles backslash/newline pairs. If
3553 you specify a different shell in your makefiles it may treat them
3556 Sometimes you want to split a long line inside of single quotes, but
3557 you don't want the backslash-newline to appear in the quoted content.
3558 This is often the case when passing scripts to languages such as Perl,
3559 where extraneous backslashes inside the script can change its meaning
3560 or even be a syntax error. One simple way of handling this is to
3561 place the quoted string, or even the entire command, into a
3562 @code{make} variable then use the variable in the recipe. In this
3563 situation the newline quoting rules for makefiles will be used, and
3564 the backslash-newline will be removed. If we rewrite our example
3565 above using this method:
3572 all : ; @@echo $(HELLO)
3577 we will get output like this:
3585 If you like, you can also use target-specific variables
3586 (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3587 a tighter correspondence between the variable and the recipe that
3590 @node Variables in Recipes, , Splitting Lines, Recipe Syntax
3591 @subsection Using Variables in Recipes
3592 @cindex variable references in recipes
3593 @cindex recipes, using variables in
3595 The other way in which @code{make} processes recipes is by expanding
3596 any variable references in them (@pxref{Reference,Basics of Variable
3597 References}). This occurs after make has finished reading all the
3598 makefiles and the target is determined to be out of date; so, the
3599 recipes for targets which are not rebuilt are never expanded.
3601 Variable and function references in recipes have identical syntax and
3602 semantics to references elsewhere in the makefile. They also have the
3603 same quoting rules: if you want a dollar sign to appear in your
3604 recipe, you must double it (@samp{$$}). For shells like the default
3605 shell, that use dollar signs to introduce variables, it's important to
3606 keep clear in your mind whether the variable you want to reference is
3607 a @code{make} variable (use a single dollar sign) or a shell variable
3608 (use two dollar signs). For example:
3612 LIST = one two three
3614 for i in $(LIST); do \
3621 results in the following command being passed to the shell:
3625 for i in one two three; do \
3632 which generates the expected result:
3642 @node Echoing, Execution, Recipe Syntax, Recipes
3643 @section Recipe Echoing
3644 @cindex echoing of recipes
3645 @cindex silent operation
3646 @cindex @code{@@} (in recipes)
3647 @cindex recipes, echoing
3648 @cindex printing of recipes
3650 Normally @code{make} prints each line of the recipe before it is
3651 executed. We call this @dfn{echoing} because it gives the appearance
3652 that you are typing the lines yourself.
3654 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3655 The @samp{@@} is discarded before the line is passed to the shell.
3656 Typically you would use this for a command whose only effect is to print
3657 something, such as an @code{echo} command to indicate progress through
3661 @@echo About to make distribution files
3665 @cindex @code{--just-print}
3666 @cindex @code{--dry-run}
3667 @cindex @code{--recon}
3668 When @code{make} is given the flag @samp{-n} or @samp{--just-print} it
3669 only echoes most recipes, without executing them. @xref{Options
3670 Summary, ,Summary of Options}. In this case even the recipe lines
3671 starting with @samp{@@} are printed. This flag is useful for finding
3672 out which recipes @code{make} thinks are necessary without actually
3676 @cindex @code{--silent}
3677 @cindex @code{--quiet}
3679 The @samp{-s} or @samp{--silent}
3680 flag to @code{make} prevents all echoing, as if all recipes
3681 started with @samp{@@}. A rule in the makefile for the special target
3682 @code{.SILENT} without prerequisites has the same effect
3683 (@pxref{Special Targets, ,Special Built-in Target Names}).
3684 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3686 @node Execution, Parallel, Echoing, Recipes
3687 @section Recipe Execution
3688 @cindex recipe, execution
3689 @cindex execution, of recipes
3690 @vindex @code{SHELL} @r{(recipe execution)}
3692 When it is time to execute recipes to update a target, they are
3693 executed by invoking a new subshell for each line of the recipe. (In
3694 practice, @code{make} may take shortcuts that do not affect the
3697 @cindex @code{cd} (shell command)
3698 @cindex shell variables, setting in recipes
3699 @cindex recipes setting shell variables
3700 @strong{Please note:} this implies that setting shell variables and
3701 invoking shell commands such as @code{cd} that set a context local to
3702 each process will not affect the following lines in the recipe.@footnote{On
3703 MS-DOS, the value of current working directory is @strong{global}, so
3704 changing it @emph{will} affect the following recipe lines on those
3705 systems.} If you want to use @code{cd} to affect the next statement,
3706 put both statements in a single recipe line. Then @code{make} will
3707 invoke one shell to run the entire line, and the shell will execute
3708 the statements in sequence. For example:
3712 cd $(@@D) && gobble $(@@F) > ../$@@
3716 Here we use the shell AND operator (@code{&&}) so that if the
3717 @code{cd} command fails, the script will fail without trying to invoke
3718 the @code{gobble} command in the wrong directory, which could cause
3719 problems (in this case it would certainly cause @file{../foo} to be
3720 truncated, at least).
3723 * Choosing the Shell:: How @code{make} chooses the shell used
3727 @node Choosing the Shell, , Execution, Execution
3728 @subsection Choosing the Shell
3729 @cindex shell, choosing the
3730 @cindex @code{SHELL}, value of
3731 @cindex @code{.SHELLFLAGS}, value of
3735 The program used as the shell is taken from the variable @code{SHELL}.
3736 If this variable is not set in your makefile, the program
3737 @file{/bin/sh} is used as the shell. The argument(s) passed to the
3738 shell are taken from the variable @code{.SHELLFLAGS}. The default
3739 value of @code{.SHELLFLAGS} is @code{-c} normally, or @code{-ec} in
3740 POSIX-conforming mode.
3742 @cindex environment, @code{SHELL} in
3743 Unlike most variables, the variable @code{SHELL} is never set from the
3744 environment. This is because the @code{SHELL} environment variable is
3745 used to specify your personal choice of shell program for interactive
3746 use. It would be very bad for personal choices like this to affect the
3747 functioning of makefiles. @xref{Environment, ,Variables from the
3750 Furthermore, when you do set @code{SHELL} in your makefile that value
3751 is @emph{not} exported in the environment to recipe lines that
3752 @code{make} invokes. Instead, the value inherited from the user's
3753 environment, if any, is exported. You can override this behavior by
3754 explicitly exporting @code{SHELL} (@pxref{Variables/Recursion,
3755 ,Communicating Variables to a Sub-@code{make}}), forcing it to be
3756 passed in the environment to recipe lines.
3758 @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3759 However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3760 environment @strong{is} used, since on those systems most users do not
3761 set this variable, and therefore it is most likely set specifically to
3762 be used by @code{make}. On MS-DOS, if the setting of @code{SHELL} is
3763 not suitable for @code{make}, you can set the variable
3764 @code{MAKESHELL} to the shell that @code{make} should use; if set it
3765 will be used as the shell instead of the value of @code{SHELL}.
3767 @subsubheading Choosing a Shell in DOS and Windows
3768 @cindex shell, in DOS and Windows
3769 @cindex DOS, choosing a shell in
3770 @cindex Windows, choosing a shell in
3772 Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3776 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3777 @code{COMSPEC} (which is always set) is used instead.
3779 @cindex @code{SHELL}, MS-DOS specifics
3780 The processing of lines that set the variable @code{SHELL} in Makefiles
3781 is different on MS-DOS. The stock shell, @file{command.com}, is
3782 ridiculously limited in its functionality and many users of @code{make}
3783 tend to install a replacement shell. Therefore, on MS-DOS, @code{make}
3784 examines the value of @code{SHELL}, and changes its behavior based on
3785 whether it points to a Unix-style or DOS-style shell. This allows
3786 reasonable functionality even if @code{SHELL} points to
3789 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3790 additionally checks whether that shell can indeed be found; if not, it
3791 ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make}
3792 searches for the shell in the following places:
3796 In the precise place pointed to by the value of @code{SHELL}. For
3797 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3798 will look in the directory @file{/bin} on the current drive.
3801 In the current directory.
3804 In each of the directories in the @code{PATH} variable, in order.
3808 In every directory it examines, @code{make} will first look for the
3809 specific file (@file{sh} in the example above). If this is not found,
3810 it will also look in that directory for that file with one of the known
3811 extensions which identify executable files. For example @file{.exe},
3812 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3814 If any of these attempts is successful, the value of @code{SHELL} will
3815 be set to the full pathname of the shell as found. However, if none of
3816 these is found, the value of @code{SHELL} will not be changed, and thus
3817 the line that sets it will be effectively ignored. This is so
3818 @code{make} will only support features specific to a Unix-style shell if
3819 such a shell is actually installed on the system where @code{make} runs.
3821 Note that this extended search for the shell is limited to the cases
3822 where @code{SHELL} is set from the Makefile; if it is set in the
3823 environment or command line, you are expected to set it to the full
3824 pathname of the shell, exactly as things are on Unix.
3826 The effect of the above DOS-specific processing is that a Makefile that
3827 contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3828 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
3829 directory along your @code{PATH}.
3831 @node Parallel, Errors, Execution, Recipes
3832 @section Parallel Execution
3833 @cindex recipes, execution in parallel
3834 @cindex parallel execution
3835 @cindex execution, in parallel
3838 @cindex @code{--jobs}
3840 GNU @code{make} knows how to execute several recipes at once.
3841 Normally, @code{make} will execute only one recipe at a time, waiting
3842 for it to finish before executing the next. However, the @samp{-j} or
3843 @samp{--jobs} option tells @code{make} to execute many recipes
3844 simultaneously. You can inhibit parallelism in a particular makefile
3845 with the @code{.NOTPARALLEL} pseudo-target (@pxref{Special
3846 Targets,Special Built-in Target Names}).@refill
3848 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3849 support multi-processing.
3851 If the @samp{-j} option is followed by an integer, this is the number of
3852 recipes to execute at once; this is called the number of @dfn{job slots}.
3853 If there is nothing looking like an integer after the @samp{-j} option,
3854 there is no limit on the number of job slots. The default number of job
3855 slots is one, which means serial execution (one thing at a time).
3857 One unpleasant consequence of running several recipes simultaneously is
3858 that output generated by the recipes appears whenever each recipe
3859 sends it, so messages from different recipes may be interspersed.
3861 Another problem is that two processes cannot both take input from the
3862 same device; so to make sure that only one recipe tries to take input
3863 from the terminal at once, @code{make} will invalidate the standard
3864 input streams of all but one running recipe. This means that
3865 attempting to read from standard input will usually be a fatal error (a
3866 @samp{Broken pipe} signal) for most child processes if there are
3869 @cindex standard input
3871 It is unpredictable which recipe will have a valid standard input stream
3872 (which will come from the terminal, or wherever you redirect the standard
3873 input of @code{make}). The first recipe run will always get it first, and
3874 the first recipe started after that one finishes will get it next, and so
3877 We will change how this aspect of @code{make} works if we find a better
3878 alternative. In the mean time, you should not rely on any recipe using
3879 standard input at all if you are using the parallel execution feature; but
3880 if you are not using this feature, then standard input works normally in
3883 Finally, handling recursive @code{make} invocations raises issues. For
3884 more information on this, see
3885 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3887 If a recipe fails (is killed by a signal or exits with a nonzero
3888 status), and errors are not ignored for that recipe
3889 (@pxref{Errors, ,Errors in Recipes}),
3890 the remaining recipe lines to remake the same target will not be run.
3891 If a recipe fails and the @samp{-k} or @samp{--keep-going}
3892 option was not given
3893 (@pxref{Options Summary, ,Summary of Options}),
3894 @code{make} aborts execution. If make
3895 terminates for any reason (including a signal) with child processes
3896 running, it waits for them to finish before actually exiting.@refill
3898 @cindex load average
3899 @cindex limiting jobs based on load
3900 @cindex jobs, limiting based on load
3901 @cindex @code{-l} (load average)
3902 @cindex @code{--max-load}
3903 @cindex @code{--load-average}
3904 When the system is heavily loaded, you will probably want to run fewer jobs
3905 than when it is lightly loaded. You can use the @samp{-l} option to tell
3906 @code{make} to limit the number of jobs to run at once, based on the load
3907 average. The @samp{-l} or @samp{--max-load}
3908 option is followed by a floating-point number. For
3916 will not let @code{make} start more than one job if the load average is
3917 above 2.5. The @samp{-l} option with no following number removes the
3918 load limit, if one was given with a previous @samp{-l} option.@refill
3920 More precisely, when @code{make} goes to start up a job, and it already has
3921 at least one job running, it checks the current load average; if it is not
3922 lower than the limit given with @samp{-l}, @code{make} waits until the load
3923 average goes below that limit, or until all the other jobs finish.
3925 By default, there is no load limit.
3927 @node Errors, Interrupts, Parallel, Recipes
3928 @section Errors in Recipes
3929 @cindex errors (in recipes)
3930 @cindex recipes, errors in
3931 @cindex exit status (errors)
3933 After each shell invocation returns, @code{make} looks at its exit
3934 status. If the shell completed successfully (the exit status is
3935 zero), the next line in the recipe is executed in a new shell; after
3936 the last line is finished, the rule is finished.
3938 If there is an error (the exit status is nonzero), @code{make} gives up on
3939 the current rule, and perhaps on all rules.
3941 Sometimes the failure of a certain recipe line does not indicate a problem.
3942 For example, you may use the @code{mkdir} command to ensure that a
3943 directory exists. If the directory already exists, @code{mkdir} will
3944 report an error, but you probably want @code{make} to continue regardless.
3946 @cindex @code{-} (in recipes)
3947 To ignore errors in a recipe line, write a @samp{-} at the beginning
3948 of the line's text (after the initial tab). The @samp{-} is discarded
3949 before the line is passed to the shell for execution.
3959 @cindex @code{rm} (shell command)
3962 This causes @code{make} to continue even if @code{rm} is unable to
3966 @cindex @code{--ignore-errors}
3968 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3969 flag, errors are ignored in all recipes of all rules. A rule in the
3970 makefile for the special target @code{.IGNORE} has the same effect, if
3971 there are no prerequisites. These ways of ignoring errors are obsolete
3972 because @samp{-} is more flexible.
3974 When errors are to be ignored, because of either a @samp{-} or the
3975 @samp{-i} flag, @code{make} treats an error return just like success,
3976 except that it prints out a message that tells you the status code
3977 the shell exited with, and says that the error has been ignored.
3979 When an error happens that @code{make} has not been told to ignore,
3980 it implies that the current target cannot be correctly remade, and neither
3981 can any other that depends on it either directly or indirectly. No further
3982 recipes will be executed for these targets, since their preconditions
3983 have not been achieved.
3987 @cindex @code{--keep-going}
3988 Normally @code{make} gives up immediately in this circumstance, returning a
3989 nonzero status. However, if the @samp{-k} or @samp{--keep-going}
3990 flag is specified, @code{make}
3991 continues to consider the other prerequisites of the pending targets,
3992 remaking them if necessary, before it gives up and returns nonzero status.
3993 For example, after an error in compiling one object file, @samp{make -k}
3994 will continue compiling other object files even though it already knows
3995 that linking them will be impossible. @xref{Options Summary, ,Summary of Options}.
3997 The usual behavior assumes that your purpose is to get the specified
3998 targets up to date; once @code{make} learns that this is impossible, it
3999 might as well report the failure immediately. The @samp{-k} option says
4000 that the real purpose is to test as many of the changes made in the
4001 program as possible, perhaps to find several independent problems so
4002 that you can correct them all before the next attempt to compile. This
4003 is why Emacs' @code{compile} command passes the @samp{-k} flag by
4005 @cindex Emacs (@code{M-x compile})
4007 @findex .DELETE_ON_ERROR
4008 @cindex deletion of target files
4009 @cindex removal of target files
4010 @cindex target, deleting on error
4011 Usually when a recipe line fails, if it has changed the target file at all,
4012 the file is corrupted and cannot be used---or at least it is not
4013 completely updated. Yet the file's time stamp says that it is now up to
4014 date, so the next time @code{make} runs, it will not try to update that
4015 file. The situation is just the same as when the shell is killed by a
4016 signal; @pxref{Interrupts}. So generally the right thing to do is to
4017 delete the target file if the recipe fails after beginning to change
4018 the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears
4019 as a target. This is almost always what you want @code{make} to do, but
4020 it is not historical practice; so for compatibility, you must explicitly
4023 @node Interrupts, Recursion, Errors, Recipes
4024 @section Interrupting or Killing @code{make}
4027 @cindex deletion of target files
4028 @cindex removal of target files
4029 @cindex target, deleting on interrupt
4030 @cindex killing (interruption)
4032 If @code{make} gets a fatal signal while a shell is executing, it may
4033 delete the target file that the recipe was supposed to update. This is
4034 done if the target file's last-modification time has changed since
4035 @code{make} first checked it.
4037 The purpose of deleting the target is to make sure that it is remade from
4038 scratch when @code{make} is next run. Why is this? Suppose you type
4039 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
4040 object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting
4041 in an incomplete file whose last-modification time is newer than the source
4042 file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal
4043 and deletes this incomplete file. If @code{make} did not do this, the next
4044 invocation of @code{make} would think that @file{foo.o} did not require
4045 updating---resulting in a strange error message from the linker when it
4046 tries to link an object file half of which is missing.
4049 You can prevent the deletion of a target file in this way by making the
4050 special target @code{.PRECIOUS} depend on it. Before remaking a target,
4051 @code{make} checks to see whether it appears on the prerequisites of
4052 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
4053 if a signal happens. Some reasons why you might do this are that the
4054 target is updated in some atomic fashion, or exists only to record a
4055 modification-time (its contents do not matter), or must exist at all
4056 times to prevent other sorts of trouble.
4058 @node Recursion, Canned Recipes, Interrupts, Recipes
4059 @section Recursive Use of @code{make}
4061 @cindex subdirectories, recursion for
4063 Recursive use of @code{make} means using @code{make} as a command in a
4064 makefile. This technique is useful when you want separate makefiles for
4065 various subsystems that compose a larger system. For example, suppose you
4066 have a subdirectory @file{subdir} which has its own makefile, and you would
4067 like the containing directory's makefile to run @code{make} on the
4068 subdirectory. You can do it by writing this:
4072 cd subdir && $(MAKE)
4076 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4083 @cindex @code{--directory}
4085 You can write recursive @code{make} commands just by copying this example,
4086 but there are many things to know about how they work and why, and about
4087 how the sub-@code{make} relates to the top-level @code{make}. You may
4088 also find it useful to declare targets that invoke recursive
4089 @code{make} commands as @samp{.PHONY} (for more discussion on when
4090 this is useful, see @ref{Phony Targets}).
4092 @vindex @code{CURDIR}
4093 For your convenience, when GNU @code{make} starts (after it has
4094 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4095 the pathname of the current working directory. This value is never
4096 touched by @code{make} again: in particular note that if you include
4097 files from other directories the value of @code{CURDIR} does not
4098 change. The value has the same precedence it would have if it were
4099 set in the makefile (by default, an environment variable @code{CURDIR}
4100 will not override this value). Note that setting this variable has no
4101 impact on the operation of @code{make} (it does not cause @code{make}
4102 to change its working directory, for example).
4105 * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
4106 * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
4107 * Options/Recursion:: How to communicate options to a sub-@code{make}.
4108 * -w Option:: How the @samp{-w} or @samp{--print-directory} option
4109 helps debug use of recursive @code{make} commands.
4112 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4113 @subsection How the @code{MAKE} Variable Works
4115 @cindex recursion, and @code{MAKE} variable
4117 Recursive @code{make} commands should always use the variable @code{MAKE},
4118 not the explicit command name @samp{make}, as shown here:
4123 cd subdir && $(MAKE)
4127 The value of this variable is the file name with which @code{make} was
4128 invoked. If this file name was @file{/bin/make}, then the recipe executed
4129 is @samp{cd subdir && /bin/make}. If you use a special version of
4130 @code{make} to run the top-level makefile, the same special version will be
4131 executed for recursive invocations.
4132 @cindex @code{cd} (shell command)
4134 @cindex +, and recipes
4135 As a special feature, using the variable @code{MAKE} in the recipe of
4136 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4137 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4138 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4139 character at the beginning of the recipe line. @xref{Instead of
4140 Execution, ,Instead of Executing the Recipes}. This special feature
4141 is only enabled if the @code{MAKE} variable appears directly in the
4142 recipe: it does not apply if the @code{MAKE} variable is referenced
4143 through expansion of another variable. In the latter case you must
4144 use the @samp{+} token to get these special effects.@refill
4146 Consider the command @samp{make -t} in the above example. (The
4147 @samp{-t} option marks targets as up to date without actually running
4148 any recipes; see @ref{Instead of Execution}.) Following the usual
4149 definition of @samp{-t}, a @samp{make -t} command in the example would
4150 create a file named @file{subsystem} and do nothing else. What you
4151 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but
4152 that would require executing the recipe, and @samp{-t} says not to
4153 execute recipes.@refill
4154 @cindex @code{-t}, and recursion
4155 @cindex recursion, and @code{-t}
4156 @cindex @code{--touch}, and recursion
4158 The special feature makes this do what you want: whenever a recipe
4159 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4160 @samp{-n} and @samp{-q} do not apply to that line. Recipe lines
4161 containing @code{MAKE} are executed normally despite the presence of a
4162 flag that causes most recipes not to be run. The usual
4163 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4164 (@pxref{Options/Recursion, ,Communicating Options to a
4165 Sub-@code{make}}), so your request to touch the files, or print the
4166 recipes, is propagated to the subsystem.@refill
4168 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4169 @subsection Communicating Variables to a Sub-@code{make}
4170 @cindex sub-@code{make}
4171 @cindex environment, and recursion
4172 @cindex exporting variables
4173 @cindex variables, environment
4174 @cindex variables, exporting
4175 @cindex recursion, and environment
4176 @cindex recursion, and variables
4178 Variable values of the top-level @code{make} can be passed to the
4179 sub-@code{make} through the environment by explicit request. These
4180 variables are defined in the sub-@code{make} as defaults, but do not
4181 override what is specified in the makefile used by the sub-@code{make}
4182 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
4183 ,Summary of Options}).@refill
4185 To pass down, or @dfn{export}, a variable, @code{make} adds the
4186 variable and its value to the environment for running each line of the
4187 recipe. The sub-@code{make}, in turn, uses the environment to
4188 initialize its table of variable values. @xref{Environment,
4189 ,Variables from the Environment}.
4191 Except by explicit request, @code{make} exports a variable only if it
4192 is either defined in the environment initially or set on the command
4193 line, and if its name consists only of letters, numbers, and underscores.
4194 Some shells cannot cope with environment variable names consisting of
4195 characters other than letters, numbers, and underscores.
4197 @cindex SHELL, exported value
4198 The value of the @code{make} variable @code{SHELL} is not exported.
4199 Instead, the value of the @code{SHELL} variable from the invoking
4200 environment is passed to the sub-@code{make}. You can force
4201 @code{make} to export its value for @code{SHELL} by using the
4202 @code{export} directive, described below. @xref{Choosing the Shell}.
4204 The special variable @code{MAKEFLAGS} is always exported (unless you
4205 unexport it). @code{MAKEFILES} is exported if you set it to anything.
4207 @code{make} automatically passes down variable values that were defined
4208 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4210 See the next section.
4213 @xref{Options/Recursion}.
4216 Variables are @emph{not} normally passed down if they were created by
4217 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4218 Implicit Rules}). The sub-@code{make} will define these for
4222 If you want to export specific variables to a sub-@code{make}, use the
4223 @code{export} directive, like this:
4226 export @var{variable} @dots{}
4231 If you want to @emph{prevent} a variable from being exported, use the
4232 @code{unexport} directive, like this:
4235 unexport @var{variable} @dots{}
4239 In both of these forms, the arguments to @code{export} and
4240 @code{unexport} are expanded, and so could be variables or functions
4241 which expand to a (list of) variable names to be (un)exported.
4243 As a convenience, you can define a variable and export it at the same
4247 export @var{variable} = value
4251 has the same result as:
4254 @var{variable} = value
4255 export @var{variable}
4262 export @var{variable} := value
4266 has the same result as:
4269 @var{variable} := value
4270 export @var{variable}
4276 export @var{variable} += value
4283 @var{variable} += value
4284 export @var{variable}
4288 @xref{Appending, ,Appending More Text to Variables}.
4290 You may notice that the @code{export} and @code{unexport} directives
4291 work in @code{make} in the same way they work in the shell, @code{sh}.
4293 If you want all variables to be exported by default, you can use
4294 @code{export} by itself:
4301 This tells @code{make} that variables which are not explicitly mentioned
4302 in an @code{export} or @code{unexport} directive should be exported.
4303 Any variable given in an @code{unexport} directive will still @emph{not}
4304 be exported. If you use @code{export} by itself to export variables by
4305 default, variables whose names contain characters other than
4306 alphanumerics and underscores will not be exported unless specifically
4307 mentioned in an @code{export} directive.@refill
4309 @findex .EXPORT_ALL_VARIABLES
4310 The behavior elicited by an @code{export} directive by itself was the
4311 default in older versions of GNU @code{make}. If your makefiles depend
4312 on this behavior and you want to be compatible with old versions of
4313 @code{make}, you can write a rule for the special target
4314 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4315 This will be ignored by old @code{make}s, while the @code{export}
4316 directive will cause a syntax error.@refill
4317 @cindex compatibility in exporting
4319 Likewise, you can use @code{unexport} by itself to tell @code{make}
4320 @emph{not} to export variables by default. Since this is the default
4321 behavior, you would only need to do this if @code{export} had been used
4322 by itself earlier (in an included makefile, perhaps). You
4323 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4324 have variables exported for some recipes and not for others. The last
4325 @code{export} or @code{unexport} directive that appears by itself
4326 determines the behavior for the entire run of @code{make}.@refill
4329 @cindex recursion, level of
4330 As a special feature, the variable @code{MAKELEVEL} is changed when it
4331 is passed down from level to level. This variable's value is a string
4332 which is the depth of the level as a decimal number. The value is
4333 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4334 @samp{2} for a sub-sub-@code{make}, and so on. The incrementation
4335 happens when @code{make} sets up the environment for a recipe.@refill
4337 The main use of @code{MAKELEVEL} is to test it in a conditional
4338 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4339 way you can write a makefile that behaves one way if run recursively and
4340 another way if run directly by you.@refill
4343 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4344 commands to use additional makefiles. The value of @code{MAKEFILES} is
4345 a whitespace-separated list of file names. This variable, if defined in
4346 the outer-level makefile, is passed down through the environment; then
4347 it serves as a list of extra makefiles for the sub-@code{make} to read
4348 before the usual or specified ones. @xref{MAKEFILES Variable, ,The
4349 Variable @code{MAKEFILES}}.@refill
4351 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4352 @subsection Communicating Options to a Sub-@code{make}
4353 @cindex options, and recursion
4354 @cindex recursion, and options
4357 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4358 sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is
4359 set up automatically by @code{make} to contain the flag letters that
4360 @code{make} received. Thus, if you do @w{@samp{make -ks}} then
4361 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4363 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4364 in its environment. In response, it takes the flags from that value and
4365 processes them as if they had been given as arguments.
4366 @xref{Options Summary, ,Summary of Options}.
4368 @cindex command line variable definitions, and recursion
4369 @cindex variables, command line, and recursion
4370 @cindex recursion, and command line variable definitions
4371 Likewise variables defined on the command line are passed to the
4372 sub-@code{make} through @code{MAKEFLAGS}. Words in the value of
4373 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4374 definitions just as if they appeared on the command line.
4375 @xref{Overriding, ,Overriding Variables}.
4377 @cindex @code{-C}, and recursion
4378 @cindex @code{-f}, and recursion
4379 @cindex @code{-o}, and recursion
4380 @cindex @code{-W}, and recursion
4381 @cindex @code{--directory}, and recursion
4382 @cindex @code{--file}, and recursion
4383 @cindex @code{--old-file}, and recursion
4384 @cindex @code{--assume-old}, and recursion
4385 @cindex @code{--assume-new}, and recursion
4386 @cindex @code{--new-file}, and recursion
4387 @cindex recursion, and @code{-C}
4388 @cindex recursion, and @code{-f}
4389 @cindex recursion, and @code{-o}
4390 @cindex recursion, and @code{-W}
4391 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4392 into @code{MAKEFLAGS}; these options are not passed down.@refill
4394 @cindex @code{-j}, and recursion
4395 @cindex @code{--jobs}, and recursion
4396 @cindex recursion, and @code{-j}
4397 @cindex job slots, and recursion
4398 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4399 If you set it to some numeric value @samp{N} and your operating system
4400 supports it (most any UNIX system will; others typically won't), the
4401 parent @code{make} and all the sub-@code{make}s will communicate to
4402 ensure that there are only @samp{N} jobs running at the same time
4403 between them all. Note that any job that is marked recursive
4404 (@pxref{Instead of Execution, ,Instead of Executing Recipes})
4405 doesn't count against the total jobs (otherwise we could get @samp{N}
4406 sub-@code{make}s running and have no slots left over for any real work!)
4408 If your operating system doesn't support the above communication, then
4409 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4410 specified. This is because if the @w{@samp{-j}} option were passed down
4411 to sub-@code{make}s, you would get many more jobs running in parallel
4412 than you asked for. If you give @samp{-j} with no numeric argument,
4413 meaning to run as many jobs as possible in parallel, this is passed
4414 down, since multiple infinities are no more than one.@refill
4416 If you do not want to pass the other flags down, you must change the
4417 value of @code{MAKEFLAGS}, like this:
4421 cd subdir && $(MAKE) MAKEFLAGS=
4424 @vindex MAKEOVERRIDES
4425 The command line variable definitions really appear in the variable
4426 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4427 variable. If you do want to pass flags down normally, but don't want to
4428 pass down the command line variable definitions, you can reset
4429 @code{MAKEOVERRIDES} to empty, like this:
4436 @cindex Arg list too long
4438 This is not usually useful to do. However, some systems have a small
4439 fixed limit on the size of the environment, and putting so much
4440 information into the value of @code{MAKEFLAGS} can exceed it. If you
4441 see the error message @samp{Arg list too long}, this may be the problem.
4444 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4445 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4446 in the makefile. You probably do not care about this.)
4449 A similar variable @code{MFLAGS} exists also, for historical
4450 compatibility. It has the same value as @code{MAKEFLAGS} except that it
4451 does not contain the command line variable definitions, and it always
4452 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4453 hyphen only when it begins with an option that has no single-letter
4454 version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was
4455 traditionally used explicitly in the recursive @code{make} command, like
4460 cd subdir && $(MAKE) $(MFLAGS)
4464 but now @code{MAKEFLAGS} makes this usage redundant. If you want your
4465 makefiles to be compatible with old @code{make} programs, use this
4466 technique; it will work fine with more modern @code{make} versions too.
4468 @cindex setting options from environment
4469 @cindex options, setting from environment
4470 @cindex setting options in makefiles
4471 @cindex options, setting in makefiles
4472 The @code{MAKEFLAGS} variable can also be useful if you want to have
4473 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4474 Options}), set each time you run @code{make}. You simply put a value for
4475 @code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in
4476 a makefile, to specify additional flags that should also be in effect for
4477 that makefile. (Note that you cannot use @code{MFLAGS} this way. That
4478 variable is set only for compatibility; @code{make} does not interpret a
4479 value you set for it in any way.)
4481 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4482 environment or from a makefile), it first prepends a hyphen if the value
4483 does not already begin with one. Then it chops the value into words
4484 separated by blanks, and parses these words as if they were options given
4485 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4486 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4487 is no error for an invalid option).
4489 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4490 to include any options that will drastically affect the actions of
4491 @code{make} and undermine the purpose of makefiles and of @code{make}
4492 itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4493 put in one of these variables, could have disastrous consequences and would
4494 certainly have at least surprising and probably annoying effects.@refill
4496 @node -w Option, , Options/Recursion, Recursion
4497 @subsection The @samp{--print-directory} Option
4498 @cindex directories, printing them
4499 @cindex printing directories
4500 @cindex recursion, and printing directories
4502 If you use several levels of recursive @code{make} invocations, the
4503 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4504 lot easier to understand by showing each directory as @code{make}
4505 starts processing it and as @code{make} finishes processing it. For
4506 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4507 @code{make} will print a line of the form:@refill
4510 make: Entering directory `/u/gnu/make'.
4514 before doing anything else, and a line of the form:
4517 make: Leaving directory `/u/gnu/make'.
4521 when processing is completed.
4523 @cindex @code{-C}, and @code{-w}
4524 @cindex @code{--directory}, and @code{--print-directory}
4525 @cindex recursion, and @code{-w}
4526 @cindex @code{-w}, and @code{-C}
4527 @cindex @code{-w}, and recursion
4528 @cindex @code{--print-directory}, and @code{--directory}
4529 @cindex @code{--print-directory}, and recursion
4530 @cindex @code{--no-print-directory}
4531 @cindex @code{--print-directory}, disabling
4532 @cindex @code{-w}, disabling
4533 Normally, you do not need to specify this option because @samp{make}
4534 does it for you: @samp{-w} is turned on automatically when you use the
4535 @samp{-C} option, and in sub-@code{make}s. @code{make} will not
4536 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4537 be silent, or if you use @samp{--no-print-directory} to explicitly
4540 @node Canned Recipes, Empty Recipes, Recursion, Recipes
4541 @section Defining Canned Recipes
4542 @cindex canned recipes
4543 @cindex recipes, canned
4544 @cindex sequences of commands
4545 @cindex commands, sequences of
4547 When the same sequence of commands is useful in making various
4548 targets, you can define it as a canned sequence with the @code{define}
4549 directive, and refer to the canned sequence from the recipes for those
4550 targets. The canned sequence is actually a variable, so the name must
4551 not conflict with other variable names.
4553 Here is an example of defining a canned recipe:
4557 yacc $(firstword $^)
4564 Here @code{run-yacc} is the name of the variable being defined;
4565 @code{endef} marks the end of the definition; the lines in between are the
4566 commands. The @code{define} directive does not expand variable references
4567 and function calls in the canned sequence; the @samp{$} characters,
4568 parentheses, variable names, and so on, all become part of the value of the
4569 variable you are defining.
4570 @xref{Multi-Line, ,Defining Multi-Line Variables},
4571 for a complete explanation of @code{define}.
4573 The first command in this example runs Yacc on the first prerequisite of
4574 whichever rule uses the canned sequence. The output file from Yacc is
4575 always named @file{y.tab.c}. The second command moves the output to the
4576 rule's target file name.
4578 To use the canned sequence, substitute the variable into the recipe of a
4579 rule. You can substitute it like any other variable
4580 (@pxref{Reference, ,Basics of Variable References}).
4581 Because variables defined by @code{define} are recursively expanded
4582 variables, all the variable references you wrote inside the @code{define}
4583 are expanded now. For example:
4591 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4592 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4594 This is a realistic example, but this particular one is not needed in
4595 practice because @code{make} has an implicit rule to figure out these
4596 commands based on the file names involved
4597 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4599 @cindex @@, and @code{define}
4600 @cindex -, and @code{define}
4601 @cindex +, and @code{define}
4602 In recipe execution, each line of a canned sequence is treated just as
4603 if the line appeared on its own in the rule, preceded by a tab. In
4604 particular, @code{make} invokes a separate subshell for each line. You
4605 can use the special prefix characters that affect command lines
4606 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4607 @xref{Recipes, ,Writing Recipes in Rules}.
4608 For example, using this canned sequence:
4612 @@echo "frobnicating target $@@"
4613 frob-step-1 $< -o $@@-step-1
4614 frob-step-2 $@@-step-1 -o $@@
4619 @code{make} will not echo the first line, the @code{echo} command.
4620 But it @emph{will} echo the following two recipe lines.
4622 On the other hand, prefix characters on the recipe line that refers to
4623 a canned sequence apply to every line in the sequence. So the rule:
4631 does not echo @emph{any} recipe lines.
4632 (@xref{Echoing, ,Recipe Echoing}, for a full explanation of @samp{@@}.)
4634 @node Empty Recipes, , Canned Recipes, Recipes
4635 @section Using Empty Recipes
4636 @cindex empty recipes
4637 @cindex recipes, empty
4639 It is sometimes useful to define recipes which do nothing. This is done
4640 simply by giving a recipe that consists of nothing but whitespace. For
4648 defines an empty recipe for @file{target}. You could also use a line
4649 beginning with a recipe prefix character to define an empty recipe,
4650 but this would be confusing because such a line looks empty.
4652 @findex .DEFAULT@r{, and empty recipes}
4653 You may be wondering why you would want to define a recipe that
4654 does nothing. The only reason this is useful is to prevent a target
4655 from getting implicit recipes (from implicit rules or the
4656 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4657 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4659 @c !!! another reason is for canonical stamp files:
4664 create foo frm foo.in
4669 You may be inclined to define empty recipes for targets that are
4670 not actual files, but only exist so that their prerequisites can be
4671 remade. However, this is not the best way to do that, because the
4672 prerequisites may not be remade properly if the target file actually does exist.
4673 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4675 @node Using Variables, Conditionals, Recipes, Top
4676 @chapter How to Use Variables
4679 @cindex recursive variable expansion
4680 @cindex simple variable expansion
4682 A @dfn{variable} is a name defined in a makefile to represent a string
4683 of text, called the variable's @dfn{value}. These values are
4684 substituted by explicit request into targets, prerequisites, recipes,
4685 and other parts of the makefile. (In some other versions of @code{make},
4686 variables are called @dfn{macros}.)
4689 Variables and functions in all parts of a makefile are expanded when
4690 read, except for in recipes, the right-hand sides of variable
4691 definitions using @samp{=}, and the bodies of variable definitions
4692 using the @code{define} directive.@refill
4694 Variables can represent lists of file names, options to pass to compilers,
4695 programs to run, directories to look in for source files, directories to
4696 write output in, or anything else you can imagine.
4698 A variable name may be any sequence of characters not containing @samp{:},
4699 @samp{#}, @samp{=}, or leading or trailing whitespace. However,
4700 variable names containing characters other than letters, numbers, and
4701 underscores should be avoided, as they may be given special meanings in the
4702 future, and with some shells they cannot be passed through the environment to a
4704 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4706 Variable names are case-sensitive. The names @samp{foo}, @samp{FOO},
4707 and @samp{Foo} all refer to different variables.
4709 It is traditional to use upper case letters in variable names, but we
4710 recommend using lower case letters for variable names that serve internal
4711 purposes in the makefile, and reserving upper case for parameters that
4712 control implicit rules or for parameters that the user should override with
4713 command options (@pxref{Overriding, ,Overriding Variables}).
4715 A few variables have names that are a single punctuation character or
4716 just a few characters. These are the @dfn{automatic variables}, and
4717 they have particular specialized uses. @xref{Automatic Variables}.
4720 * Reference:: How to use the value of a variable.
4721 * Flavors:: Variables come in two flavors.
4722 * Advanced:: Advanced features for referencing a variable.
4723 * Values:: All the ways variables get their values.
4724 * Setting:: How to set a variable in the makefile.
4725 * Appending:: How to append more text to the old value
4727 * Override Directive:: How to set a variable in the makefile even if
4728 the user has set it with a command argument.
4729 * Multi-Line:: An alternate way to set a variable
4730 to a multi-line string.
4731 * Undefine Directive:: How to undefine a variable so that it appears
4732 as if it was never set.
4733 * Environment:: Variable values can come from the environment.
4734 * Target-specific:: Variable values can be defined on a per-target
4736 * Pattern-specific:: Target-specific variable values can be applied
4737 to a group of targets that match a pattern.
4738 * Suppressing Inheritance:: Suppress inheritance of variables.
4739 * Special Variables:: Variables with special meaning or behavior.
4742 @node Reference, Flavors, Using Variables, Using Variables
4743 @section Basics of Variable References
4744 @cindex variables, how to reference
4745 @cindex reference to variables
4746 @cindex @code{$}, in variable reference
4747 @cindex dollar sign (@code{$}), in variable reference
4749 To substitute a variable's value, write a dollar sign followed by the name
4750 of the variable in parentheses or braces: either @samp{$(foo)} or
4751 @samp{$@{foo@}} is a valid reference to the variable @code{foo}. This
4752 special significance of @samp{$} is why you must write @samp{$$} to have
4753 the effect of a single dollar sign in a file name or recipe.
4755 Variable references can be used in any context: targets, prerequisites,
4756 recipes, most directives, and new variable values. Here is an
4757 example of a common case, where a variable holds the names of all the
4758 object files in a program:
4762 objects = program.o foo.o utils.o
4763 program : $(objects)
4764 cc -o program $(objects)
4770 Variable references work by strict textual substitution. Thus, the rule
4775 prog.o : prog.$(foo)
4776 $(foo)$(foo) -$(foo) prog.$(foo)
4781 could be used to compile a C program @file{prog.c}. Since spaces before
4782 the variable value are ignored in variable assignments, the value of
4783 @code{foo} is precisely @samp{c}. (Don't actually write your makefiles
4786 A dollar sign followed by a character other than a dollar sign,
4787 open-parenthesis or open-brace treats that single character as the
4788 variable name. Thus, you could reference the variable @code{x} with
4789 @samp{$x}. However, this practice is strongly discouraged, except in
4790 the case of the automatic variables (@pxref{Automatic Variables}).
4792 @node Flavors, Advanced, Reference, Using Variables
4793 @section The Two Flavors of Variables
4794 @cindex flavors of variables
4795 @cindex recursive variable expansion
4796 @cindex variables, flavors
4797 @cindex recursively expanded variables
4798 @cindex variables, recursively expanded
4800 There are two ways that a variable in GNU @code{make} can have a value;
4801 we call them the two @dfn{flavors} of variables. The two flavors are
4802 distinguished in how they are defined and in what they do when expanded.
4805 The first flavor of variable is a @dfn{recursively expanded} variable.
4806 Variables of this sort are defined by lines using @samp{=}
4807 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4808 (@pxref{Multi-Line, ,Defining Multi-Line Variables}). The value you specify
4809 is installed verbatim; if it contains references to other variables,
4810 these references are expanded whenever this variable is substituted (in
4811 the course of expanding some other string). When this happens, it is
4812 called @dfn{recursive expansion}.@refill
4825 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4826 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4828 This flavor of variable is the only sort supported by other versions of
4829 @code{make}. It has its advantages and its disadvantages. An advantage
4830 (most would say) is that:
4833 CFLAGS = $(include_dirs) -O
4834 include_dirs = -Ifoo -Ibar
4838 will do what was intended: when @samp{CFLAGS} is expanded in a recipe,
4839 it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you
4840 cannot append something on the end of a variable, as in
4843 CFLAGS = $(CFLAGS) -O
4847 because it will cause an infinite loop in the variable expansion.
4848 (Actually @code{make} detects the infinite loop and reports an error.)
4849 @cindex loops in variable expansion
4850 @cindex variables, loops in expansion
4852 Another disadvantage is that any functions
4853 (@pxref{Functions, ,Functions for Transforming Text})
4854 referenced in the definition will be executed every time the variable is
4855 expanded. This makes @code{make} run slower; worse, it causes the
4856 @code{wildcard} and @code{shell} functions to give unpredictable results
4857 because you cannot easily control when they are called, or even how many
4860 To avoid all the problems and inconveniences of recursively expanded
4861 variables, there is another flavor: simply expanded variables.
4863 @cindex simply expanded variables
4864 @cindex variables, simply expanded
4866 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4867 (@pxref{Setting, ,Setting Variables}).
4868 The value of a simply expanded variable is scanned
4869 once and for all, expanding any references to other variables and
4870 functions, when the variable is defined. The actual value of the simply
4871 expanded variable is the result of expanding the text that you write.
4872 It does not contain any references to other variables; it contains their
4873 values @emph{as of the time this variable was defined}. Therefore,
4889 When a simply expanded variable is referenced, its value is substituted
4892 Here is a somewhat more complicated example, illustrating the use of
4893 @samp{:=} in conjunction with the @code{shell} function.
4894 (@xref{Shell Function, , The @code{shell} Function}.) This example
4895 also shows use of the variable @code{MAKELEVEL}, which is changed
4896 when it is passed down from level to level.
4897 (@xref{Variables/Recursion, , Communicating Variables to a
4898 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4904 ifeq (0,$@{MAKELEVEL@})
4905 whoami := $(shell whoami)
4906 host-type := $(shell arch)
4907 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4913 An advantage of this use of @samp{:=} is that a typical
4914 `descend into a directory' recipe then looks like this:
4919 $@{MAKE@} -C $@@ all
4923 Simply expanded variables generally make complicated makefile programming
4924 more predictable because they work like variables in most programming
4925 languages. They allow you to redefine a variable using its own value (or
4926 its value processed in some way by one of the expansion functions) and to
4927 use the expansion functions much more efficiently
4928 (@pxref{Functions, ,Functions for Transforming Text}).
4930 @cindex spaces, in variable values
4931 @cindex whitespace, in variable values
4932 @cindex variables, spaces in values
4933 You can also use them to introduce controlled leading whitespace into
4934 variable values. Leading whitespace characters are discarded from your
4935 input before substitution of variable references and function calls;
4936 this means you can include leading spaces in a variable value by
4937 protecting them with variable references, like this:
4941 space := $(nullstring) # end of the line
4945 Here the value of the variable @code{space} is precisely one space. The
4946 comment @w{@samp{# end of the line}} is included here just for clarity.
4947 Since trailing space characters are @emph{not} stripped from variable
4948 values, just a space at the end of the line would have the same effect
4949 (but be rather hard to read). If you put whitespace at the end of a
4950 variable value, it is a good idea to put a comment like that at the end
4951 of the line to make your intent clear. Conversely, if you do @emph{not}
4952 want any whitespace characters at the end of your variable value, you
4953 must remember not to put a random comment on the end of the line after
4954 some whitespace, such as this:
4957 dir := /foo/bar # directory to put the frobs in
4961 Here the value of the variable @code{dir} is @w{@samp{/foo/bar }}
4962 (with four trailing spaces), which was probably not the intention.
4963 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4965 @cindex conditional variable assignment
4966 @cindex variables, conditional assignment
4968 There is another assignment operator for variables, @samp{?=}. This
4969 is called a conditional variable assignment operator, because it only
4970 has an effect if the variable is not yet defined. This statement:
4977 is exactly equivalent to this
4978 (@pxref{Origin Function, ,The @code{origin} Function}):
4981 ifeq ($(origin FOO), undefined)
4986 Note that a variable set to an empty value is still defined, so
4987 @samp{?=} will not set that variable.
4989 @node Advanced, Values, Flavors, Using Variables
4990 @section Advanced Features for Reference to Variables
4991 @cindex reference to variables
4993 This section describes some advanced features you can use to reference
4994 variables in more flexible ways.
4997 * Substitution Refs:: Referencing a variable with
4998 substitutions on the value.
4999 * Computed Names:: Computing the name of the variable to refer to.
5002 @node Substitution Refs, Computed Names, Advanced, Advanced
5003 @subsection Substitution References
5004 @cindex modified variable reference
5005 @cindex substitution variable reference
5006 @cindex variables, modified reference
5007 @cindex variables, substitution reference
5009 @cindex variables, substituting suffix in
5010 @cindex suffix, substituting in variables
5011 A @dfn{substitution reference} substitutes the value of a variable with
5012 alterations that you specify. It has the form
5013 @samp{$(@var{var}:@var{a}=@var{b})} (or
5014 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
5015 of the variable @var{var}, replace every @var{a} at the end of a word with
5016 @var{b} in that value, and substitute the resulting string.
5018 When we say ``at the end of a word'', we mean that @var{a} must appear
5019 either followed by whitespace or at the end of the value in order to be
5020 replaced; other occurrences of @var{a} in the value are unaltered. For
5029 sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}.
5031 A substitution reference is actually an abbreviation for use of the
5032 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide
5033 substitution references as well as @code{patsubst} for compatibility with
5034 other implementations of @code{make}.
5037 Another type of substitution reference lets you use the full power of
5038 the @code{patsubst} function. It has the same form
5039 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
5040 @var{a} must contain a single @samp{%} character. This case is
5041 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
5042 @xref{Text Functions, ,Functions for String Substitution and Analysis},
5043 for a description of the @code{patsubst} function.@refill
5047 @exdent For example:
5050 bar := $(foo:%.o=%.c)
5055 sets @samp{bar} to @samp{a.c b.c c.c}.
5057 @node Computed Names, , Substitution Refs, Advanced
5058 @subsection Computed Variable Names
5059 @cindex nested variable reference
5060 @cindex computed variable name
5061 @cindex variables, computed names
5062 @cindex variables, nested references
5063 @cindex variables, @samp{$} in name
5064 @cindex @code{$}, in variable name
5065 @cindex dollar sign (@code{$}), in variable name
5067 Computed variable names are a complicated concept needed only for
5068 sophisticated makefile programming. For most purposes you need not
5069 consider them, except to know that making a variable with a dollar sign
5070 in its name might have strange results. However, if you are the type
5071 that wants to understand everything, or you are actually interested in
5072 what they do, read on.
5074 Variables may be referenced inside the name of a variable. This is
5075 called a @dfn{computed variable name} or a @dfn{nested variable
5076 reference}. For example,
5085 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5086 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5087 to @samp{z}. Here the name of the variable to reference is not stated
5088 explicitly; it is computed by expansion of @samp{$(x)}. The reference
5089 @samp{$(x)} here is nested within the outer variable reference.
5091 The previous example shows two levels of nesting, but any number of levels
5092 is possible. For example, here are three levels:
5102 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5103 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5104 @samp{$(z)}, which becomes @samp{u}.
5106 References to recursively-expanded variables within a variable name are
5107 reexpanded in the usual fashion. For example:
5117 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5118 which becomes @samp{$(z)} which becomes @samp{Hello}.
5120 Nested variable references can also contain modified references and
5121 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5122 just like any other reference.
5123 For example, using the @code{subst} function
5124 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5130 y = $(subst 1,2,$(x))
5137 eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone
5138 would ever want to write a nested reference as convoluted as this one, but
5139 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5140 @samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from
5141 @code{x} and changes it by substitution to @samp{variable2}, so that the
5142 entire string becomes @samp{$(variable2)}, a simple variable reference
5143 whose value is @samp{Hello}.@refill
5145 A computed variable name need not consist entirely of a single variable
5146 reference. It can contain several variable references, as well as some
5147 invariant text. For example,
5156 a_files := filea fileb
5157 1_files := file1 file2
5161 ifeq "$(use_a)" "yes"
5169 ifeq "$(use_dirs)" "yes"
5175 dirs := $($(a1)_$(df))
5180 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5181 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5182 and @code{use_dirs}.@refill
5184 Computed variable names can also be used in substitution references:
5188 a_objects := a.o b.o c.o
5189 1_objects := 1.o 2.o 3.o
5191 sources := $($(a1)_objects:.o=.c)
5196 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5197 depending on the value of @code{a1}.
5199 The only restriction on this sort of use of nested variable references
5200 is that they cannot specify part of the name of a function to be called.
5201 This is because the test for a recognized function name is done before
5202 the expansion of nested references. For example,
5218 foo := $($(func) $(bar))
5223 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5224 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5225 as the argument to either the @code{sort} or the @code{strip} function.
5226 This restriction could be removed in the future if that change is shown
5229 You can also use computed variable names in the left-hand side of a
5230 variable assignment, or in a @code{define} directive, as in:
5234 $(dir)_sources := $(wildcard $(dir)/*.c)
5235 define $(dir)_print =
5236 lpr $($(dir)_sources)
5241 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5244 Note that @dfn{nested variable references} are quite different from
5245 @dfn{recursively expanded variables}
5246 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5247 used together in complex ways when doing makefile programming.@refill
5249 @node Values, Setting, Advanced, Using Variables
5250 @section How Variables Get Their Values
5251 @cindex variables, how they get their values
5252 @cindex value, how a variable gets it
5254 Variables can get values in several different ways:
5258 You can specify an overriding value when you run @code{make}.
5259 @xref{Overriding, ,Overriding Variables}.
5262 You can specify a value in the makefile, either
5263 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5264 verbatim definition (@pxref{Multi-Line, ,Defining Multi-Line Variables}).@refill
5267 Variables in the environment become @code{make} variables.
5268 @xref{Environment, ,Variables from the Environment}.
5271 Several @dfn{automatic} variables are given new values for each rule.
5272 Each of these has a single conventional use.
5273 @xref{Automatic Variables}.
5276 Several variables have constant initial values.
5277 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5280 @node Setting, Appending, Values, Using Variables
5281 @section Setting Variables
5282 @cindex setting variables
5283 @cindex variables, setting
5288 To set a variable from the makefile, write a line starting with the
5289 variable name followed by @samp{=} or @samp{:=}. Whatever follows the
5290 @samp{=} or @samp{:=} on the line becomes the value. For example,
5293 objects = main.o foo.o bar.o utils.o
5297 defines a variable named @code{objects}. Whitespace around the variable
5298 name and immediately after the @samp{=} is ignored.
5300 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5301 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5302 definitions can contain variable references which will be expanded before
5303 the definition is made. @xref{Flavors, ,The Two Flavors of Variables}.
5305 The variable name may contain function and variable references, which
5306 are expanded when the line is read to find the actual variable name to use.
5308 There is no limit on the length of the value of a variable except the
5309 amount of swapping space on the computer. When a variable definition is
5310 long, it is a good idea to break it into several lines by inserting
5311 backslash-newline at convenient places in the definition. This will not
5312 affect the functioning of @code{make}, but it will make the makefile easier
5315 Most variable names are considered to have the empty string as a value if
5316 you have never set them. Several variables have built-in initial values
5317 that are not empty, but you can set them in the usual ways
5318 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5319 Several special variables are set
5320 automatically to a new value for each rule; these are called the
5321 @dfn{automatic} variables (@pxref{Automatic Variables}).
5323 If you'd like a variable to be set to a value only if it's not already
5324 set, then you can use the shorthand operator @samp{?=} instead of
5325 @samp{=}. These two settings of the variable @samp{FOO} are identical
5326 (@pxref{Origin Function, ,The @code{origin} Function}):
5336 ifeq ($(origin FOO), undefined)
5341 @node Appending, Override Directive, Setting, Using Variables
5342 @section Appending More Text to Variables
5344 @cindex appending to variables
5345 @cindex variables, appending to
5347 Often it is useful to add more text to the value of a variable already defined.
5348 You do this with a line containing @samp{+=}, like this:
5351 objects += another.o
5355 This takes the value of the variable @code{objects}, and adds the text
5356 @samp{another.o} to it (preceded by a single space). Thus:
5359 objects = main.o foo.o bar.o utils.o
5360 objects += another.o
5364 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5366 Using @samp{+=} is similar to:
5369 objects = main.o foo.o bar.o utils.o
5370 objects := $(objects) another.o
5374 but differs in ways that become important when you use more complex values.
5376 When the variable in question has not been defined before, @samp{+=}
5377 acts just like normal @samp{=}: it defines a recursively-expanded
5378 variable. However, when there @emph{is} a previous definition, exactly
5379 what @samp{+=} does depends on what flavor of variable you defined
5380 originally. @xref{Flavors, ,The Two Flavors of Variables}, for an
5381 explanation of the two flavors of variables.
5383 When you add to a variable's value with @samp{+=}, @code{make} acts
5384 essentially as if you had included the extra text in the initial
5385 definition of the variable. If you defined it first with @samp{:=},
5386 making it a simply-expanded variable, @samp{+=} adds to that
5387 simply-expanded definition, and expands the new text before appending it
5388 to the old value just as @samp{:=} does
5389 (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5398 is exactly equivalent to:
5403 variable := $(variable) more
5406 On the other hand, when you use @samp{+=} with a variable that you defined
5407 first to be recursively-expanded using plain @samp{=}, @code{make} does
5408 something a bit different. Recall that when you define a
5409 recursively-expanded variable, @code{make} does not expand the value you set
5410 for variable and function references immediately. Instead it stores the text
5411 verbatim, and saves these variable and function references to be expanded
5412 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5413 of Variables}). When you use @samp{+=} on a recursively-expanded variable,
5414 it is this unexpanded text to which @code{make} appends the new text you
5425 is roughly equivalent to:
5430 variable = $(temp) more
5435 except that of course it never defines a variable called @code{temp}.
5436 The importance of this comes when the variable's old value contains
5437 variable references. Take this common example:
5440 CFLAGS = $(includes) -O
5442 CFLAGS += -pg # enable profiling
5446 The first line defines the @code{CFLAGS} variable with a reference to another
5447 variable, @code{includes}. (@code{CFLAGS} is used by the rules for C
5448 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5449 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5450 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5451 @code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes}
5452 need not be defined yet for its value to take effect. It only has to be
5453 defined before any reference to @code{CFLAGS}. If we tried to append to the
5454 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5457 CFLAGS := $(CFLAGS) -pg # enable profiling
5461 This is pretty close, but not quite what we want. Using @samp{:=}
5462 redefines @code{CFLAGS} as a simply-expanded variable; this means
5463 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5464 variable. If @code{includes} is not yet defined, we get @w{@samp{ -O
5465 -pg}}, and a later definition of @code{includes} will have no effect.
5466 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5467 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve
5468 the reference to @code{includes}, so if that variable gets defined at
5469 any later point, a reference like @samp{$(CFLAGS)} still uses its
5472 @node Override Directive, Multi-Line, Appending, Using Variables
5473 @section The @code{override} Directive
5475 @cindex overriding with @code{override}
5476 @cindex variables, overriding
5478 If a variable has been set with a command argument
5479 (@pxref{Overriding, ,Overriding Variables}),
5480 then ordinary assignments in the makefile are ignored. If you want to set
5481 the variable in the makefile even though it was set with a command
5482 argument, you can use an @code{override} directive, which is a line that
5483 looks like this:@refill
5486 override @var{variable} = @var{value}
5493 override @var{variable} := @var{value}
5496 To append more text to a variable defined on the command line, use:
5499 override @var{variable} += @var{more text}
5503 @xref{Appending, ,Appending More Text to Variables}.
5505 Variable assignments marked with the @code{override} flag have a
5506 higher priority than all other assignments, except another
5507 @code{override}. Subsequent assignments or appends to this variable
5508 which are not marked @code{override} will be ignored.
5510 The @code{override} directive was not invented for escalation in the war
5511 between makefiles and command arguments. It was invented so you can alter
5512 and add to values that the user specifies with command arguments.
5514 For example, suppose you always want the @samp{-g} switch when you run the
5515 C compiler, but you would like to allow the user to specify the other
5516 switches with a command argument just as usual. You could use this
5517 @code{override} directive:
5520 override CFLAGS += -g
5523 You can also use @code{override} directives with @code{define} directives.
5524 This is done as you might expect:
5527 override define foo =
5534 See the next section for information about @code{define}.
5537 @xref{Multi-Line, ,Defining Multi-Line Variables}.
5540 @node Multi-Line, Undefine Directive, Override Directive, Using Variables
5541 @section Defining Multi-Line Variables
5544 @cindex multi-line variable definition
5545 @cindex variables, multi-line
5546 @cindex verbatim variable definition
5547 @cindex defining variables verbatim
5548 @cindex variables, defining verbatim
5550 Another way to set the value of a variable is to use the @code{define}
5551 directive. This directive has an unusual syntax which allows newline
5552 characters to be included in the value, which is convenient for
5553 defining both canned sequences of commands (@pxref{Canned Recipes,
5554 ,Defining Canned Recipes}), and also sections of makefile syntax to
5555 use with @code{eval} (@pxref{Eval Function}).@refill
5557 The @code{define} directive is followed on the same line by the name
5558 of the variable being defined and an (optional) assignment operator,
5559 and nothing more. The value to give the variable appears on the
5560 following lines. The end of the value is marked by a line containing
5561 just the word @code{endef}. Aside from this difference in syntax,
5562 @code{define} works just like any other variable definition. The
5563 variable name may contain function and variable references, which are
5564 expanded when the directive is read to find the actual variable name
5567 You may omit the variable assignment operator if you prefer. If
5568 omitted, @code{make} assumes it to be @samp{=} and creates a
5569 recursively-expanded variable (@pxref{Flavors, ,The Two Flavors of Variables}).
5570 When using a @samp{+=} operator, the value is appended to the previous
5571 value as with any other append operation: with a single space
5572 separating the old and new values.
5574 You may nest @code{define} directives: @code{make} will keep track of
5575 nested directives and report an error if they are not all properly
5576 closed with @code{endef}. Note that lines beginning with the recipe
5577 prefix character are considered part of a recipe, so any @code{define}
5578 or @code{endef} strings appearing on such a line will not be
5579 considered @code{make} directives.
5588 The value in an ordinary assignment cannot contain a newline; but the
5589 newlines that separate the lines of the value in a @code{define} become
5590 part of the variable's value (except for the final newline which precedes
5591 the @code{endef} and is not considered part of the value).@refill
5594 When used in a recipe, the previous example is functionally equivalent
5598 two-lines = echo foo; echo $(bar)
5602 since two commands separated by semicolon behave much like two separate
5603 shell commands. However, note that using two separate lines means
5604 @code{make} will invoke the shell twice, running an independent subshell
5605 for each line. @xref{Execution, ,Recipe Execution}.
5607 If you want variable definitions made with @code{define} to take
5608 precedence over command-line variable definitions, you can use the
5609 @code{override} directive together with @code{define}:
5612 override define two-lines =
5619 @xref{Override Directive, ,The @code{override} Directive}.
5621 @node Undefine Directive, Environment, Multi-Line, Using Variables
5622 @section Undefining Variables
5624 @cindex undefining variable
5626 If you want to clear a variable, setting its value to empty is usually
5627 sufficient. Expanding such a variable will yield the same result (empty
5628 string) regardless of whether it was set or not. However, if you are
5629 using the @code{flavor} (@pxref{Flavor Function}) and
5630 @code{origin} (@pxref{Origin Function}) functions, there is a difference
5631 between a variable that was never set and a variable with an empty value.
5632 In such situations you may want to use the @code{undefine} directive to
5633 make a variable appear as if it was never set. For example:
5642 $(info $(origin foo))
5643 $(info $(flavor bar))
5646 This example will print ``undefined'' for both variables.
5648 If you want to undefine a command-line variable definition, you can use
5649 the @code{override} directive together with @code{undefine}, similar to
5650 how this is done for variable definitions:
5653 override undefine CFLAGS
5656 @node Environment, Target-specific, Undefine Directive, Using Variables
5657 @section Variables from the Environment
5659 @cindex variables, environment
5661 Variables in @code{make} can come from the environment in which
5662 @code{make} is run. Every environment variable that @code{make} sees
5663 when it starts up is transformed into a @code{make} variable with the
5664 same name and value. However, an explicit assignment in the makefile,
5665 or with a command argument, overrides the environment. (If the
5666 @samp{-e} flag is specified, then values from the environment override
5667 assignments in the makefile. @xref{Options Summary, ,Summary of
5668 Options}. But this is not recommended practice.)
5670 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5671 cause all C compilations in most makefiles to use the compiler switches you
5672 prefer. This is safe for variables with standard or conventional meanings
5673 because you know that no makefile will use them for other things. (Note
5674 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5675 and therefore are not affected by the value in the environment.)
5677 When @code{make} runs a recipe, variables defined in the
5678 makefile are placed into the environment of each shell. This allows
5679 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5680 ,Recursive Use of @code{make}}). By default, only variables that came
5681 from the environment or the command line are passed to recursive
5682 invocations. You can use the @code{export} directive to pass other
5683 variables. @xref{Variables/Recursion, , Communicating Variables to a
5684 Sub-@code{make}}, for full details.
5686 Other use of variables from the environment is not recommended. It is not
5687 wise for makefiles to depend for their functioning on environment variables
5688 set up outside their control, since this would cause different users to get
5689 different results from the same makefile. This is against the whole
5690 purpose of most makefiles.
5692 @cindex SHELL, import from environment
5693 Such problems would be especially likely with the variable
5694 @code{SHELL}, which is normally present in the environment to specify
5695 the user's choice of interactive shell. It would be very undesirable
5696 for this choice to affect @code{make}; so, @code{make} handles the
5697 @code{SHELL} environment variable in a special way; see @ref{Choosing
5700 @node Target-specific, Pattern-specific, Environment, Using Variables
5701 @section Target-specific Variable Values
5702 @cindex target-specific variables
5703 @cindex variables, target-specific
5705 Variable values in @code{make} are usually global; that is, they are the
5706 same regardless of where they are evaluated (unless they're reset, of
5707 course). One exception to that is automatic variables
5708 (@pxref{Automatic Variables}).
5710 The other exception is @dfn{target-specific variable values}. This
5711 feature allows you to define different values for the same variable,
5712 based on the target that @code{make} is currently building. As with
5713 automatic variables, these values are only available within the context
5714 of a target's recipe (and in other target-specific assignments).
5716 Set a target-specific variable value like this:
5719 @var{target} @dots{} : @var{variable-assignment}
5722 Target-specific variable assignments can be prefixed with any or all of the
5723 special keywords @code{export}, @code{override}, or @code{private};
5724 these apply their normal behavior to this instance of the variable only.
5726 Multiple @var{target} values create a target-specific variable value for
5727 each member of the target list individually.
5729 The @var{variable-assignment} can be any valid form of assignment;
5730 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5731 conditional (@samp{?=}). All variables that appear within the
5732 @var{variable-assignment} are evaluated within the context of the
5733 target: thus, any previously-defined target-specific variable values
5734 will be in effect. Note that this variable is actually distinct from
5735 any ``global'' value: the two variables do not have to have the same
5736 flavor (recursive vs.@: static).
5738 Target-specific variables have the same priority as any other makefile
5739 variable. Variables provided on the command line (and in the
5740 environment if the @samp{-e} option is in force) will take precedence.
5741 Specifying the @code{override} directive will allow the target-specific
5742 variable value to be preferred.
5744 There is one more special feature of target-specific variables: when
5745 you define a target-specific variable that variable value is also in
5746 effect for all prerequisites of this target, and all their
5747 prerequisites, etc.@: (unless those prerequisites override that variable
5748 with their own target-specific variable value). So, for example, a
5749 statement like this:
5753 prog : prog.o foo.o bar.o
5757 will set @code{CFLAGS} to @samp{-g} in the recipe for @file{prog}, but
5758 it will also set @code{CFLAGS} to @samp{-g} in the recipes that create
5759 @file{prog.o}, @file{foo.o}, and @file{bar.o}, and any recipes which
5760 create their prerequisites.
5762 Be aware that a given prerequisite will only be built once per
5763 invocation of make, at most. If the same file is a prerequisite of
5764 multiple targets, and each of those targets has a different value for
5765 the same target-specific variable, then the first target to be built
5766 will cause that prerequisite to be built and the prerequisite will
5767 inherit the target-specific value from the first target. It will
5768 ignore the target-specific values from any other targets.
5770 @node Pattern-specific, Suppressing Inheritance, Target-specific, Using Variables
5771 @section Pattern-specific Variable Values
5772 @cindex pattern-specific variables
5773 @cindex variables, pattern-specific
5775 In addition to target-specific variable values
5776 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5777 @code{make} supports pattern-specific variable values. In this form,
5778 the variable is defined for any target that matches the pattern
5781 Set a pattern-specific variable value like this:
5784 @var{pattern} @dots{} : @var{variable-assignment}
5786 where @var{pattern} is a %-pattern. As with target-specific variable
5787 values, multiple @var{pattern} values create a pattern-specific variable
5788 value for each pattern individually. The @var{variable-assignment} can
5789 be any valid form of assignment. Any command line variable setting will
5790 take precedence, unless @code{override} is specified.
5799 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5800 matching the pattern @code{%.o}.
5802 If a target matches more than one pattern, the matching pattern-specific
5803 variables with longer stems are interpreted first. This results in more
5804 specific variables taking precedence over the more generic ones, for
5809 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
5811 lib/%.o: CFLAGS := -fPIC -g
5814 all: foo.o lib/bar.o
5817 In this example the first definition of the @code{CFLAGS} variable
5818 will be used to update @file{lib/bar.o} even though the second one
5819 also applies to this target. Pattern-specific variables which result
5820 in the same stem length are considered in the order in which they
5821 were defined in the makefile.
5823 Pattern-specific variables are searched after any target-specific
5824 variables defined explicitly for that target, and before target-specific
5825 variables defined for the parent target.
5827 @node Suppressing Inheritance, Special Variables, Pattern-specific, Using Variables
5828 @section Suppressing Inheritance
5830 @cindex suppressing inheritance
5831 @cindex inheritance, suppressing
5833 As described in previous sections, @code{make} variables are inherited
5834 by prerequisites. This capability allows you to modify the behavior
5835 of a prerequisite based on which targets caused it to be rebuilt. For
5836 example, you might set a target-specific variable on a @code{debug}
5837 target, then running @samp{make debug} will cause that variable to be
5838 inherited by all prerequisites of @code{debug}, while just running
5839 @samp{make all} (for example) would not have that assignment.
5841 Sometimes, however, you may not want a variable to be inherited. For
5842 these situations, @code{make} provides the @code{private} modifier.
5843 Although this modifier can be used with any variable assignment, it
5844 makes the most sense with target- and pattern-specific variables. Any
5845 variable marked @code{private} will be visible to its local target but
5846 will not be inherited by prerequisites of that target. A global
5847 variable marked @code{private} will be visible in the global scope but
5848 will not be inherited by any target, and hence will not be visible
5851 As an example, consider this makefile:
5855 prog: private EXTRA_CFLAGS = -L/usr/local/lib
5859 Due to the @code{private} modifier, @code{a.o} and @code{b.o} will not
5860 inherit the @code{EXTRA_CFLAGS} variable assignment from the
5861 @code{progs} target.
5863 @node Special Variables, , Suppressing Inheritance, Using Variables
5864 @comment node-name, next, previous, up
5865 @section Other Special Variables
5866 @cindex makefiles, and special variables
5867 @cindex special variables
5869 GNU @code{make} supports some variables that have special properties.
5873 @vindex MAKEFILE_LIST @r{(list of parsed makefiles)}
5874 @cindex makefiles, and @code{MAKEFILE_LIST} variable
5875 @cindex including (@code{MAKEFILE_LIST} variable)
5877 Contains the name of each makefile that is parsed by @code{make}, in
5878 the order in which it was parsed. The name is appended just
5879 before @code{make} begins to parse the makefile. Thus, if the first
5880 thing a makefile does is examine the last word in this variable, it
5881 will be the name of the current makefile. Once the current makefile
5882 has used @code{include}, however, the last word will be the
5883 just-included makefile.
5885 If a makefile named @code{Makefile} has this content:
5889 name1 := $(lastword $(MAKEFILE_LIST))
5893 name2 := $(lastword $(MAKEFILE_LIST))
5896 @@echo name1 = $(name1)
5897 @@echo name2 = $(name2)
5902 then you would expect to see this output:
5911 @vindex .DEFAULT_GOAL @r{(define default goal)}
5913 Sets the default goal to be used if no targets were specified on the
5914 command line (@pxref{Goals, , Arguments to Specify the Goals}). The
5915 @code{.DEFAULT_GOAL} variable allows you to discover the current
5916 default goal, restart the default goal selection algorithm by clearing
5917 its value, or to explicitly set the default goal. The following
5918 example illustrates these cases:
5922 # Query the default goal.
5923 ifeq ($(.DEFAULT_GOAL),)
5924 $(warning no default goal is set)
5930 $(warning default goal is $(.DEFAULT_GOAL))
5932 # Reset the default goal.
5938 $(warning default goal is $(.DEFAULT_GOAL))
5941 .DEFAULT_GOAL := foo
5945 This makefile prints:
5949 no default goal is set
5956 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
5957 illegal and will result in an error.
5959 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
5961 This variable is set only if this instance of @code{make} has
5962 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
5963 will contain the number of times this instance has restarted. Note
5964 this is not the same as recursion (counted by the @code{MAKELEVEL}
5965 variable). You should not set, modify, or export this variable.
5967 @vindex .CMDPREFIX @r{(change the recipe prefix character)}
5969 The first character of the value of this variable is used as the
5970 character make assumes is introducing a recipe line. If the variable
5971 is empty (as it is by default) that character is the standard tab
5972 character. For example, this is a valid makefile:
5978 > @@echo Hello, world
5982 The value of @code{.CMDPREFIX} can be changed multiple times; once set
5983 it stays in effect for all rules parsed until it is modified.
5985 @vindex .VARIABLES @r{(list of variables)}
5987 Expands to a list of the @emph{names} of all global variables defined
5988 so far. This includes variables which have empty values, as well as
5989 built-in variables (@pxref{Implicit Variables, , Variables Used by
5990 Implicit Rules}), but does not include any variables which are only
5991 defined in a target-specific context. Note that any value you assign
5992 to this variable will be ignored; it will always return its special
5995 @c @vindex .TARGETS @r{(list of targets)}
5997 @c The second special variable is @code{.TARGETS}. When expanded, the
5998 @c value consists of a list of all targets defined in all makefiles read
5999 @c up until that point. Note it's not enough for a file to be simply
6000 @c mentioned in the makefile to be listed in this variable, even if it
6001 @c would match an implicit rule and become an ``implicit target''. The
6002 @c file must appear as a target, on the left-hand side of a ``:'', to be
6003 @c considered a target for the purposes of this variable.
6005 @vindex .FEATURES @r{(list of supported features)}
6007 Expands to a list of special features supported by this version of
6008 @code{make}. Possible values include:
6013 Supports @code{ar} (archive) files using special filename syntax.
6014 @xref{Archives, ,Using @code{make} to Update Archive Files}.
6017 Supports the @code{-L} (@code{--check-symlink-times}) flag.
6018 @xref{Options Summary, ,Summary of Options}.
6021 Supports ``else if'' non-nested conditionals. @xref{Conditional
6022 Syntax, ,Syntax of Conditionals}.
6025 Supports ``job server'' enhanced parallel builds. @xref{Parallel,
6026 ,Parallel Execution}.
6028 @item second-expansion
6029 Supports secondary expansion of prerequisite lists.
6032 Supports order-only prerequisites. @xref{Prerequisite Types, ,Types
6035 @item target-specific
6036 Supports target-specific and pattern-specific variable assignments.
6037 @xref{Target-specific, ,Target-specific Variable Values}.
6041 @vindex .INCLUDE_DIRS @r{(list of include directories)}
6043 Expands to a list of directories that @code{make} searches for
6044 included makefiles (@pxref{Include, , Including Other Makefiles}).
6048 @node Conditionals, Functions, Using Variables, Top
6049 @chapter Conditional Parts of Makefiles
6051 @cindex conditionals
6052 A @dfn{conditional} directive causes part of a makefile to be obeyed
6053 or ignored depending on the values of variables. Conditionals can
6054 compare the value of one variable to another, or the value of a
6055 variable to a constant string. Conditionals control what @code{make}
6056 actually ``sees'' in the makefile, so they @emph{cannot} be used to
6057 control recipes at the time of execution.@refill
6060 * Conditional Example:: Example of a conditional
6061 * Conditional Syntax:: The syntax of conditionals.
6062 * Testing Flags:: Conditionals that test flags.
6065 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
6066 @section Example of a Conditional
6068 The following example of a conditional tells @code{make} to use one
6069 set of libraries if the @code{CC} variable is @samp{gcc}, and a
6070 different set of libraries otherwise. It works by controlling which
6071 of two recipe lines will be used for the rule. The result is that
6072 @samp{CC=gcc} as an argument to @code{make} changes not only which
6073 compiler is used but also which libraries are linked.
6076 libs_for_gcc = -lgnu
6081 $(CC) -o foo $(objects) $(libs_for_gcc)
6083 $(CC) -o foo $(objects) $(normal_libs)
6087 This conditional uses three directives: one @code{ifeq}, one @code{else}
6088 and one @code{endif}.
6090 The @code{ifeq} directive begins the conditional, and specifies the
6091 condition. It contains two arguments, separated by a comma and surrounded
6092 by parentheses. Variable substitution is performed on both arguments and
6093 then they are compared. The lines of the makefile following the
6094 @code{ifeq} are obeyed if the two arguments match; otherwise they are
6097 The @code{else} directive causes the following lines to be obeyed if the
6098 previous conditional failed. In the example above, this means that the
6099 second alternative linking command is used whenever the first alternative
6100 is not used. It is optional to have an @code{else} in a conditional.
6102 The @code{endif} directive ends the conditional. Every conditional must
6103 end with an @code{endif}. Unconditional makefile text follows.
6105 As this example illustrates, conditionals work at the textual level:
6106 the lines of the conditional are treated as part of the makefile, or
6107 ignored, according to the condition. This is why the larger syntactic
6108 units of the makefile, such as rules, may cross the beginning or the
6109 end of the conditional.
6111 When the variable @code{CC} has the value @samp{gcc}, the above example has
6116 $(CC) -o foo $(objects) $(libs_for_gcc)
6120 When the variable @code{CC} has any other value, the effect is this:
6124 $(CC) -o foo $(objects) $(normal_libs)
6127 Equivalent results can be obtained in another way by conditionalizing a
6128 variable assignment and then using the variable unconditionally:
6131 libs_for_gcc = -lgnu
6135 libs=$(libs_for_gcc)
6141 $(CC) -o foo $(objects) $(libs)
6144 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
6145 @section Syntax of Conditionals
6153 The syntax of a simple conditional with no @code{else} is as follows:
6156 @var{conditional-directive}
6162 The @var{text-if-true} may be any lines of text, to be considered as part
6163 of the makefile if the condition is true. If the condition is false, no
6164 text is used instead.
6166 The syntax of a complex conditional is as follows:
6169 @var{conditional-directive}
6179 @var{conditional-directive}
6180 @var{text-if-one-is-true}
6181 else @var{conditional-directive}
6189 There can be as many ``@code{else} @var{conditional-directive}''
6190 clauses as necessary. Once a given condition is true,
6191 @var{text-if-true} is used and no other clause is used; if no
6192 condition is true then @var{text-if-false} is used. The
6193 @var{text-if-true} and @var{text-if-false} can be any number of lines
6196 The syntax of the @var{conditional-directive} is the same whether the
6197 conditional is simple or complex; after an @code{else} or not. There
6198 are four different directives that test different conditions. Here is
6202 @item ifeq (@var{arg1}, @var{arg2})
6203 @itemx ifeq '@var{arg1}' '@var{arg2}'
6204 @itemx ifeq "@var{arg1}" "@var{arg2}"
6205 @itemx ifeq "@var{arg1}" '@var{arg2}'
6206 @itemx ifeq '@var{arg1}' "@var{arg2}"
6207 Expand all variable references in @var{arg1} and @var{arg2} and
6208 compare them. If they are identical, the @var{text-if-true} is
6209 effective; otherwise, the @var{text-if-false}, if any, is effective.
6211 Often you want to test if a variable has a non-empty value. When the
6212 value results from complex expansions of variables and functions,
6213 expansions you would consider empty may actually contain whitespace
6214 characters and thus are not seen as empty. However, you can use the
6215 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
6216 whitespace as a non-empty value. For example:
6220 ifeq ($(strip $(foo)),)
6227 will evaluate @var{text-if-empty} even if the expansion of
6228 @code{$(foo)} contains whitespace characters.
6230 @item ifneq (@var{arg1}, @var{arg2})
6231 @itemx ifneq '@var{arg1}' '@var{arg2}'
6232 @itemx ifneq "@var{arg1}" "@var{arg2}"
6233 @itemx ifneq "@var{arg1}" '@var{arg2}'
6234 @itemx ifneq '@var{arg1}' "@var{arg2}"
6235 Expand all variable references in @var{arg1} and @var{arg2} and
6236 compare them. If they are different, the @var{text-if-true} is
6237 effective; otherwise, the @var{text-if-false}, if any, is effective.
6239 @item ifdef @var{variable-name}
6240 The @code{ifdef} form takes the @emph{name} of a variable as its
6241 argument, not a reference to a variable. The value of that variable
6242 has a non-empty value, the @var{text-if-true} is effective; otherwise,
6243 the @var{text-if-false}, if any, is effective. Variables that have
6244 never been defined have an empty value. The text @var{variable-name}
6245 is expanded, so it could be a variable or function that expands
6246 to the name of a variable. For example:
6256 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6257 which is considered to be the name of a variable. The variable
6258 @code{bar} is not expanded, but its value is examined to determine if
6261 Note that @code{ifdef} only tests whether a variable has a value. It
6262 does not expand the variable to see if that value is nonempty.
6263 Consequently, tests using @code{ifdef} return true for all definitions
6264 except those like @code{foo =}. To test for an empty value, use
6265 @w{@code{ifeq ($(foo),)}}. For example,
6278 sets @samp{frobozz} to @samp{yes}, while:
6290 sets @samp{frobozz} to @samp{no}.
6292 @item ifndef @var{variable-name}
6293 If the variable @var{variable-name} has an empty value, the
6294 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
6295 if any, is effective. The rules for expansion and testing of
6296 @var{variable-name} are identical to the @code{ifdef} directive.
6299 Extra spaces are allowed and ignored at the beginning of the
6300 conditional directive line, but a tab is not allowed. (If the line
6301 begins with a tab, it will be considered part of a recipe for a rule.)
6302 Aside from this, extra spaces or tabs may be inserted with no effect
6303 anywhere except within the directive name or within an argument. A
6304 comment starting with @samp{#} may appear at the end of the line.
6306 The other two directives that play a part in a conditional are @code{else}
6307 and @code{endif}. Each of these directives is written as one word, with no
6308 arguments. Extra spaces are allowed and ignored at the beginning of the
6309 line, and spaces or tabs at the end. A comment starting with @samp{#} may
6310 appear at the end of the line.
6312 Conditionals affect which lines of the makefile @code{make} uses. If
6313 the condition is true, @code{make} reads the lines of the
6314 @var{text-if-true} as part of the makefile; if the condition is false,
6315 @code{make} ignores those lines completely. It follows that syntactic
6316 units of the makefile, such as rules, may safely be split across the
6317 beginning or the end of the conditional.@refill
6319 @code{make} evaluates conditionals when it reads a makefile.
6320 Consequently, you cannot use automatic variables in the tests of
6321 conditionals because they are not defined until recipes are run
6322 (@pxref{Automatic Variables}).
6324 To prevent intolerable confusion, it is not permitted to start a
6325 conditional in one makefile and end it in another. However, you may
6326 write an @code{include} directive within a conditional, provided you do
6327 not attempt to terminate the conditional inside the included file.
6329 @node Testing Flags, , Conditional Syntax, Conditionals
6330 @section Conditionals that Test Flags
6332 You can write a conditional that tests @code{make} command flags such as
6333 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
6334 @code{findstring} function
6335 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6336 This is useful when @code{touch} is not enough to make a file appear up
6339 The @code{findstring} function determines whether one string appears as a
6340 substring of another. If you want to test for the @samp{-t} flag,
6341 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6344 For example, here is how to arrange to use @samp{ranlib -t} to finish
6345 marking an archive file up to date:
6349 ifneq (,$(findstring t,$(MAKEFLAGS)))
6351 +ranlib -t archive.a
6358 The @samp{+} prefix marks those recipe lines as ``recursive'' so that
6359 they will be executed despite use of the @samp{-t} flag.
6360 @xref{Recursion, ,Recursive Use of @code{make}}.
6362 @node Functions, Running, Conditionals, Top
6363 @chapter Functions for Transforming Text
6366 @dfn{Functions} allow you to do text processing in the makefile to
6367 compute the files to operate on or the commands to use in recipes.
6368 You use a function in a @dfn{function call}, where you give the name
6369 of the function and some text (the @dfn{arguments}) for the function
6370 to operate on. The result of the function's processing is substituted
6371 into the makefile at the point of the call, just as a variable might
6375 * Syntax of Functions:: How to write a function call.
6376 * Text Functions:: General-purpose text manipulation functions.
6377 * File Name Functions:: Functions for manipulating file names.
6378 * Conditional Functions:: Functions that implement conditions.
6379 * Foreach Function:: Repeat some text with controlled variation.
6380 * Call Function:: Expand a user-defined function.
6381 * Value Function:: Return the un-expanded value of a variable.
6382 * Eval Function:: Evaluate the arguments as makefile syntax.
6383 * Origin Function:: Find where a variable got its value.
6384 * Flavor Function:: Find out the flavor of a variable.
6385 * Shell Function:: Substitute the output of a shell command.
6386 * Make Control Functions:: Functions that control how make runs.
6389 @node Syntax of Functions, Text Functions, Functions, Functions
6390 @section Function Call Syntax
6391 @cindex @code{$}, in function call
6392 @cindex dollar sign (@code{$}), in function call
6393 @cindex arguments of functions
6394 @cindex functions, syntax of
6396 A function call resembles a variable reference. It looks like this:
6399 $(@var{function} @var{arguments})
6406 $@{@var{function} @var{arguments}@}
6409 Here @var{function} is a function name; one of a short list of names
6410 that are part of @code{make}. You can also essentially create your own
6411 functions by using the @code{call} builtin function.
6413 The @var{arguments} are the arguments of the function. They are
6414 separated from the function name by one or more spaces or tabs, and if
6415 there is more than one argument, then they are separated by commas.
6416 Such whitespace and commas are not part of an argument's value. The
6417 delimiters which you use to surround the function call, whether
6418 parentheses or braces, can appear in an argument only in matching pairs;
6419 the other kind of delimiters may appear singly. If the arguments
6420 themselves contain other function calls or variable references, it is
6421 wisest to use the same kind of delimiters for all the references; write
6422 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This
6423 is because it is clearer, and because only one type of delimiter is
6424 matched to find the end of the reference.
6426 The text written for each argument is processed by substitution of
6427 variables and function calls to produce the argument value, which
6428 is the text on which the function acts. The substitution is done in the
6429 order in which the arguments appear.
6431 Commas and unmatched parentheses or braces cannot appear in the text of an
6432 argument as written; leading spaces cannot appear in the text of the first
6433 argument as written. These characters can be put into the argument value
6434 by variable substitution. First define variables @code{comma} and
6435 @code{space} whose values are isolated comma and space characters, then
6436 substitute these variables where such characters are wanted, like this:
6442 space:= $(empty) $(empty)
6444 bar:= $(subst $(space),$(comma),$(foo))
6445 # @r{bar is now `a,b,c'.}
6450 Here the @code{subst} function replaces each space with a comma, through
6451 the value of @code{foo}, and substitutes the result.
6453 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6454 @section Functions for String Substitution and Analysis
6455 @cindex functions, for text
6457 Here are some functions that operate on strings:
6460 @item $(subst @var{from},@var{to},@var{text})
6462 Performs a textual replacement on the text @var{text}: each occurrence
6463 of @var{from} is replaced by @var{to}. The result is substituted for
6464 the function call. For example,
6467 $(subst ee,EE,feet on the street)
6470 substitutes the string @samp{fEEt on the strEEt}.
6472 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6474 Finds whitespace-separated words in @var{text} that match
6475 @var{pattern} and replaces them with @var{replacement}. Here
6476 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6477 matching any number of any characters within a word. If
6478 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6479 by the text that matched the @samp{%} in @var{pattern}. Only the first
6480 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6481 way; any subsequent @samp{%} is unchanged.@refill
6483 @cindex @code{%}, quoting in @code{patsubst}
6484 @cindex @code{%}, quoting with @code{\} (backslash)
6485 @cindex @code{\} (backslash), to quote @code{%}
6486 @cindex backslash (@code{\}), to quote @code{%}
6487 @cindex quoting @code{%}, in @code{patsubst}
6488 @samp{%} characters in @code{patsubst} function invocations can be
6489 quoted with preceding backslashes (@samp{\}). Backslashes that would
6490 otherwise quote @samp{%} characters can be quoted with more backslashes.
6491 Backslashes that quote @samp{%} characters or other backslashes are
6492 removed from the pattern before it is compared file names or has a stem
6493 substituted into it. Backslashes that are not in danger of quoting
6494 @samp{%} characters go unmolested. For example, the pattern
6495 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6496 operative @samp{%} character, and @samp{pattern\\} following it. The
6497 final two backslashes are left alone because they cannot affect any
6498 @samp{%} character.@refill
6500 Whitespace between words is folded into single space characters;
6501 leading and trailing whitespace is discarded.
6506 $(patsubst %.c,%.o,x.c.c bar.c)
6510 produces the value @samp{x.c.o bar.o}.
6512 Substitution references (@pxref{Substitution Refs, ,Substitution
6513 References}) are a simpler way to get the effect of the @code{patsubst}
6517 $(@var{var}:@var{pattern}=@var{replacement})
6524 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6527 The second shorthand simplifies one of the most common uses of
6528 @code{patsubst}: replacing the suffix at the end of file names.
6531 $(@var{var}:@var{suffix}=@var{replacement})
6538 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6542 For example, you might have a list of object files:
6545 objects = foo.o bar.o baz.o
6549 To get the list of corresponding source files, you could simply write:
6556 instead of using the general form:
6559 $(patsubst %.o,%.c,$(objects))
6562 @item $(strip @var{string})
6563 @cindex stripping whitespace
6564 @cindex whitespace, stripping
6565 @cindex spaces, stripping
6567 Removes leading and trailing whitespace from @var{string} and replaces
6568 each internal sequence of one or more whitespace characters with a
6569 single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}.
6571 The function @code{strip} can be very useful when used in conjunction
6572 with conditionals. When comparing something with the empty string
6573 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6574 just whitespace to match the empty string (@pxref{Conditionals}).
6576 Thus, the following may fail to have the desired results:
6580 ifneq "$(needs_made)" ""
6583 all:;@@echo 'Nothing to make!'
6588 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6589 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6590 directive would make it more robust.@refill
6592 @item $(findstring @var{find},@var{in})
6594 @cindex searching for strings
6595 @cindex finding strings
6596 @cindex strings, searching for
6597 Searches @var{in} for an occurrence of @var{find}. If it occurs, the
6598 value is @var{find}; otherwise, the value is empty. You can use this
6599 function in a conditional to test for the presence of a specific
6600 substring in a given string. Thus, the two examples,
6603 $(findstring a,a b c)
6608 produce the values @samp{a} and @samp{} (the empty string),
6609 respectively. @xref{Testing Flags}, for a practical application of
6610 @code{findstring}.@refill
6614 @cindex filtering words
6615 @cindex words, filtering
6616 @item $(filter @var{pattern}@dots{},@var{text})
6617 Returns all whitespace-separated words in @var{text} that @emph{do} match
6618 any of the @var{pattern} words, removing any words that @emph{do not}
6619 match. The patterns are written using @samp{%}, just like the patterns
6620 used in the @code{patsubst} function above.@refill
6622 The @code{filter} function can be used to separate out different types
6623 of strings (such as file names) in a variable. For example:
6626 sources := foo.c bar.c baz.s ugh.h
6628 cc $(filter %.c %.s,$(sources)) -o foo
6632 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6633 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6634 @file{baz.s} should be specified in the command to the
6637 @item $(filter-out @var{pattern}@dots{},@var{text})
6639 @cindex filtering out words
6640 @cindex words, filtering out
6641 Returns all whitespace-separated words in @var{text} that @emph{do not}
6642 match any of the @var{pattern} words, removing the words that @emph{do}
6643 match one or more. This is the exact opposite of the @code{filter}
6650 objects=main1.o foo.o main2.o bar.o
6651 mains=main1.o main2.o
6656 the following generates a list which contains all the object files not
6660 $(filter-out $(mains),$(objects))
6665 @cindex sorting words
6666 @item $(sort @var{list})
6667 Sorts the words of @var{list} in lexical order, removing duplicate
6668 words. The output is a list of words separated by single spaces.
6672 $(sort foo bar lose)
6676 returns the value @samp{bar foo lose}.
6678 @cindex removing duplicate words
6679 @cindex duplicate words, removing
6680 @cindex words, removing duplicates
6681 Incidentally, since @code{sort} removes duplicate words, you can use
6682 it for this purpose even if you don't care about the sort order.
6684 @item $(word @var{n},@var{text})
6686 @cindex word, selecting a
6687 @cindex selecting a word
6688 Returns the @var{n}th word of @var{text}. The legitimate values of
6689 @var{n} start from 1. If @var{n} is bigger than the number of words
6690 in @var{text}, the value is empty. For example,
6693 $(word 2, foo bar baz)
6699 @item $(wordlist @var{s},@var{e},@var{text})
6701 @cindex words, selecting lists of
6702 @cindex selecting word lists
6703 Returns the list of words in @var{text} starting with word @var{s} and
6704 ending with word @var{e} (inclusive). The legitimate values of @var{s}
6705 start from 1; @var{e} may start from 0. If @var{s} is bigger than the
6706 number of words in @var{text}, the value is empty. If @var{e} is
6707 bigger than the number of words in @var{text}, words up to the end of
6708 @var{text} are returned. If @var{s} is greater than @var{e}, nothing
6709 is returned. For example,
6712 $(wordlist 2, 3, foo bar baz)
6716 returns @samp{bar baz}.
6718 @c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92
6719 @item $(words @var{text})
6721 @cindex words, finding number
6722 Returns the number of words in @var{text}.
6723 Thus, the last word of @var{text} is
6724 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6726 @item $(firstword @var{names}@dots{})
6728 @cindex words, extracting first
6729 The argument @var{names} is regarded as a series of names, separated
6730 by whitespace. The value is the first name in the series. The rest
6731 of the names are ignored.
6736 $(firstword foo bar)
6740 produces the result @samp{foo}. Although @code{$(firstword
6741 @var{text})} is the same as @code{$(word 1,@var{text})}, the
6742 @code{firstword} function is retained for its simplicity.@refill
6745 @item $(lastword @var{names}@dots{})
6747 @cindex words, extracting last
6748 The argument @var{names} is regarded as a series of names, separated
6749 by whitespace. The value is the last name in the series.
6758 produces the result @samp{bar}. Although @code{$(lastword
6759 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6760 the @code{lastword} function was added for its simplicity and better
6765 Here is a realistic example of the use of @code{subst} and
6766 @code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable
6767 to specify a list of directories that @code{make} should search for
6769 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6770 This example shows how to
6771 tell the C compiler to search for header files in the same list of
6774 The value of @code{VPATH} is a list of directories separated by colons,
6775 such as @samp{src:../headers}. First, the @code{subst} function is used to
6776 change the colons to spaces:
6779 $(subst :, ,$(VPATH))
6783 This produces @samp{src ../headers}. Then @code{patsubst} is used to turn
6784 each directory name into a @samp{-I} flag. These can be added to the
6785 value of the variable @code{CFLAGS}, which is passed automatically to the C
6786 compiler, like this:
6789 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6793 The effect is to append the text @samp{-Isrc -I../headers} to the
6794 previously given value of @code{CFLAGS}. The @code{override} directive is
6795 used so that the new value is assigned even if the previous value of
6796 @code{CFLAGS} was specified with a command argument (@pxref{Override
6797 Directive, , The @code{override} Directive}).
6799 @node File Name Functions, Conditional Functions, Text Functions, Functions
6800 @section Functions for File Names
6801 @cindex functions, for file names
6802 @cindex file name functions
6804 Several of the built-in expansion functions relate specifically to
6805 taking apart file names or lists of file names.
6807 Each of the following functions performs a specific transformation on a
6808 file name. The argument of the function is regarded as a series of file
6809 names, separated by whitespace. (Leading and trailing whitespace is
6810 ignored.) Each file name in the series is transformed in the same way and
6811 the results are concatenated with single spaces between them.
6814 @item $(dir @var{names}@dots{})
6816 @cindex directory part
6817 @cindex file name, directory part
6818 Extracts the directory-part of each file name in @var{names}. The
6819 directory-part of the file name is everything up through (and
6820 including) the last slash in it. If the file name contains no slash,
6821 the directory part is the string @samp{./}. For example,
6824 $(dir src/foo.c hacks)
6828 produces the result @samp{src/ ./}.
6830 @item $(notdir @var{names}@dots{})
6832 @cindex file name, nondirectory part
6833 @cindex nondirectory part
6834 Extracts all but the directory-part of each file name in @var{names}.
6835 If the file name contains no slash, it is left unchanged. Otherwise,
6836 everything through the last slash is removed from it.
6838 A file name that ends with a slash becomes an empty string. This is
6839 unfortunate, because it means that the result does not always have the
6840 same number of whitespace-separated file names as the argument had;
6841 but we do not see any other valid alternative.
6846 $(notdir src/foo.c hacks)
6850 produces the result @samp{foo.c hacks}.
6852 @item $(suffix @var{names}@dots{})
6854 @cindex suffix, function to find
6855 @cindex file name suffix
6856 Extracts the suffix of each file name in @var{names}. If the file name
6857 contains a period, the suffix is everything starting with the last
6858 period. Otherwise, the suffix is the empty string. This frequently
6859 means that the result will be empty when @var{names} is not, and if
6860 @var{names} contains multiple file names, the result may contain fewer
6866 $(suffix src/foo.c src-1.0/bar.c hacks)
6870 produces the result @samp{.c .c}.
6872 @item $(basename @var{names}@dots{})
6875 @cindex file name, basename of
6876 Extracts all but the suffix of each file name in @var{names}. If the
6877 file name contains a period, the basename is everything starting up to
6878 (and not including) the last period. Periods in the directory part are
6879 ignored. If there is no period, the basename is the entire file name.
6883 $(basename src/foo.c src-1.0/bar hacks)
6887 produces the result @samp{src/foo src-1.0/bar hacks}.
6889 @c plural convention with dots (be consistent)
6890 @item $(addsuffix @var{suffix},@var{names}@dots{})
6892 @cindex suffix, adding
6893 @cindex file name suffix, adding
6894 The argument @var{names} is regarded as a series of names, separated
6895 by whitespace; @var{suffix} is used as a unit. The value of
6896 @var{suffix} is appended to the end of each individual name and the
6897 resulting larger names are concatenated with single spaces between
6901 $(addsuffix .c,foo bar)
6905 produces the result @samp{foo.c bar.c}.
6907 @item $(addprefix @var{prefix},@var{names}@dots{})
6909 @cindex prefix, adding
6910 @cindex file name prefix, adding
6911 The argument @var{names} is regarded as a series of names, separated
6912 by whitespace; @var{prefix} is used as a unit. The value of
6913 @var{prefix} is prepended to the front of each individual name and the
6914 resulting larger names are concatenated with single spaces between
6918 $(addprefix src/,foo bar)
6922 produces the result @samp{src/foo src/bar}.
6924 @item $(join @var{list1},@var{list2})
6926 @cindex joining lists of words
6927 @cindex words, joining lists
6928 Concatenates the two arguments word by word: the two first words (one
6929 from each argument) concatenated form the first word of the result, the
6930 two second words form the second word of the result, and so on. So the
6931 @var{n}th word of the result comes from the @var{n}th word of each
6932 argument. If one argument has more words that the other, the extra
6933 words are copied unchanged into the result.
6935 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6937 Whitespace between the words in the lists is not preserved; it is
6938 replaced with a single space.
6940 This function can merge the results of the @code{dir} and
6941 @code{notdir} functions, to produce the original list of files which
6942 was given to those two functions.@refill
6944 @item $(wildcard @var{pattern})
6946 @cindex wildcard, function
6947 The argument @var{pattern} is a file name pattern, typically containing
6948 wildcard characters (as in shell file name patterns). The result of
6949 @code{wildcard} is a space-separated list of the names of existing files
6950 that match the pattern.
6951 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
6953 @item $(realpath @var{names}@dots{})
6956 @cindex file name, realpath of
6957 For each file name in @var{names} return the canonical absolute name.
6958 A canonical name does not contain any @code{.} or @code{..} components,
6959 nor any repeated path separators (@code{/}) or symlinks. In case of a
6960 failure the empty string is returned. Consult the @code{realpath(3)}
6961 documentation for a list of possible failure causes.
6963 @item $(abspath @var{names}@dots{})
6966 @cindex file name, abspath of
6967 For each file name in @var{names} return an absolute name that does
6968 not contain any @code{.} or @code{..} components, nor any repeated path
6969 separators (@code{/}). Note that, in contrast to @code{realpath}
6970 function, @code{abspath} does not resolve symlinks and does not require
6971 the file names to refer to an existing file or directory. Use the
6972 @code{wildcard} function to test for existence.
6975 @node Conditional Functions, Foreach Function, File Name Functions, Functions
6976 @section Functions for Conditionals
6978 @cindex conditional expansion
6979 There are three functions that provide conditional expansion. A key
6980 aspect of these functions is that not all of the arguments are
6981 expanded initially. Only those arguments which need to be expanded,
6985 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
6987 The @code{if} function provides support for conditional expansion in a
6988 functional context (as opposed to the GNU @code{make} makefile
6989 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6992 The first argument, @var{condition}, first has all preceding and
6993 trailing whitespace stripped, then is expanded. If it expands to any
6994 non-empty string, then the condition is considered to be true. If it
6995 expands to an empty string, the condition is considered to be false.
6997 If the condition is true then the second argument, @var{then-part}, is
6998 evaluated and this is used as the result of the evaluation of the entire
7001 If the condition is false then the third argument, @var{else-part}, is
7002 evaluated and this is the result of the @code{if} function. If there is
7003 no third argument, the @code{if} function evaluates to nothing (the
7006 Note that only one of the @var{then-part} or the @var{else-part} will be
7007 evaluated, never both. Thus, either can contain side-effects (such as
7008 @code{shell} function calls, etc.)
7010 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7012 The @code{or} function provides a ``short-circuiting'' OR operation.
7013 Each argument is expanded, in order. If an argument expands to a
7014 non-empty string the processing stops and the result of the expansion
7015 is that string. If, after all arguments are expanded, all of them are
7016 false (empty), then the result of the expansion is the empty string.
7018 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7020 The @code{and} function provides a ``short-circuiting'' AND operation.
7021 Each argument is expanded, in order. If an argument expands to an
7022 empty string the processing stops and the result of the expansion is
7023 the empty string. If all arguments expand to a non-empty string then
7024 the result of the expansion is the expansion of the last argument.
7028 @node Foreach Function, Call Function, Conditional Functions, Functions
7029 @section The @code{foreach} Function
7031 @cindex words, iterating over
7033 The @code{foreach} function is very different from other functions. It
7034 causes one piece of text to be used repeatedly, each time with a different
7035 substitution performed on it. It resembles the @code{for} command in the
7036 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
7038 The syntax of the @code{foreach} function is:
7041 $(foreach @var{var},@var{list},@var{text})
7045 The first two arguments, @var{var} and @var{list}, are expanded before
7046 anything else is done; note that the last argument, @var{text}, is
7047 @strong{not} expanded at the same time. Then for each word of the expanded
7048 value of @var{list}, the variable named by the expanded value of @var{var}
7049 is set to that word, and @var{text} is expanded. Presumably @var{text}
7050 contains references to that variable, so its expansion will be different
7053 The result is that @var{text} is expanded as many times as there are
7054 whitespace-separated words in @var{list}. The multiple expansions of
7055 @var{text} are concatenated, with spaces between them, to make the result
7058 This simple example sets the variable @samp{files} to the list of all files
7059 in the directories in the list @samp{dirs}:
7063 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
7066 Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition
7067 finds the value @samp{a} for @code{dir}, so it produces the same result
7068 as @samp{$(wildcard a/*)}; the second repetition produces the result
7069 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
7071 This example has the same result (except for setting @samp{dirs}) as
7072 the following example:
7075 files := $(wildcard a/* b/* c/* d/*)
7078 When @var{text} is complicated, you can improve readability by giving it
7079 a name, with an additional variable:
7082 find_files = $(wildcard $(dir)/*)
7084 files := $(foreach dir,$(dirs),$(find_files))
7088 Here we use the variable @code{find_files} this way. We use plain @samp{=}
7089 to define a recursively-expanding variable, so that its value contains an
7090 actual function call to be reexpanded under the control of @code{foreach};
7091 a simply-expanded variable would not do, since @code{wildcard} would be
7092 called only once at the time of defining @code{find_files}.
7094 The @code{foreach} function has no permanent effect on the variable
7095 @var{var}; its value and flavor after the @code{foreach} function call are
7096 the same as they were beforehand. The other values which are taken from
7097 @var{list} are in effect only temporarily, during the execution of
7098 @code{foreach}. The variable @var{var} is a simply-expanded variable
7099 during the execution of @code{foreach}. If @var{var} was undefined
7100 before the @code{foreach} function call, it is undefined after the call.
7101 @xref{Flavors, ,The Two Flavors of Variables}.@refill
7103 You must take care when using complex variable expressions that result in
7104 variable names because many strange things are valid variable names, but
7105 are probably not what you intended. For example,
7108 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
7112 might be useful if the value of @code{find_files} references the variable
7113 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
7114 no?), but it is more likely to be a mistake.
7116 @node Call Function, Value Function, Foreach Function, Functions
7117 @section The @code{call} Function
7119 @cindex functions, user defined
7120 @cindex user defined functions
7122 The @code{call} function is unique in that it can be used to create new
7123 parameterized functions. You can write a complex expression as the
7124 value of a variable, then use @code{call} to expand it with different
7127 The syntax of the @code{call} function is:
7130 $(call @var{variable},@var{param},@var{param},@dots{})
7133 When @code{make} expands this function, it assigns each @var{param} to
7134 temporary variables @code{$(1)}, @code{$(2)}, etc. The variable
7135 @code{$(0)} will contain @var{variable}. There is no maximum number of
7136 parameter arguments. There is no minimum, either, but it doesn't make
7137 sense to use @code{call} with no parameters.
7139 Then @var{variable} is expanded as a @code{make} variable in the context
7140 of these temporary assignments. Thus, any reference to @code{$(1)} in
7141 the value of @var{variable} will resolve to the first @var{param} in the
7142 invocation of @code{call}.
7144 Note that @var{variable} is the @emph{name} of a variable, not a
7145 @emph{reference} to that variable. Therefore you would not normally use
7146 a @samp{$} or parentheses when writing it. (You can, however, use a
7147 variable reference in the name if you want the name not to be a
7150 If @var{variable} is the name of a builtin function, the builtin function
7151 is always invoked (even if a @code{make} variable by that name also
7154 The @code{call} function expands the @var{param} arguments before
7155 assigning them to temporary variables. This means that @var{variable}
7156 values containing references to builtin functions that have special
7157 expansion rules, like @code{foreach} or @code{if}, may not work as you
7160 Some examples may make this clearer.
7162 This macro simply reverses its arguments:
7167 foo = $(call reverse,a,b)
7171 Here @var{foo} will contain @samp{b a}.
7173 This one is slightly more interesting: it defines a macro to search for
7174 the first instance of a program in @code{PATH}:
7177 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7179 LS := $(call pathsearch,ls)
7183 Now the variable LS contains @code{/bin/ls} or similar.
7185 The @code{call} function can be nested. Each recursive invocation gets
7186 its own local values for @code{$(1)}, etc.@: that mask the values of
7187 higher-level @code{call}. For example, here is an implementation of a
7191 map = $(foreach a,$(2),$(call $(1),$(a)))
7194 Now you can @var{map} a function that normally takes only one argument,
7195 such as @code{origin}, to multiple values in one step:
7198 o = $(call map,origin,o map MAKE)
7201 and end up with @var{o} containing something like @samp{file file default}.
7203 A final caution: be careful when adding whitespace to the arguments to
7204 @code{call}. As with other functions, any whitespace contained in the
7205 second and subsequent arguments is kept; this can cause strange
7206 effects. It's generally safest to remove all extraneous whitespace when
7207 providing parameters to @code{call}.
7209 @node Value Function, Eval Function, Call Function, Functions
7210 @comment node-name, next, previous, up
7211 @section The @code{value} Function
7213 @cindex variables, unexpanded value
7215 The @code{value} function provides a way for you to use the value of a
7216 variable @emph{without} having it expanded. Please note that this
7217 does not undo expansions which have already occurred; for example if
7218 you create a simply expanded variable its value is expanded during the
7219 definition; in that case the @code{value} function will return the
7220 same result as using the variable directly.
7222 The syntax of the @code{value} function is:
7225 $(value @var{variable})
7228 Note that @var{variable} is the @emph{name} of a variable; not a
7229 @emph{reference} to that variable. Therefore you would not normally
7230 use a @samp{$} or parentheses when writing it. (You can, however, use
7231 a variable reference in the name if you want the name not to be a
7234 The result of this function is a string containing the value of
7235 @var{variable}, without any expansion occurring. For example, in this
7249 The first output line would be @code{ATH}, since the ``$P'' would be
7250 expanded as a @code{make} variable, while the second output line would
7251 be the current value of your @code{$PATH} environment variable, since
7252 the @code{value} function avoided the expansion.
7254 The @code{value} function is most often used in conjunction with the
7255 @code{eval} function (@pxref{Eval Function}).
7257 @node Eval Function, Origin Function, Value Function, Functions
7258 @comment node-name, next, previous, up
7259 @section The @code{eval} Function
7261 @cindex evaluating makefile syntax
7262 @cindex makefile syntax, evaluating
7264 The @code{eval} function is very special: it allows you to define new
7265 makefile constructs that are not constant; which are the result of
7266 evaluating other variables and functions. The argument to the
7267 @code{eval} function is expanded, then the results of that expansion
7268 are parsed as makefile syntax. The expanded results can define new
7269 @code{make} variables, targets, implicit or explicit rules, etc.
7271 The result of the @code{eval} function is always the empty string;
7272 thus, it can be placed virtually anywhere in a makefile without
7273 causing syntax errors.
7275 It's important to realize that the @code{eval} argument is expanded
7276 @emph{twice}; first by the @code{eval} function, then the results of
7277 that expansion are expanded again when they are parsed as makefile
7278 syntax. This means you may need to provide extra levels of escaping
7279 for ``$'' characters when using @code{eval}. The @code{value}
7280 function (@pxref{Value Function}) can sometimes be useful in these
7281 situations, to circumvent unwanted expansions.
7283 Here is an example of how @code{eval} can be used; this example
7284 combines a number of concepts and other functions. Although it might
7285 seem overly complex to use @code{eval} in this example, rather than
7286 just writing out the rules, consider two things: first, the template
7287 definition (in @code{PROGRAM_template}) could need to be much more
7288 complex than it is here; and second, you might put the complex,
7289 ``generic'' part of this example into another makefile, then include
7290 it in all the individual makefiles. Now your individual makefiles are
7291 quite straightforward.
7295 PROGRAMS = server client
7297 server_OBJS = server.o server_priv.o server_access.o
7298 server_LIBS = priv protocol
7300 client_OBJS = client.o client_api.o client_mem.o
7301 client_LIBS = protocol
7303 # Everything after this is generic
7308 define PROGRAM_template =
7309 $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7310 ALL_OBJS += $$($(1)_OBJS)
7313 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7316 $(LINK.o) $^ $(LDLIBS) -o $@@
7319 rm -f $(ALL_OBJS) $(PROGRAMS)
7323 @node Origin Function, Flavor Function, Eval Function, Functions
7324 @section The @code{origin} Function
7326 @cindex variables, origin of
7327 @cindex origin of variable
7329 The @code{origin} function is unlike most other functions in that it does
7330 not operate on the values of variables; it tells you something @emph{about}
7331 a variable. Specifically, it tells you where it came from.
7333 The syntax of the @code{origin} function is:
7336 $(origin @var{variable})
7339 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7340 not a @emph{reference} to that variable. Therefore you would not normally
7341 use a @samp{$} or parentheses when writing it. (You can, however, use a
7342 variable reference in the name if you want the name not to be a constant.)
7344 The result of this function is a string telling you how the variable
7345 @var{variable} was defined:
7350 if @var{variable} was never defined.
7354 if @var{variable} has a default definition, as is usual with @code{CC}
7355 and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7356 Note that if you have redefined a default variable, the @code{origin}
7357 function will return the origin of the later definition.
7361 if @var{variable} was inherited from the environment provided to
7364 @item environment override
7366 if @var{variable} was inherited from the environment provided to
7367 @code{make}, and is overriding a setting for @var{variable} in the
7368 makefile as a result of the @w{@samp{-e}} option (@pxref{Options
7369 Summary, ,Summary of Options}).@refill
7373 if @var{variable} was defined in a makefile.
7377 if @var{variable} was defined on the command line.
7381 if @var{variable} was defined with an @code{override} directive in a
7382 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7386 if @var{variable} is an automatic variable defined for the execution
7387 of the recipe for each rule (@pxref{Automatic Variables}).
7390 This information is primarily useful (other than for your curiosity) to
7391 determine if you want to believe the value of a variable. For example,
7392 suppose you have a makefile @file{foo} that includes another makefile
7393 @file{bar}. You want a variable @code{bletch} to be defined in @file{bar}
7394 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7395 a definition of @code{bletch}. However, if @file{foo} defined
7396 @code{bletch} before including @file{bar}, you do not want to override that
7397 definition. This could be done by using an @code{override} directive in
7398 @file{foo}, giving that definition precedence over the later definition in
7399 @file{bar}; unfortunately, the @code{override} directive would also
7400 override any command line definitions. So, @file{bar} could
7406 ifeq "$(origin bletch)" "environment"
7407 bletch = barf, gag, etc.
7414 If @code{bletch} has been defined from the environment, this will redefine
7417 If you want to override a previous definition of @code{bletch} if it came
7418 from the environment, even under @samp{-e}, you could instead write:
7422 ifneq "$(findstring environment,$(origin bletch))" ""
7423 bletch = barf, gag, etc.
7428 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7429 @samp{environment} or @samp{environment override}.
7430 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7432 @node Flavor Function, Shell Function, Origin Function, Functions
7433 @section The @code{flavor} Function
7435 @cindex variables, flavor of
7436 @cindex flavor of variable
7438 The @code{flavor} function is unlike most other functions (and like
7439 @code{origin} function) in that it does not operate on the values of
7440 variables; it tells you something @emph{about} a variable.
7441 Specifically, it tells you the flavor of a variable (@pxref{Flavors,
7442 ,The Two Flavors of Variables}).
7444 The syntax of the @code{flavor} function is:
7447 $(flavor @var{variable})
7450 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7451 not a @emph{reference} to that variable. Therefore you would not normally
7452 use a @samp{$} or parentheses when writing it. (You can, however, use a
7453 variable reference in the name if you want the name not to be a constant.)
7455 The result of this function is a string that identifies the flavor of the
7456 variable @var{variable}:
7461 if @var{variable} was never defined.
7465 if @var{variable} is a recursively expanded variable.
7469 if @var{variable} is a simply expanded variable.
7474 @node Shell Function, Make Control Functions, Flavor Function, Functions
7475 @section The @code{shell} Function
7477 @cindex command expansion
7479 @cindex shell command, function for
7481 The @code{shell} function is unlike any other function other than the
7482 @code{wildcard} function
7483 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7484 communicates with the world outside of @code{make}.
7486 The @code{shell} function performs the same function that backquotes
7487 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7488 This means that it takes as an argument a shell command and evaluates
7489 to the output of the command. The only processing @code{make} does on
7490 the result is to convert each newline (or carriage-return / newline
7491 pair) to a single space. If there is a trailing (carriage-return
7492 and) newline it will simply be removed.@refill
7494 The commands run by calls to the @code{shell} function are run when the
7495 function calls are expanded (@pxref{Reading Makefiles, , How
7496 @code{make} Reads a Makefile}). Because this function involves
7497 spawning a new shell, you should carefully consider the performance
7498 implications of using the @code{shell} function within recursively
7499 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7500 Two Flavors of Variables}).
7502 Here are some examples of the use of the @code{shell} function:
7505 contents := $(shell cat foo)
7509 sets @code{contents} to the contents of the file @file{foo}, with a space
7510 (rather than a newline) separating each line.
7513 files := $(shell echo *.c)
7517 sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is
7518 using a very strange shell, this has the same result as
7519 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7522 @node Make Control Functions, , Shell Function, Functions
7523 @section Functions That Control Make
7524 @cindex functions, for controlling make
7525 @cindex controlling make
7527 These functions control the way make runs. Generally, they are used to
7528 provide information to the user of the makefile or to cause make to stop
7529 if some sort of environmental error is detected.
7532 @item $(error @var{text}@dots{})
7534 @cindex error, stopping on
7535 @cindex stopping make
7536 Generates a fatal error where the message is @var{text}. Note that
7537 the error is generated whenever this function is evaluated. So, if
7538 you put it inside a recipe or on the right side of a recursive
7539 variable assignment, it won't be evaluated until later. The
7540 @var{text} will be expanded before the error is generated.
7546 $(error error is $(ERROR1))
7551 will generate a fatal error during the read of the makefile if the
7552 @code{make} variable @code{ERROR1} is defined. Or,
7555 ERR = $(error found an error!)
7562 will generate a fatal error while @code{make} is running, if the
7563 @code{err} target is invoked.
7565 @item $(warning @var{text}@dots{})
7567 @cindex warnings, printing
7568 @cindex printing user warnings
7569 This function works similarly to the @code{error} function, above,
7570 except that @code{make} doesn't exit. Instead, @var{text} is expanded
7571 and the resulting message is displayed, but processing of the makefile
7574 The result of the expansion of this function is the empty string.
7576 @item $(info @var{text}@dots{})
7578 @cindex printing messages
7579 This function does nothing more than print its (expanded) argument(s)
7580 to standard output. No makefile name or line number is added. The
7581 result of the expansion of this function is the empty string.
7584 @node Running, Implicit Rules, Functions, Top
7585 @chapter How to Run @code{make}
7587 A makefile that says how to recompile a program can be used in more
7588 than one way. The simplest use is to recompile every file that is out
7589 of date. Usually, makefiles are written so that if you run
7590 @code{make} with no arguments, it does just that.
7592 But you might want to update only some of the files; you might want to use
7593 a different compiler or different compiler options; you might want just to
7594 find out which files are out of date without changing them.
7596 By giving arguments when you run @code{make}, you can do any of these
7597 things and many others.
7599 @cindex exit status of make
7600 The exit status of @code{make} is always one of three values:
7603 The exit status is zero if @code{make} is successful.
7605 The exit status is two if @code{make} encounters any errors.
7606 It will print messages describing the particular errors.
7608 The exit status is one if you use the @samp{-q} flag and @code{make}
7609 determines that some target is not already up to date.
7610 @xref{Instead of Execution, ,Instead of Executing Recipes}.
7614 * Makefile Arguments:: How to specify which makefile to use.
7615 * Goals:: How to use goal arguments to specify which
7616 parts of the makefile to use.
7617 * Instead of Execution:: How to use mode flags to specify what
7618 kind of thing to do with the recipes
7619 in the makefile other than simply
7621 * Avoiding Compilation:: How to avoid recompiling certain files.
7622 * Overriding:: How to override a variable to specify
7623 an alternate compiler and other things.
7624 * Testing:: How to proceed past some errors, to
7626 * Options Summary:: Summary of Options
7629 @node Makefile Arguments, Goals, Running, Running
7630 @section Arguments to Specify the Makefile
7631 @cindex @code{--file}
7632 @cindex @code{--makefile}
7635 The way to specify the name of the makefile is with the @samp{-f} or
7636 @samp{--file} option (@samp{--makefile} also works). For example,
7637 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7639 If you use the @samp{-f} flag several times and follow each @samp{-f}
7640 with an argument, all the specified files are used jointly as
7643 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7644 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7645 that order, and use the first of these three which exists or can be made
7646 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7648 @node Goals, Instead of Execution, Makefile Arguments, Running
7649 @section Arguments to Specify the Goals
7650 @cindex goal, how to specify
7652 The @dfn{goals} are the targets that @code{make} should strive ultimately
7653 to update. Other targets are updated as well if they appear as
7654 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7656 By default, the goal is the first target in the makefile (not counting
7657 targets that start with a period). Therefore, makefiles are usually
7658 written so that the first target is for compiling the entire program or
7659 programs they describe. If the first rule in the makefile has several
7660 targets, only the first target in the rule becomes the default goal, not
7661 the whole list. You can manage the selection of the default goal from
7662 within your makefile using the @code{.DEFAULT_GOAL} variable
7663 (@pxref{Special Variables, , Other Special Variables}).
7665 You can also specify a different goal or goals with command line
7666 arguments to @code{make}. Use the name of the goal as an argument.
7667 If you specify several goals, @code{make} processes each of them in
7668 turn, in the order you name them.
7670 Any target in the makefile may be specified as a goal (unless it
7671 starts with @samp{-} or contains an @samp{=}, in which case it will be
7672 parsed as a switch or variable definition, respectively). Even
7673 targets not in the makefile may be specified, if @code{make} can find
7674 implicit rules that say how to make them.
7676 @vindex MAKECMDGOALS
7677 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
7678 list of goals you specified on the command line. If no goals were given
7679 on the command line, this variable is empty. Note that this variable
7680 should be used only in special circumstances.
7682 An example of appropriate use is to avoid including @file{.d} files
7683 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7684 @code{make} won't create them only to immediately remove them
7689 sources = foo.c bar.c
7691 ifneq ($(MAKECMDGOALS),clean)
7692 include $(sources:.c=.d)
7697 One use of specifying a goal is if you want to compile only a part of
7698 the program, or only one of several programs. Specify as a goal each
7699 file that you wish to remake. For example, consider a directory containing
7700 several programs, with a makefile that starts like this:
7704 all: size nm ld ar as
7707 If you are working on the program @code{size}, you might want to say
7708 @w{@samp{make size}} so that only the files of that program are recompiled.
7710 Another use of specifying a goal is to make files that are not normally
7711 made. For example, there may be a file of debugging output, or a
7712 version of the program that is compiled specially for testing, which has
7713 a rule in the makefile but is not a prerequisite of the default goal.
7715 Another use of specifying a goal is to run the recipe associated with
7716 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7717 Targets, ,Empty Target Files to Record Events}). Many makefiles contain
7718 a phony target named @file{clean} which deletes everything except source
7719 files. Naturally, this is done only if you request it explicitly with
7720 @w{@samp{make clean}}. Following is a list of typical phony and empty
7721 target names. @xref{Standard Targets}, for a detailed list of all the
7722 standard target names which GNU software packages use.
7726 @cindex @code{all} @r{(standard target)}
7727 Make all the top-level targets the makefile knows about.
7730 @cindex @code{clean} @r{(standard target)}
7731 Delete all files that are normally created by running @code{make}.
7734 @cindex @code{mostlyclean} @r{(standard target)}
7735 Like @samp{clean}, but may refrain from deleting a few files that people
7736 normally don't want to recompile. For example, the @samp{mostlyclean}
7737 target for GCC does not delete @file{libgcc.a}, because recompiling it
7738 is rarely necessary and takes a lot of time.
7741 @cindex @code{distclean} @r{(standard target)}
7743 @cindex @code{realclean} @r{(standard target)}
7745 @cindex @code{clobber} @r{(standard target)}
7746 Any of these targets might be defined to delete @emph{more} files than
7747 @samp{clean} does. For example, this would delete configuration files
7748 or links that you would normally create as preparation for compilation,
7749 even if the makefile itself cannot create these files.
7752 @cindex @code{install} @r{(standard target)}
7753 Copy the executable file into a directory that users typically search
7754 for commands; copy any auxiliary files that the executable uses into
7755 the directories where it will look for them.
7758 @cindex @code{print} @r{(standard target)}
7759 Print listings of the source files that have changed.
7762 @cindex @code{tar} @r{(standard target)}
7763 Create a tar file of the source files.
7766 @cindex @code{shar} @r{(standard target)}
7767 Create a shell archive (shar file) of the source files.
7770 @cindex @code{dist} @r{(standard target)}
7771 Create a distribution file of the source files. This might
7772 be a tar file, or a shar file, or a compressed version of one of the
7773 above, or even more than one of the above.
7776 @cindex @code{TAGS} @r{(standard target)}
7777 Update a tags table for this program.
7780 @cindex @code{check} @r{(standard target)}
7782 @cindex @code{test} @r{(standard target)}
7783 Perform self tests on the program this makefile builds.
7786 @node Instead of Execution, Avoiding Compilation, Goals, Running
7787 @section Instead of Executing Recipes
7788 @cindex execution, instead of
7789 @cindex recipes, instead of executing
7791 The makefile tells @code{make} how to tell whether a target is up to date,
7792 and how to update each target. But updating the targets is not always
7793 what you want. Certain options specify other activities for @code{make}.
7795 @comment Extra blank lines make it print better.
7801 @cindex @code{--just-print}
7802 @cindex @code{--dry-run}
7803 @cindex @code{--recon}
7806 ``No-op''. The activity is to print what recipe would be used to make
7807 the targets up to date, but not actually execute it. Some recipes are
7808 still executed, even with this flag (@pxref{MAKE Variable, ,How the @code{MAKE} Variable Works}).
7812 @cindex @code{--touch}
7813 @cindex touching files
7814 @cindex target, touching
7817 ``Touch''. The activity is to mark the targets as up to date without
7818 actually changing them. In other words, @code{make} pretends to compile
7819 the targets but does not really change their contents.
7823 @cindex @code{--question}
7825 @cindex question mode
7827 ``Question''. The activity is to find out silently whether the targets
7828 are up to date already; but execute no recipe in either case. In other
7829 words, neither compilation nor output will occur.
7832 @itemx --what-if=@var{file}
7833 @itemx --assume-new=@var{file}
7834 @itemx --new-file=@var{file}
7835 @cindex @code{--what-if}
7837 @cindex @code{--assume-new}
7838 @cindex @code{--new-file}
7840 @cindex files, assuming new
7842 ``What if''. Each @samp{-W} flag is followed by a file name. The given
7843 files' modification times are recorded by @code{make} as being the present
7844 time, although the actual modification times remain the same.
7845 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7846 to see what would happen if you were to modify specific files.@refill
7849 With the @samp{-n} flag, @code{make} prints the recipe that it would
7850 normally execute but usually does not execute it.
7852 With the @samp{-t} flag, @code{make} ignores the recipes in the rules
7853 and uses (in effect) the command @code{touch} for each target that needs to
7854 be remade. The @code{touch} command is also printed, unless @samp{-s} or
7855 @code{.SILENT} is used. For speed, @code{make} does not actually invoke
7856 the program @code{touch}. It does the work directly.
7858 With the @samp{-q} flag, @code{make} prints nothing and executes no
7859 recipes, but the exit status code it returns is zero if and only if the
7860 targets to be considered are already up to date. If the exit status is
7861 one, then some updating needs to be done. If @code{make} encounters an
7862 error, the exit status is two, so you can distinguish an error from a
7863 target that is not up to date.
7865 It is an error to use more than one of these three flags in the same
7866 invocation of @code{make}.
7868 @cindex +, and recipe execution
7869 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect recipe
7870 lines that begin with @samp{+} characters or contain the strings
7871 @samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing
7872 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7873 is run regardless of these options. Other lines in the same rule are
7874 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7875 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7877 @cindex phony targets and recipe execution
7878 The @samp{-t} flag prevents phony targets (@pxref{Phony Targets}) from
7879 being updated, unless there are recipe lines beginning with @samp{+}
7880 or containing @samp{$(MAKE)} or @samp{$@{MAKE@}}.
7882 The @samp{-W} flag provides two features:
7886 If you also use the @samp{-n} or @samp{-q} flag, you can see what
7887 @code{make} would do if you were to modify some files.
7890 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
7891 executing recipes, the @samp{-W} flag can direct @code{make} to act as
7892 if some files had been modified, without actually running the recipes
7893 for those files.@refill
7896 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
7897 information about @code{make} or about the makefiles in use
7898 (@pxref{Options Summary, ,Summary of Options}).@refill
7900 @node Avoiding Compilation, Overriding, Instead of Execution, Running
7901 @section Avoiding Recompilation of Some Files
7903 @cindex @code{--old-file}
7904 @cindex @code{--assume-old}
7905 @cindex files, assuming old
7906 @cindex files, avoiding recompilation of
7907 @cindex recompilation, avoiding
7909 Sometimes you may have changed a source file but you do not want to
7910 recompile all the files that depend on it. For example, suppose you add
7911 a macro or a declaration to a header file that many other files depend
7912 on. Being conservative, @code{make} assumes that any change in the
7913 header file requires recompilation of all dependent files, but you know
7914 that they do not need to be recompiled and you would rather not waste
7915 the time waiting for them to compile.
7917 If you anticipate the problem before changing the header file, you can
7918 use the @samp{-t} flag. This flag tells @code{make} not to run the
7919 recipes in the rules, but rather to mark the target up to date by
7920 changing its last-modification date. You would follow this procedure:
7924 Use the command @samp{make} to recompile the source files that really
7925 need recompilation, ensuring that the object files are up-to-date
7929 Make the changes in the header files.
7932 Use the command @samp{make -t} to mark all the object files as
7933 up to date. The next time you run @code{make}, the changes in the
7934 header files will not cause any recompilation.
7937 If you have already changed the header file at a time when some files
7938 do need recompilation, it is too late to do this. Instead, you can
7939 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
7940 ``old'' (@pxref{Options Summary, ,Summary of Options}). This means
7941 that the file itself will not be remade, and nothing else will be
7942 remade on its account. Follow this procedure:
7946 Recompile the source files that need compilation for reasons independent
7947 of the particular header file, with @samp{make -o @var{headerfile}}.
7948 If several header files are involved, use a separate @samp{-o} option
7949 for each header file.
7952 Touch all the object files with @samp{make -t}.
7955 @node Overriding, Testing, Avoiding Compilation, Running
7956 @section Overriding Variables
7957 @cindex overriding variables with arguments
7958 @cindex variables, overriding with arguments
7959 @cindex command line variables
7960 @cindex variables, command line
7962 An argument that contains @samp{=} specifies the value of a variable:
7963 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
7964 If you specify a value in this way, all ordinary assignments of the same
7965 variable in the makefile are ignored; we say they have been
7966 @dfn{overridden} by the command line argument.
7968 The most common way to use this facility is to pass extra flags to
7969 compilers. For example, in a properly written makefile, the variable
7970 @code{CFLAGS} is included in each recipe that runs the C compiler, so a
7971 file @file{foo.c} would be compiled something like this:
7974 cc -c $(CFLAGS) foo.c
7977 Thus, whatever value you set for @code{CFLAGS} affects each compilation
7978 that occurs. The makefile probably specifies the usual value for
7979 @code{CFLAGS}, like this:
7985 Each time you run @code{make}, you can override this value if you
7986 wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C
7987 compilation will be done with @samp{cc -c -g -O}. (This also
7988 illustrates how you can use quoting in the shell to enclose spaces and
7989 other special characters in the value of a variable when you override
7992 The variable @code{CFLAGS} is only one of many standard variables that
7993 exist just so that you can change them this way. @xref{Implicit
7994 Variables, , Variables Used by Implicit Rules}, for a complete list.
7996 You can also program the makefile to look at additional variables of your
7997 own, giving the user the ability to control other aspects of how the
7998 makefile works by changing the variables.
8000 When you override a variable with a command line argument, you can
8001 define either a recursively-expanded variable or a simply-expanded
8002 variable. The examples shown above make a recursively-expanded
8003 variable; to make a simply-expanded variable, write @samp{:=} instead
8004 of @samp{=}. But, unless you want to include a variable reference or
8005 function call in the @emph{value} that you specify, it makes no
8006 difference which kind of variable you create.
8008 There is one way that the makefile can change a variable that you have
8009 overridden. This is to use the @code{override} directive, which is a line
8010 that looks like this: @samp{override @var{variable} = @var{value}}
8011 (@pxref{Override Directive, ,The @code{override} Directive}).
8013 @node Testing, Options Summary, Overriding, Running
8014 @section Testing the Compilation of a Program
8015 @cindex testing compilation
8016 @cindex compilation, testing
8018 Normally, when an error happens in executing a shell command, @code{make}
8019 gives up immediately, returning a nonzero status. No further recipes are
8020 executed for any target. The error implies that the goal cannot be
8021 correctly remade, and @code{make} reports this as soon as it knows.
8023 When you are compiling a program that you have just changed, this is not
8024 what you want. Instead, you would rather that @code{make} try compiling
8025 every file that can be tried, to show you as many compilation errors
8029 @cindex @code{--keep-going}
8030 On these occasions, you should use the @samp{-k} or
8031 @samp{--keep-going} flag. This tells @code{make} to continue to
8032 consider the other prerequisites of the pending targets, remaking them
8033 if necessary, before it gives up and returns nonzero status. For
8034 example, after an error in compiling one object file, @samp{make -k}
8035 will continue compiling other object files even though it already
8036 knows that linking them will be impossible. In addition to continuing
8037 after failed shell commands, @samp{make -k} will continue as much as
8038 possible after discovering that it does not know how to make a target
8039 or prerequisite file. This will always cause an error message, but
8040 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
8041 ,Summary of Options}).@refill
8043 The usual behavior of @code{make} assumes that your purpose is to get the
8044 goals up to date; once @code{make} learns that this is impossible, it might
8045 as well report the failure immediately. The @samp{-k} flag says that the
8046 real purpose is to test as much as possible of the changes made in the
8047 program, perhaps to find several independent problems so that you can
8048 correct them all before the next attempt to compile. This is why Emacs'
8049 @kbd{M-x compile} command passes the @samp{-k} flag by default.
8051 @node Options Summary, , Testing, Running
8052 @section Summary of Options
8057 Here is a table of all the options @code{make} understands:
8064 These options are ignored for compatibility with other versions of @code{make}.
8068 @itemx --always-make
8069 @cindex @code{--always-make}
8070 Consider all targets out-of-date. GNU @code{make} proceeds to
8071 consider targets and their prerequisites using the normal algorithms;
8072 however, all targets so considered are always remade regardless of the
8073 status of their prerequisites. To avoid infinite recursion, if
8074 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
8075 Variables}) is set to a number greater than 0 this option is disabled
8076 when considering whether to remake makefiles (@pxref{Remaking
8077 Makefiles, , How Makefiles Are Remade}).
8081 @itemx --directory=@var{dir}
8082 @cindex @code{--directory}
8083 Change to directory @var{dir} before reading the makefiles. If multiple
8084 @samp{-C} options are specified, each is interpreted relative to the
8085 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
8086 This is typically used with recursive invocations of @code{make}
8087 (@pxref{Recursion, ,Recursive Use of @code{make}}).
8091 @c Extra blank line here makes the table look better.
8093 Print debugging information in addition to normal processing. The
8094 debugging information says which files are being considered for
8095 remaking, which file-times are being compared and with what results,
8096 which files actually need to be remade, which implicit rules are
8097 considered and which are applied---everything interesting about how
8098 @code{make} decides what to do. The @code{-d} option is equivalent to
8099 @samp{--debug=a} (see below).
8101 @item --debug[=@var{options}]
8102 @cindex @code{--debug}
8103 @c Extra blank line here makes the table look better.
8105 Print debugging information in addition to normal processing. Various
8106 levels and types of output can be chosen. With no arguments, print the
8107 ``basic'' level of debugging. Possible arguments are below; only the
8108 first character is considered, and values must be comma- or
8113 All types of debugging output are enabled. This is equivalent to using
8117 Basic debugging prints each target that was found to be out-of-date, and
8118 whether the build was successful or not.
8120 @item v (@i{verbose})
8121 A level above @samp{basic}; includes messages about which makefiles were
8122 parsed, prerequisites that did not need to be rebuilt, etc. This option
8123 also enables @samp{basic} messages.
8125 @item i (@i{implicit})
8126 Prints messages describing the implicit rule searches for each target.
8127 This option also enables @samp{basic} messages.
8130 Prints messages giving details on the invocation of specific subcommands.
8132 @item m (@i{makefile})
8133 By default, the above messages are not enabled while trying to remake
8134 the makefiles. This option enables messages while rebuilding makefiles,
8135 too. Note that the @samp{all} option does enable this option. This
8136 option also enables @samp{basic} messages.
8141 @itemx --environment-overrides
8142 @cindex @code{--environment-overrides}
8143 Give variables taken from the environment precedence
8144 over variables from makefiles.
8145 @xref{Environment, ,Variables from the Environment}.
8147 @item --eval=@var{string}
8148 @cindex @code{--eval}
8149 @c Extra blank line here makes the table look better.
8151 Evaluate @var{string} as makefile syntax. This is a command-line
8152 version of the @code{eval} function (@pxref{Eval Function}). The
8153 evaluation is performed after the default rules and variables have
8154 been defined, but before any makefiles are read.
8158 @itemx --file=@var{file}
8159 @cindex @code{--file}
8160 @itemx --makefile=@var{file}
8161 @cindex @code{--makefile}
8162 Read the file named @var{file} as a makefile.
8163 @xref{Makefiles, ,Writing Makefiles}.
8168 @cindex @code{--help}
8169 @c Extra blank line here makes the table look better.
8171 Remind you of the options that @code{make} understands and then exit.
8175 @itemx --ignore-errors
8176 @cindex @code{--ignore-errors}
8177 Ignore all errors in recipes executed to remake files.
8178 @xref{Errors, ,Errors in Recipes}.
8182 @itemx --include-dir=@var{dir}
8183 @cindex @code{--include-dir}
8184 Specifies a directory @var{dir} to search for included makefiles.
8185 @xref{Include, ,Including Other Makefiles}. If several @samp{-I}
8186 options are used to specify several directories, the directories are
8187 searched in the order specified.
8189 @item -j [@var{jobs}]
8191 @itemx --jobs[=@var{jobs}]
8192 @cindex @code{--jobs}
8193 Specifies the number of recipes (jobs) to run simultaneously. With no
8194 argument, @code{make} runs as many recipes simultaneously as possible.
8195 If there is more than one @samp{-j} option, the last one is effective.
8196 @xref{Parallel, ,Parallel Execution}, for more information on how
8197 recipes are run. Note that this option is ignored on MS-DOS.
8202 @cindex @code{--keep-going}
8203 Continue as much as possible after an error. While the target that
8204 failed, and those that depend on it, cannot be remade, the other
8205 prerequisites of these targets can be processed all the same.
8206 @xref{Testing, ,Testing the Compilation of a Program}.
8208 @item -l [@var{load}]
8210 @itemx --load-average[=@var{load}]
8211 @cindex @code{--load-average}
8212 @itemx --max-load[=@var{load}]
8213 @cindex @code{--max-load}
8214 Specifies that no new recipes should be started if there are other
8215 recipes running and the load average is at least @var{load} (a
8216 floating-point number). With no argument, removes a previous load
8217 limit. @xref{Parallel, ,Parallel Execution}.
8221 @itemx --check-symlink-times
8222 @cindex @code{--check-symlink-times}
8223 On systems that support symbolic links, this option causes @code{make}
8224 to consider the timestamps on any symbolic links in addition to the
8225 timestamp on the file referenced by those links. When this option is
8226 provided, the most recent timestamp among the file and the symbolic
8227 links is taken as the modification time for this target file.
8232 @cindex @code{--just-print}
8234 @cindex @code{--dry-run}
8236 @cindex @code{--recon}
8237 @c Extra blank line here makes the table look better.
8239 Print the recipe that would be executed, but do not execute it (except
8240 in certain circumstances).
8241 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8245 @itemx --old-file=@var{file}
8246 @cindex @code{--old-file}
8247 @itemx --assume-old=@var{file}
8248 @cindex @code{--assume-old}
8249 Do not remake the file @var{file} even if it is older than its
8250 prerequisites, and do not remake anything on account of changes in
8251 @var{file}. Essentially the file is treated as very old and its rules
8252 are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of
8257 @itemx --print-data-base
8258 @cindex @code{--print-data-base}
8259 @cindex data base of @code{make} rules
8260 @cindex predefined rules and variables, printing
8261 Print the data base (rules and variable values) that results from
8262 reading the makefiles; then execute as usual or as otherwise
8263 specified. This also prints the version information given by the
8264 @samp{-v} switch (see below). To print the data base without trying
8265 to remake any files, use @w{@samp{make -qp}}. To print the data base
8266 of predefined rules and variables, use @w{@samp{make -p -f /dev/null}}.
8267 The data base output contains filename and linenumber information for
8268 recipe and variable definitions, so it can be a useful debugging tool
8269 in complex environments.
8274 @cindex @code{--question}
8275 ``Question mode''. Do not run any recipes, or print anything; just
8276 return an exit status that is zero if the specified targets are already
8277 up to date, one if any remaking is required, or two if an error is
8278 encountered. @xref{Instead of Execution, ,Instead of Executing
8283 @itemx --no-builtin-rules
8284 @cindex @code{--no-builtin-rules}
8285 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8286 ,Using Implicit Rules}). You can still define your own by writing
8287 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8288 Rules}). The @samp{-r} option also clears out the default list of
8289 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8290 Rules}). But you can still define your own suffixes with a rule for
8291 @code{.SUFFIXES}, and then define your own suffix rules. Note that only
8292 @emph{rules} are affected by the @code{-r} option; default variables
8293 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8294 Rules}); see the @samp{-R} option below.
8298 @itemx --no-builtin-variables
8299 @cindex @code{--no-builtin-variables}
8300 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8301 Variables, ,Variables Used by Implicit Rules}). You can still define
8302 your own, of course. The @samp{-R} option also automatically enables
8303 the @samp{-r} option (see above), since it doesn't make sense to have
8304 implicit rules without any definitions for the variables that they use.
8309 @cindex @code{--silent}
8311 @cindex @code{--quiet}
8312 @c Extra blank line here makes the table look better.
8314 Silent operation; do not print the recipes as they are executed.
8315 @xref{Echoing, ,Recipe Echoing}.
8319 @itemx --no-keep-going
8320 @cindex @code{--no-keep-going}
8322 @cindex @code{--stop}
8323 @c Extra blank line here makes the table look better.
8325 Cancel the effect of the @samp{-k} option. This is never necessary
8326 except in a recursive @code{make} where @samp{-k} might be inherited
8327 from the top-level @code{make} via @code{MAKEFLAGS}
8328 (@pxref{Recursion, ,Recursive Use of @code{make}})
8329 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8334 @cindex @code{--touch}
8335 @c Extra blank line here makes the table look better.
8337 Touch files (mark them up to date without really changing them)
8338 instead of running their recipes. This is used to pretend that the
8339 recipes were done, in order to fool future invocations of
8340 @code{make}. @xref{Instead of Execution, ,Instead of Executing Recipes}.
8345 @cindex @code{--version}
8346 Print the version of the @code{make} program plus a copyright, a list
8347 of authors, and a notice that there is no warranty; then exit.
8351 @itemx --print-directory
8352 @cindex @code{--print-directory}
8353 Print a message containing the working directory both before and after
8354 executing the makefile. This may be useful for tracking down errors
8355 from complicated nests of recursive @code{make} commands.
8356 @xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you
8357 rarely need to specify this option since @samp{make} does it for you;
8358 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8360 @itemx --no-print-directory
8361 @cindex @code{--no-print-directory}
8362 Disable printing of the working directory under @code{-w}.
8363 This option is useful when @code{-w} is turned on automatically,
8364 but you do not want to see the extra messages.
8365 @xref{-w Option, ,The @samp{--print-directory} Option}.
8369 @itemx --what-if=@var{file}
8370 @cindex @code{--what-if}
8371 @itemx --new-file=@var{file}
8372 @cindex @code{--new-file}
8373 @itemx --assume-new=@var{file}
8374 @cindex @code{--assume-new}
8375 Pretend that the target @var{file} has just been modified. When used
8376 with the @samp{-n} flag, this shows you what would happen if you were
8377 to modify that file. Without @samp{-n}, it is almost the same as
8378 running a @code{touch} command on the given file before running
8379 @code{make}, except that the modification time is changed only in the
8380 imagination of @code{make}.
8381 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8383 @item --warn-undefined-variables
8384 @cindex @code{--warn-undefined-variables}
8385 @cindex variables, warning for undefined
8386 @cindex undefined variables, warning message
8387 Issue a warning message whenever @code{make} sees a reference to an
8388 undefined variable. This can be helpful when you are trying to debug
8389 makefiles which use variables in complex ways.
8392 @node Implicit Rules, Archives, Running, Top
8393 @chapter Using Implicit Rules
8394 @cindex implicit rule
8395 @cindex rule, implicit
8397 Certain standard ways of remaking target files are used very often. For
8398 example, one customary way to make an object file is from a C source file
8399 using the C compiler, @code{cc}.
8401 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8402 that you do not have to specify them in detail when you want to use
8403 them. For example, there is an implicit rule for C compilation. File
8404 names determine which implicit rules are run. For example, C
8405 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8406 So @code{make} applies the implicit rule for C compilation when it sees
8407 this combination of file name endings.@refill
8409 A chain of implicit rules can apply in sequence; for example, @code{make}
8410 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8412 @xref{Chained Rules, ,Chains of Implicit Rules}.
8415 The built-in implicit rules use several variables in their recipes so
8416 that, by changing the values of the variables, you can change the way the
8417 implicit rule works. For example, the variable @code{CFLAGS} controls the
8418 flags given to the C compiler by the implicit rule for C compilation.
8420 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8423 You can define your own implicit rules by writing @dfn{pattern rules}.
8425 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8428 @dfn{Suffix rules} are a more limited way to define implicit rules.
8429 Pattern rules are more general and clearer, but suffix rules are
8430 retained for compatibility.
8432 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8436 * Using Implicit:: How to use an existing implicit rule
8437 to get the recipes for updating a file.
8438 * Catalogue of Rules:: A list of built-in implicit rules.
8439 * Implicit Variables:: How to change what predefined rules do.
8440 * Chained Rules:: How to use a chain of implicit rules.
8441 * Pattern Rules:: How to define new implicit rules.
8442 * Last Resort:: How to define recipes for rules which
8444 * Suffix Rules:: The old-fashioned style of implicit rule.
8445 * Implicit Rule Search:: The precise algorithm for applying
8449 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8450 @section Using Implicit Rules
8451 @cindex implicit rule, how to use
8452 @cindex rule, implicit, how to use
8454 To allow @code{make} to find a customary method for updating a target
8455 file, all you have to do is refrain from specifying recipes yourself.
8456 Either write a rule with no recipe, or don't write a rule at all.
8457 Then @code{make} will figure out which implicit rule to use based on
8458 which kind of source file exists or can be made.
8460 For example, suppose the makefile looks like this:
8464 cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8468 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8469 will automatically look for an implicit rule that tells how to update it.
8470 This happens whether or not the file @file{foo.o} currently exists.
8472 If an implicit rule is found, it can supply both a recipe and one or
8473 more prerequisites (the source files). You would want to write a rule
8474 for @file{foo.o} with no recipe if you need to specify additional
8475 prerequisites, such as header files, that the implicit rule cannot
8478 Each implicit rule has a target pattern and prerequisite patterns. There may
8479 be many implicit rules with the same target pattern. For example, numerous
8480 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8481 another, from a @samp{.p} file with the Pascal compiler; and so on. The rule
8482 that actually applies is the one whose prerequisites exist or can be made.
8483 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8484 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8485 compiler; and so on.
8487 Of course, when you write the makefile, you know which implicit rule you
8488 want @code{make} to use, and you know it will choose that one because you
8489 know which possible prerequisite files are supposed to exist.
8490 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8491 for a catalogue of all the predefined implicit rules.
8493 Above, we said an implicit rule applies if the required prerequisites ``exist
8494 or can be made''. A file ``can be made'' if it is mentioned explicitly in
8495 the makefile as a target or a prerequisite, or if an implicit rule can be
8496 recursively found for how to make it. When an implicit prerequisite is the
8497 result of another implicit rule, we say that @dfn{chaining} is occurring.
8498 @xref{Chained Rules, ,Chains of Implicit Rules}.
8500 In general, @code{make} searches for an implicit rule for each target, and
8501 for each double-colon rule, that has no recipe. A file that is mentioned
8502 only as a prerequisite is considered a target whose rule specifies nothing,
8503 so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8504 details of how the search is done.
8506 Note that explicit prerequisites do not influence implicit rule search.
8507 For example, consider this explicit rule:
8514 The prerequisite on @file{foo.p} does not necessarily mean that
8515 @code{make} will remake @file{foo.o} according to the implicit rule to
8516 make an object file, a @file{.o} file, from a Pascal source file, a
8517 @file{.p} file. For example, if @file{foo.c} also exists, the implicit
8518 rule to make an object file from a C source file is used instead,
8519 because it appears before the Pascal rule in the list of predefined
8520 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8523 If you do not want an implicit rule to be used for a target that has no
8524 recipe, you can give that target an empty recipe by writing a semicolon
8525 (@pxref{Empty Recipes, ,Defining Empty Recipes}).
8527 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8528 @section Catalogue of Implicit Rules
8529 @cindex implicit rule, predefined
8530 @cindex rule, implicit, predefined
8532 Here is a catalogue of predefined implicit rules which are always
8533 available unless the makefile explicitly overrides or cancels them.
8534 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8535 canceling or overriding an implicit rule. The @samp{-r} or
8536 @samp{--no-builtin-rules} option cancels all predefined rules.
8538 This manual only documents the default rules available on POSIX-based
8539 operating systems. Other operating systems, such as VMS, Windows,
8540 OS/2, etc. may have different sets of default rules. To see the full
8541 list of default rules and variables available in your version of GNU
8542 @code{make}, run @samp{make -p} in a directory with no makefile.
8544 Not all of these rules will always be defined, even when the @samp{-r}
8545 option is not given. Many of the predefined implicit rules are
8546 implemented in @code{make} as suffix rules, so which ones will be
8547 defined depends on the @dfn{suffix list} (the list of prerequisites of
8548 the special target @code{.SUFFIXES}). The default suffix list is:
8549 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8550 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.m},
8551 @code{.r}, @code{.y}, @code{.l}, @code{.ym}, @code{.lm}, @code{.s},
8552 @code{.S}, @code{.mod}, @code{.sym}, @code{.def}, @code{.h},
8553 @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo}, @code{.texi},
8554 @code{.txinfo}, @code{.w}, @code{.ch} @code{.web}, @code{.sh},
8555 @code{.elc}, @code{.el}. All of the implicit rules described below
8556 whose prerequisites have one of these suffixes are actually suffix
8557 rules. If you modify the suffix list, the only predefined suffix
8558 rules in effect will be those named by one or two of the suffixes that
8559 are on the list you specify; rules whose suffixes fail to be on the
8560 list are disabled. @xref{Suffix Rules, ,Old-Fashioned Suffix Rules},
8561 for full details on suffix rules.
8564 @item Compiling C programs
8565 @cindex C, rule to compile
8570 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8571 a recipe of the form @samp{$(CC) $(CPPFLAGS) $(CFLAGS) -c}.@refill
8573 @item Compiling C++ programs
8574 @cindex C++, rule to compile
8579 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8580 @file{@var{n}.cpp}, or @file{@var{n}.C} with a recipe of the form
8581 @samp{$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c}. We encourage you to use the
8582 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8584 @item Compiling Pascal programs
8585 @cindex Pascal, rule to compile
8588 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8589 with the recipe @samp{$(PC) $(PFLAGS) -c}.@refill
8591 @item Compiling Fortran and Ratfor programs
8592 @cindex Fortran, rule to compile
8593 @cindex Ratfor, rule to compile
8598 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8599 @file{@var{n}.F} or @file{@var{n}.f} by running the
8600 Fortran compiler. The precise recipe used is as follows:@refill
8604 @samp{$(FC) $(FFLAGS) -c}.
8606 @samp{$(FC) $(FFLAGS) $(CPPFLAGS) -c}.
8608 @samp{$(FC) $(FFLAGS) $(RFLAGS) -c}.
8611 @item Preprocessing Fortran and Ratfor programs
8612 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8613 @file{@var{n}.F}. This rule runs just the preprocessor to convert a
8614 Ratfor or preprocessable Fortran program into a strict Fortran
8615 program. The precise recipe used is as follows:@refill
8619 @samp{$(FC) $(CPPFLAGS) $(FFLAGS) -F}.
8621 @samp{$(FC) $(FFLAGS) $(RFLAGS) -F}.
8624 @item Compiling Modula-2 programs
8625 @cindex Modula-2, rule to compile
8630 @file{@var{n}.sym} is made from @file{@var{n}.def} with a recipe
8631 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o}
8632 is made from @file{@var{n}.mod}; the form is:
8633 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8636 @item Assembling and preprocessing assembler programs
8637 @cindex assembly, rule to compile
8640 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8641 running the assembler, @code{as}. The precise recipe is
8642 @samp{$(AS) $(ASFLAGS)}.@refill
8645 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8646 running the C preprocessor, @code{cpp}. The precise recipe is
8647 @w{@samp{$(CPP) $(CPPFLAGS)}}.
8649 @item Linking a single object file
8650 @cindex linking, predefined rule for
8653 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
8654 the linker (usually called @code{ld}) via the C compiler. The precise
8655 recipe used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8657 This rule does the right thing for a simple program with only one
8658 source file. It will also do the right thing if there are multiple
8659 object files (presumably coming from various other source files), one
8660 of which has a name matching that of the executable file. Thus,
8667 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8682 In more complicated cases, such as when there is no object file whose
8683 name derives from the executable file name, you must write an explicit
8686 Each kind of file automatically made into @samp{.o} object files will
8687 be automatically linked by using the compiler (@samp{$(CC)},
8688 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8689 assemble @samp{.s} files) without the @samp{-c} option. This could be
8690 done by using the @samp{.o} object files as intermediates, but it is
8691 faster to do the compiling and linking in one step, so that's how it's
8694 @item Yacc for C programs
8696 @cindex Yacc, rule to run
8698 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8699 running Yacc with the recipe @samp{$(YACC) $(YFLAGS)}.
8701 @item Lex for C programs
8703 @cindex Lex, rule to run
8705 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8706 running Lex. The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8708 @item Lex for Ratfor programs
8709 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8710 running Lex. The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8712 The convention of using the same suffix @samp{.l} for all Lex files
8713 regardless of whether they produce C code or Ratfor code makes it
8714 impossible for @code{make} to determine automatically which of the two
8715 languages you are using in any particular case. If @code{make} is
8716 called upon to remake an object file from a @samp{.l} file, it must
8717 guess which compiler to use. It will guess the C compiler, because
8718 that is more common. If you are using Ratfor, make sure @code{make}
8719 knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you
8720 are using Ratfor exclusively, with no C files, remove @samp{.c} from
8721 the list of implicit rule suffixes with:@refill
8726 .SUFFIXES: .o .r .f .l @dots{}
8730 @item Making Lint Libraries from C, Yacc, or Lex programs
8732 @cindex @code{lint}, rule to run
8734 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8735 The precise recipe is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8736 The same recipe is used on the C code produced from
8737 @file{@var{n}.y} or @file{@var{n}.l}.@refill
8739 @item @TeX{} and Web
8740 @cindex @TeX{}, rule to run
8741 @cindex Web, rule to run
8752 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the recipe
8753 @samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with
8754 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8755 it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is
8756 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8757 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8758 or can be made) with @samp{$(CTANGLE)}.@refill
8760 @item Texinfo and Info
8761 @cindex Texinfo, rule to format
8762 @cindex Info, rule to format
8769 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8770 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the recipe
8771 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from
8772 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8773 the recipe @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8776 @cindex RCS, rule to extract from
8778 @pindex ,v @r{(RCS file extension)}
8779 Any file @file{@var{n}} is extracted if necessary from an RCS file
8780 named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise
8781 recipe used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be
8782 extracted from RCS if it already exists, even if the RCS file is
8783 newer. The rules for RCS are terminal
8784 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8785 so RCS files cannot be generated from another source; they must
8786 actually exist.@refill
8789 @cindex SCCS, rule to extract from
8791 @pindex s. @r{(SCCS file prefix)}
8792 Any file @file{@var{n}} is extracted if necessary from an SCCS file
8793 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise
8794 recipe used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are
8795 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8796 so SCCS files cannot be generated from another source; they must
8797 actually exist.@refill
8800 For the benefit of SCCS, a file @file{@var{n}} is copied from
8801 @file{@var{n}.sh} and made executable (by everyone). This is for
8802 shell scripts that are checked into SCCS. Since RCS preserves the
8803 execution permission of a file, you do not need to use this feature
8806 We recommend that you avoid using of SCCS. RCS is widely held to be
8807 superior, and is also free. By choosing free software in place of
8808 comparable (or inferior) proprietary software, you support the free
8812 Usually, you want to change only the variables listed in the table
8813 above, which are documented in the following section.
8815 However, the recipes in built-in implicit rules actually use
8816 variables such as @code{COMPILE.c}, @code{LINK.p}, and
8817 @code{PREPROCESS.S}, whose values contain the recipes listed above.
8819 @code{make} follows the convention that the rule to compile a
8820 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8821 Similarly, the rule to produce an executable from a @file{.@var{x}}
8822 file uses @code{LINK.@var{x}}; and the rule to preprocess a
8823 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8825 @vindex OUTPUT_OPTION
8826 Every rule that produces an object file uses the variable
8827 @code{OUTPUT_OPTION}. @code{make} defines this variable either to
8828 contain @samp{-o $@@}, or to be empty, depending on a compile-time
8829 option. You need the @samp{-o} option to ensure that the output goes
8830 into the right file when the source file is in a different directory,
8831 as when using @code{VPATH} (@pxref{Directory Search}). However,
8832 compilers on some systems do not accept a @samp{-o} switch for object
8833 files. If you use such a system, and use @code{VPATH}, some
8834 compilations will put their output in the wrong place.
8835 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8836 the value @w{@samp{; mv $*.o $@@}}.
8838 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8839 @section Variables Used by Implicit Rules
8840 @cindex flags for compilers
8842 The recipes in built-in implicit rules make liberal use of certain
8843 predefined variables. You can alter the values of these variables in
8844 the makefile, with arguments to @code{make}, or in the environment to
8845 alter how the implicit rules work without redefining the rules
8846 themselves. You can cancel all variables used by implicit rules with
8847 the @samp{-R} or @samp{--no-builtin-variables} option.
8849 For example, the recipe used to compile a C source file actually says
8850 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables
8851 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By
8852 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8853 used for all C compilations performed by the implicit rule. By redefining
8854 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8855 each compilation. @emph{All} implicit rules that do C compilation use
8856 @samp{$(CC)} to get the program name for the compiler and @emph{all}
8857 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8859 The variables used in implicit rules fall into two classes: those that are
8860 names of programs (like @code{CC}) and those that contain arguments for the
8861 programs (like @code{CFLAGS}). (The ``name of a program'' may also contain
8862 some command arguments, but it must start with an actual executable program
8863 name.) If a variable value contains more than one argument, separate them
8866 The following tables describe of some of the more commonly-used predefined
8867 variables. This list is not exhaustive, and the default values shown here may
8868 not be what @code{make} selects for your environment. To see the
8869 complete list of predefined variables for your instance of GNU @code{make} you
8870 can run @samp{make -p} in a directory with no makefiles.
8872 Here is a table of some of the more common variables used as names of
8873 programs in built-in rules:
8879 Archive-maintaining program; default @samp{ar}.
8884 Program for compiling assembly files; default @samp{as}.
8889 Program for compiling C programs; default @samp{cc}.
8894 Program for compiling C++ programs; default @samp{g++}.
8899 Program for running the C preprocessor, with results to standard output;
8900 default @samp{$(CC) -E}.
8904 Program for compiling or preprocessing Fortran and Ratfor programs;
8910 Program to use to compile Modula-2 source code; default @samp{m2c}.
8915 Program for compiling Pascal programs; default @samp{pc}.
8920 Program for extracting a file from RCS; default @samp{co}.
8925 Program for extracting a file from SCCS; default @samp{get}.
8930 Program to use to turn Lex grammars into source code; default @samp{lex}.
8935 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
8940 Program to use to run lint on source code; default @samp{lint}.
8945 Program to convert a Texinfo source file into an Info file; default
8951 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
8957 Program to make @TeX{} @sc{dvi} files from Texinfo source;
8958 default @samp{texi2dvi}.
8963 Program to translate Web into @TeX{}; default @samp{weave}.
8968 Program to translate C Web into @TeX{}; default @samp{cweave}.
8973 Program to translate Web into Pascal; default @samp{tangle}.
8978 Program to translate C Web into C; default @samp{ctangle}.
8983 Command to remove a file; default @samp{rm -f}.
8987 Here is a table of variables whose values are additional arguments for the
8988 programs above. The default values for all of these is the empty
8989 string, unless otherwise noted.
8994 Flags to give the archive-maintaining program; default @samp{rv}.
8998 Extra flags to give to the assembler (when explicitly
8999 invoked on a @samp{.s} or @samp{.S} file).
9003 Extra flags to give to the C compiler.
9007 Extra flags to give to the C++ compiler.
9011 Extra flags to give to the RCS @code{co} program.
9015 Extra flags to give to the C preprocessor and programs
9016 that use it (the C and Fortran compilers).
9020 Extra flags to give to the Fortran compiler.
9024 Extra flags to give to the SCCS @code{get} program.
9028 Extra flags to give to compilers when they are supposed to invoke the linker,
9033 Extra flags to give to Lex.
9037 Extra flags to give to Yacc.
9041 Extra flags to give to the Pascal compiler.
9045 Extra flags to give to the Fortran compiler for Ratfor programs.
9049 Extra flags to give to lint.
9052 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
9053 @section Chains of Implicit Rules
9055 @cindex chains of rules
9056 @cindex rule, implicit, chains of
9057 Sometimes a file can be made by a sequence of implicit rules. For example,
9058 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
9059 first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}.
9061 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
9062 special searching is required: @code{make} finds that the object file can
9063 be made by C compilation from @file{@var{n}.c}; later on, when considering
9064 how to make @file{@var{n}.c}, the rule for running Yacc is
9065 used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
9068 @cindex intermediate files
9069 @cindex files, intermediate
9070 However, even if @file{@var{n}.c} does not exist and is not mentioned,
9071 @code{make} knows how to envision it as the missing link between
9072 @file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is
9073 called an @dfn{intermediate file}. Once @code{make} has decided to use the
9074 intermediate file, it is entered in the data base as if it had been
9075 mentioned in the makefile, along with the implicit rule that says how to
9078 Intermediate files are remade using their rules just like all other
9079 files. But intermediate files are treated differently in two ways.
9081 The first difference is what happens if the intermediate file does not
9082 exist. If an ordinary file @var{b} does not exist, and @code{make}
9083 considers a target that depends on @var{b}, it invariably creates
9084 @var{b} and then updates the target from @var{b}. But if @var{b} is an
9085 intermediate file, then @code{make} can leave well enough alone. It
9086 won't bother updating @var{b}, or the ultimate target, unless some
9087 prerequisite of @var{b} is newer than that target or there is some other
9088 reason to update that target.
9090 The second difference is that if @code{make} @emph{does} create @var{b}
9091 in order to update something else, it deletes @var{b} later on after it
9092 is no longer needed. Therefore, an intermediate file which did not
9093 exist before @code{make} also does not exist after @code{make}.
9094 @code{make} reports the deletion to you by printing a @samp{rm -f}
9095 command showing which file it is deleting.
9097 Ordinarily, a file cannot be intermediate if it is mentioned in the
9098 makefile as a target or prerequisite. However, you can explicitly mark a
9099 file as intermediate by listing it as a prerequisite of the special target
9100 @code{.INTERMEDIATE}. This takes effect even if the file is mentioned
9101 explicitly in some other way.
9103 @cindex intermediate files, preserving
9104 @cindex preserving intermediate files
9105 @cindex secondary files
9106 You can prevent automatic deletion of an intermediate file by marking it
9107 as a @dfn{secondary} file. To do this, list it as a prerequisite of the
9108 special target @code{.SECONDARY}. When a file is secondary, @code{make}
9109 will not create the file merely because it does not already exist, but
9110 @code{make} does not automatically delete the file. Marking a file as
9111 secondary also marks it as intermediate.
9113 You can list the target pattern of an implicit rule (such as @samp{%.o})
9114 as a prerequisite of the special target @code{.PRECIOUS} to preserve
9115 intermediate files made by implicit rules whose target patterns match
9116 that file's name; see @ref{Interrupts}.@refill
9117 @cindex preserving with @code{.PRECIOUS}
9118 @cindex @code{.PRECIOUS} intermediate files
9120 A chain can involve more than two implicit rules. For example, it is
9121 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
9122 Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are
9123 intermediate files that are deleted at the end.@refill
9125 No single implicit rule can appear more than once in a chain. This means
9126 that @code{make} will not even consider such a ridiculous thing as making
9127 @file{foo} from @file{foo.o.o} by running the linker twice. This
9128 constraint has the added benefit of preventing any infinite loop in the
9129 search for an implicit rule chain.
9131 There are some special implicit rules to optimize certain cases that would
9132 otherwise be handled by rule chains. For example, making @file{foo} from
9133 @file{foo.c} could be handled by compiling and linking with separate
9134 chained rules, using @file{foo.o} as an intermediate file. But what
9135 actually happens is that a special rule for this case does the compilation
9136 and linking with a single @code{cc} command. The optimized rule is used in
9137 preference to the step-by-step chain because it comes earlier in the
9140 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
9141 @section Defining and Redefining Pattern Rules
9143 You define an implicit rule by writing a @dfn{pattern rule}. A pattern
9144 rule looks like an ordinary rule, except that its target contains the
9145 character @samp{%} (exactly one of them). The target is considered a
9146 pattern for matching file names; the @samp{%} can match any nonempty
9147 substring, while other characters match only themselves. The prerequisites
9148 likewise use @samp{%} to show how their names relate to the target name.
9150 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
9151 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
9153 Note that expansion using @samp{%} in pattern rules occurs
9154 @strong{after} any variable or function expansions, which take place
9155 when the makefile is read. @xref{Using Variables, , How to Use
9156 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
9159 * Pattern Intro:: An introduction to pattern rules.
9160 * Pattern Examples:: Examples of pattern rules.
9161 * Automatic Variables:: How to use automatic variables in the
9162 recipes of implicit rules.
9163 * Pattern Match:: How patterns match.
9164 * Match-Anything Rules:: Precautions you should take prior to
9165 defining rules that can match any
9166 target file whatever.
9167 * Canceling Rules:: How to override or cancel built-in rules.
9170 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
9171 @subsection Introduction to Pattern Rules
9172 @cindex pattern rule
9173 @cindex rule, pattern
9175 A pattern rule contains the character @samp{%} (exactly one of them)
9176 in the target; otherwise, it looks exactly like an ordinary rule. The
9177 target is a pattern for matching file names; the @samp{%} matches any
9178 nonempty substring, while other characters match only themselves.
9179 @cindex target pattern, implicit
9180 @cindex @code{%}, in pattern rules
9182 For example, @samp{%.c} as a pattern matches any file name that ends in
9183 @samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts
9184 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9185 (There must be at least one character to match the @samp{%}.) The substring
9186 that the @samp{%} matches is called the @dfn{stem}.@refill
9188 @samp{%} in a prerequisite of a pattern rule stands for the same stem
9189 that was matched by the @samp{%} in the target. In order for the
9190 pattern rule to apply, its target pattern must match the file name
9191 under consideration and all of its prerequisites (after pattern
9192 substitution) must name files that exist or can be made. These files
9193 become prerequisites of the target.
9194 @cindex prerequisite pattern, implicit
9196 Thus, a rule of the form
9199 %.o : %.c ; @var{recipe}@dots{}
9203 specifies how to make a file @file{@var{n}.o}, with another file
9204 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9205 exists or can be made.
9207 There may also be prerequisites that do not use @samp{%}; such a prerequisite
9208 attaches to every file made by this pattern rule. These unvarying
9209 prerequisites are useful occasionally.
9211 A pattern rule need not have any prerequisites that contain @samp{%}, or
9212 in fact any prerequisites at all. Such a rule is effectively a general
9213 wildcard. It provides a way to make any file that matches the target
9214 pattern. @xref{Last Resort}.
9216 More than one pattern rule may match a target. In this case
9217 @code{make} will choose the ``best fit'' rule. @xref{Pattern Match,
9218 ,How Patterns Match}.
9220 @c !!! The end of of this paragraph should be rewritten. --bob
9221 Pattern rules may have more than one target. Unlike normal rules,
9222 this does not act as many different rules with the same prerequisites
9223 and recipe. If a pattern rule has multiple targets, @code{make} knows
9224 that the rule's recipe is responsible for making all of the targets.
9225 The recipe is executed only once to make all the targets. When
9226 searching for a pattern rule to match a target, the target patterns of
9227 a rule other than the one that matches the target in need of a rule
9228 are incidental: @code{make} worries only about giving a recipe and
9229 prerequisites to the file presently in question. However, when this
9230 file's recipe is run, the other targets are marked as having been
9232 @cindex multiple targets, in pattern rule
9233 @cindex target, multiple in pattern rule
9235 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9236 @subsection Pattern Rule Examples
9238 Here are some examples of pattern rules actually predefined in
9239 @code{make}. First, the rule that compiles @samp{.c} files into @samp{.o}
9244 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9248 defines a rule that can make any file @file{@var{x}.o} from
9249 @file{@var{x}.c}. The recipe uses the automatic variables @samp{$@@} and
9250 @samp{$<} to substitute the names of the target file and the source file
9251 in each case where the rule applies (@pxref{Automatic Variables}).@refill
9253 Here is a second built-in rule:
9261 defines a rule that can make any file @file{@var{x}} whatsoever from a
9262 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since
9263 the target is @samp{%}, this rule will apply to any file whatever, provided
9264 the appropriate prerequisite file exists. The double colon makes the rule
9265 @dfn{terminal}, which means that its prerequisite may not be an intermediate
9266 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9269 This pattern rule has two targets:
9273 %.tab.c %.tab.h: %.y
9279 @c The following paragraph is rewritten to avoid overfull hboxes
9280 This tells @code{make} that the recipe @samp{bison -d @var{x}.y} will
9281 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file
9282 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9283 and the file @file{scan.o} depends on the file @file{parse.tab.h},
9284 when @file{parse.y} is changed, the recipe @samp{bison -d parse.y}
9285 will be executed only once, and the prerequisites of both
9286 @file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably
9287 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9288 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9289 linked from @file{parse.tab.o}, @file{scan.o}, and its other
9290 prerequisites, and it will execute happily ever after.)@refill
9292 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9293 @subsection Automatic Variables
9294 @cindex automatic variables
9295 @cindex variables, automatic
9296 @cindex variables, and implicit rule
9298 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9299 @samp{.o} file: how do you write the @samp{cc} command so that it operates
9300 on the right source file name? You cannot write the name in the recipe,
9301 because the name is different each time the implicit rule is applied.
9303 What you do is use a special feature of @code{make}, the @dfn{automatic
9304 variables}. These variables have values computed afresh for each rule that
9305 is executed, based on the target and prerequisites of the rule. In this
9306 example, you would use @samp{$@@} for the object file name and @samp{$<}
9307 for the source file name.
9309 @cindex automatic variables in prerequisites
9310 @cindex prerequisites, and automatic variables
9311 It's very important that you recognize the limited scope in which
9312 automatic variable values are available: they only have values within
9313 the recipe. In particular, you cannot use them anywhere
9314 within the target list of a rule; they have no value there and will
9315 expand to the empty string. Also, they cannot be accessed directly
9316 within the prerequisite list of a rule. A common mistake is
9317 attempting to use @code{$@@} within the prerequisites list; this will
9318 not work. However, there is a special feature of GNU @code{make},
9319 secondary expansion (@pxref{Secondary Expansion}), which will allow
9320 automatic variable values to be used in prerequisite lists.
9322 Here is a table of automatic variables:
9326 @vindex @@ @r{(automatic variable)}
9328 The file name of the target of the rule. If the target is an archive
9329 member, then @samp{$@@} is the name of the archive file. In a pattern
9330 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9331 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9332 rule's recipe to be run.
9335 @vindex % @r{(automatic variable)}
9337 The target member name, when the target is an archive member.
9338 @xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then
9339 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is
9340 empty when the target is not an archive member.
9343 @vindex < @r{(automatic variable)}
9345 The name of the first prerequisite. If the target got its recipe from
9346 an implicit rule, this will be the first prerequisite added by the
9347 implicit rule (@pxref{Implicit Rules}).
9350 @vindex ? @r{(automatic variable)}
9352 The names of all the prerequisites that are newer than the target, with
9353 spaces between them. For prerequisites which are archive members, only
9354 the named member is used (@pxref{Archives}).
9355 @cindex prerequisites, list of changed
9356 @cindex list of changed prerequisites
9359 @vindex ^ @r{(automatic variable)}
9361 The names of all the prerequisites, with spaces between them. For
9362 prerequisites which are archive members, only the named member is used
9363 (@pxref{Archives}). A target has only one prerequisite on each other file
9364 it depends on, no matter how many times each file is listed as a
9365 prerequisite. So if you list a prerequisite more than once for a target,
9366 the value of @code{$^} contains just one copy of the name. This list
9367 does @strong{not} contain any of the order-only prerequisites; for those
9368 see the @samp{$|} variable, below.
9369 @cindex prerequisites, list of all
9370 @cindex list of all prerequisites
9373 @vindex + @r{(automatic variable)}
9375 This is like @samp{$^}, but prerequisites listed more than once are
9376 duplicated in the order they were listed in the makefile. This is
9377 primarily useful for use in linking commands where it is meaningful to
9378 repeat library file names in a particular order.
9381 @vindex | @r{(automatic variable)}
9383 The names of all the order-only prerequisites, with spaces between
9387 @vindex * @r{(automatic variable)}
9389 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9390 Patterns Match}). If the target is @file{dir/a.foo.b} and the target
9391 pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is
9392 useful for constructing names of related files.@refill
9393 @cindex stem, variable for
9395 In a static pattern rule, the stem is part of the file name that matched
9396 the @samp{%} in the target pattern.
9398 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9399 in that way. Instead, if the target name ends with a recognized suffix
9400 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9401 the target name minus the suffix. For example, if the target name is
9402 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9403 suffix. GNU @code{make} does this bizarre thing only for compatibility
9404 with other implementations of @code{make}. You should generally avoid
9405 using @samp{$*} except in implicit rules or static pattern rules.@refill
9407 If the target name in an explicit rule does not end with a recognized
9408 suffix, @samp{$*} is set to the empty string for that rule.
9411 @samp{$?} is useful even in explicit rules when you wish to operate on only
9412 the prerequisites that have changed. For example, suppose that an archive
9413 named @file{lib} is supposed to contain copies of several object files.
9414 This rule copies just the changed object files into the archive:
9418 lib: foo.o bar.o lose.o win.o
9423 Of the variables listed above, four have values that are single file
9424 names, and three have values that are lists of file names. These seven
9425 have variants that get just the file's directory name or just the file
9426 name within the directory. The variant variables' names are formed by
9427 appending @samp{D} or @samp{F}, respectively. These variants are
9428 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9429 @code{notdir} can be used to get a similar effect (@pxref{File Name
9430 Functions, , Functions for File Names}). Note, however, that the
9431 @samp{D} variants all omit the trailing slash which always appears in
9432 the output of the @code{dir} function. Here is a table of the variants:
9436 @vindex @@D @r{(automatic variable)}
9438 The directory part of the file name of the target, with the trailing
9439 slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then
9440 @samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does
9441 not contain a slash.
9444 @vindex @@F @r{(automatic variable)}
9446 The file-within-directory part of the file name of the target. If the
9447 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9448 @file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9451 @vindex *D @r{(automatic variable)}
9454 @vindex *F @r{(automatic variable)}
9456 The directory part and the file-within-directory
9457 part of the stem; @file{dir} and @file{foo} in this example.
9460 @vindex %D @r{(automatic variable)}
9463 @vindex %F @r{(automatic variable)}
9465 The directory part and the file-within-directory part of the target
9466 archive member name. This makes sense only for archive member targets
9467 of the form @file{@var{archive}(@var{member})} and is useful only when
9468 @var{member} may contain a directory name. (@xref{Archive Members,
9469 ,Archive Members as Targets}.)
9472 @vindex <D @r{(automatic variable)}
9475 @vindex <F @r{(automatic variable)}
9477 The directory part and the file-within-directory
9478 part of the first prerequisite.
9481 @vindex ^D @r{(automatic variable)}
9484 @vindex ^F @r{(automatic variable)}
9486 Lists of the directory parts and the file-within-directory
9487 parts of all prerequisites.
9490 @vindex +D @r{(automatic variable)}
9493 @vindex +F @r{(automatic variable)}
9495 Lists of the directory parts and the file-within-directory
9496 parts of all prerequisites, including multiple instances of duplicated
9500 @vindex ?D @r{(automatic variable)}
9503 @vindex ?F @r{(automatic variable)}
9505 Lists of the directory parts and the file-within-directory parts of
9506 all prerequisites that are newer than the target.
9509 Note that we use a special stylistic convention when we talk about these
9510 automatic variables; we write ``the value of @samp{$<}'', rather than
9511 @w{``the variable @code{<}''} as we would write for ordinary variables
9512 such as @code{objects} and @code{CFLAGS}. We think this convention
9513 looks more natural in this special case. Please do not assume it has a
9514 deep significance; @samp{$<} refers to the variable named @code{<} just
9515 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9516 You could just as well use @samp{$(<)} in place of @samp{$<}.
9518 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9519 @subsection How Patterns Match
9522 A target pattern is composed of a @samp{%} between a prefix and a suffix,
9523 either or both of which may be empty. The pattern matches a file name only
9524 if the file name starts with the prefix and ends with the suffix, without
9525 overlap. The text between the prefix and the suffix is called the
9526 @dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name
9527 @file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are
9528 turned into actual file names by substituting the stem for the character
9529 @samp{%}. Thus, if in the same example one of the prerequisites is written
9530 as @samp{%.c}, it expands to @samp{test.c}.@refill
9532 When the target pattern does not contain a slash (and it usually does
9533 not), directory names in the file names are removed from the file name
9534 before it is compared with the target prefix and suffix. After the
9535 comparison of the file name to the target pattern, the directory
9536 names, along with the slash that ends them, are added on to the
9537 prerequisite file names generated from the pattern rule's prerequisite
9538 patterns and the file name. The directories are ignored only for the
9539 purpose of finding an implicit rule to use, not in the application of
9540 that rule. Thus, @samp{e%t} matches the file name @file{src/eat},
9541 with @samp{src/a} as the stem. When prerequisites are turned into file
9542 names, the directories from the stem are added at the front, while the
9543 rest of the stem is substituted for the @samp{%}. The stem
9544 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9545 @file{src/car}.@refill
9547 @cindex pattern rules, order of
9548 @cindex order of pattern rules
9549 A pattern rule can be used to build a given file only if there is a
9550 target pattern that matches the file name, @emph{and} all
9551 prerequisites in that rule either exist or can be built. The rules
9552 you write take precedence over those that are built in. Note however,
9553 that a rule whose prerequisites actually exist or are mentioned always
9554 takes priority over a rule with prerequisites that must be made by
9555 chaining other implicit rules.
9557 @cindex stem, shortest
9558 It is possible that more than one pattern rule will meet these
9559 criteria. In that case, @code{make} will choose the rule with the
9560 shortest stem (that is, the pattern that matches most specifically).
9561 If more than one pattern rule has the shortest stem, @code{make} will
9562 choose the first one found in the makefile.
9564 This algorithm results in more specific rules being preferred over
9565 more generic ones; for example:
9569 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9572 $(COMPILE.F) $(OUTPUT_OPTION) $<
9575 $(CC) -fPIC -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9578 Given these rules and asked to build @file{bar.o} where both
9579 @file{bar.c} and @file{bar.f} exist, @code{make} will choose the first
9580 rule and compile @file{bar.c} into @file{bar.o}. In the same
9581 situation where @file{bar.c} does not exist, then @code{make} will
9582 choose the second rule and compile @file{bar.f} into @file{bar.o}.
9584 If @code{make} is asked to build @file{lib/bar.o} and both
9585 @file{lib/bar.c} and @file{lib/bar.f} exist, then the third rule will
9586 be chosen since the stem for this rule (@samp{bar}) is shorter than
9587 the stem for the first rule (@samp{lib/bar}). If @file{lib/bar.c}
9588 does not exist then the third rule is not eligible and the second rule
9589 will be used, even though the stem is longer.
9591 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9592 @subsection Match-Anything Pattern Rules
9594 @cindex match-anything rule
9595 @cindex terminal rule
9596 When a pattern rule's target is just @samp{%}, it matches any file name
9597 whatever. We call these rules @dfn{match-anything} rules. They are very
9598 useful, but it can take a lot of time for @code{make} to think about them,
9599 because it must consider every such rule for each file name listed either
9600 as a target or as a prerequisite.
9602 Suppose the makefile mentions @file{foo.c}. For this target, @code{make}
9603 would have to consider making it by linking an object file @file{foo.c.o},
9604 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9605 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9608 We know these possibilities are ridiculous since @file{foo.c} is a C source
9609 file, not an executable. If @code{make} did consider these possibilities,
9610 it would ultimately reject them, because files such as @file{foo.c.o} and
9611 @file{foo.c.p} would not exist. But these possibilities are so
9612 numerous that @code{make} would run very slowly if it had to consider
9615 To gain speed, we have put various constraints on the way @code{make}
9616 considers match-anything rules. There are two different constraints that
9617 can be applied, and each time you define a match-anything rule you must
9618 choose one or the other for that rule.
9620 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9621 it with a double colon. When a rule is terminal, it does not apply unless
9622 its prerequisites actually exist. Prerequisites that could be made with
9623 other implicit rules are not good enough. In other words, no further
9624 chaining is allowed beyond a terminal rule.
9626 For example, the built-in implicit rules for extracting sources from RCS
9627 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9628 not exist, @code{make} will not even consider trying to make it as an
9629 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9630 RCS and SCCS files are generally ultimate source files, which should not be
9631 remade from any other files; therefore, @code{make} can save time by not
9632 looking for ways to remake them.@refill
9634 If you do not mark the match-anything rule as terminal, then it is
9635 nonterminal. A nonterminal match-anything rule cannot apply to a file name
9636 that indicates a specific type of data. A file name indicates a specific
9637 type of data if some non-match-anything implicit rule target matches it.
9639 For example, the file name @file{foo.c} matches the target for the pattern
9640 rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this
9641 rule is actually applicable (which happens only if there is a file
9642 @file{foo.y}), the fact that its target matches is enough to prevent
9643 consideration of any nonterminal match-anything rules for the file
9644 @file{foo.c}. Thus, @code{make} will not even consider trying to make
9645 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9646 @file{foo.c.p}, etc.@refill
9648 The motivation for this constraint is that nonterminal match-anything
9649 rules are used for making files containing specific types of data (such as
9650 executable files) and a file name with a recognized suffix indicates some
9651 other specific type of data (such as a C source file).
9653 Special built-in dummy pattern rules are provided solely to recognize
9654 certain file names so that nonterminal match-anything rules will not be
9655 considered. These dummy rules have no prerequisites and no recipes, and
9656 they are ignored for all other purposes. For example, the built-in
9664 exists to make sure that Pascal source files such as @file{foo.p} match a
9665 specific target pattern and thereby prevent time from being wasted looking
9666 for @file{foo.p.o} or @file{foo.p.c}.
9668 Dummy pattern rules such as the one for @samp{%.p} are made for every
9669 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9671 @node Canceling Rules, , Match-Anything Rules, Pattern Rules
9672 @subsection Canceling Implicit Rules
9674 You can override a built-in implicit rule (or one you have defined
9675 yourself) by defining a new pattern rule with the same target and
9676 prerequisites, but a different recipe. When the new rule is defined, the
9677 built-in one is replaced. The new rule's position in the sequence of
9678 implicit rules is determined by where you write the new rule.
9680 You can cancel a built-in implicit rule by defining a pattern rule with the
9681 same target and prerequisites, but no recipe. For example, the following
9682 would cancel the rule that runs the assembler:
9688 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9689 @section Defining Last-Resort Default Rules
9690 @cindex last-resort default rules
9691 @cindex default rules, last-resort
9693 You can define a last-resort implicit rule by writing a terminal
9694 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9695 Rules}). This is just like any other pattern rule; the only thing
9696 special about it is that it will match any target. So such a rule's
9697 recipe is used for all targets and prerequisites that have no recipe
9698 of their own and for which no other implicit rule applies.
9700 For example, when testing a makefile, you might not care if the source
9701 files contain real data, only that they exist. Then you might do this:
9709 to cause all the source files needed (as prerequisites) to be created
9713 You can instead define a recipe to be used for targets for which there
9714 are no rules at all, even ones which don't specify recipes. You do
9715 this by writing a rule for the target @code{.DEFAULT}. Such a rule's
9716 recipe is used for all prerequisites which do not appear as targets in
9717 any explicit rule, and for which no implicit rule applies. Naturally,
9718 there is no @code{.DEFAULT} rule unless you write one.
9720 If you use @code{.DEFAULT} with no recipe or prerequisites:
9727 the recipe previously stored for @code{.DEFAULT} is cleared. Then
9728 @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9730 If you do not want a target to get the recipe from a match-anything
9731 pattern rule or @code{.DEFAULT}, but you also do not want any recipe
9732 to be run for the target, you can give it an empty recipe
9733 (@pxref{Empty Recipes, ,Defining Empty Recipes}).@refill
9735 You can use a last-resort rule to override part of another makefile.
9736 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9738 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9739 @section Old-Fashioned Suffix Rules
9740 @cindex old-fashioned suffix rules
9743 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9744 @code{make}. Suffix rules are obsolete because pattern rules are more
9745 general and clearer. They are supported in GNU @code{make} for
9746 compatibility with old makefiles. They come in two kinds:
9747 @dfn{double-suffix} and @dfn{single-suffix}.@refill
9749 A double-suffix rule is defined by a pair of suffixes: the target suffix
9750 and the source suffix. It matches any file whose name ends with the
9751 target suffix. The corresponding implicit prerequisite is made by
9752 replacing the target suffix with the source suffix in the file name. A
9753 two-suffix rule whose target and source suffixes are @samp{.o} and
9754 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9756 A single-suffix rule is defined by a single suffix, which is the source
9757 suffix. It matches any file name, and the corresponding implicit
9758 prerequisite name is made by appending the source suffix. A single-suffix
9759 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9762 Suffix rule definitions are recognized by comparing each rule's target
9763 against a defined list of known suffixes. When @code{make} sees a rule
9764 whose target is a known suffix, this rule is considered a single-suffix
9765 rule. When @code{make} sees a rule whose target is two known suffixes
9766 concatenated, this rule is taken as a double-suffix rule.
9768 For example, @samp{.c} and @samp{.o} are both on the default list of
9769 known suffixes. Therefore, if you define a rule whose target is
9770 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9771 suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned
9772 way to define the rule for compiling a C source file:@refill
9776 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9779 Suffix rules cannot have any prerequisites of their own. If they have any,
9780 they are treated as normal files with funny names, not as suffix rules.
9785 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9789 tells how to make the file @file{.c.o} from the prerequisite file
9790 @file{foo.h}, and is not at all like the pattern rule:
9794 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9798 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9799 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
9801 Suffix rules with no recipe are also meaningless. They do not remove
9802 previous rules as do pattern rules with no recipe (@pxref{Canceling
9803 Rules, , Canceling Implicit Rules}). They simply enter the suffix or
9804 pair of suffixes concatenated as a target in the data base.@refill
9807 The known suffixes are simply the names of the prerequisites of the special
9808 target @code{.SUFFIXES}. You can add your own suffixes by writing a rule
9809 for @code{.SUFFIXES} that adds more prerequisites, as in:
9812 .SUFFIXES: .hack .win
9816 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9818 If you wish to eliminate the default known suffixes instead of just adding
9819 to them, write a rule for @code{.SUFFIXES} with no prerequisites. By
9820 special dispensation, this eliminates all existing prerequisites of
9821 @code{.SUFFIXES}. You can then write another rule to add the suffixes you
9826 .SUFFIXES: # @r{Delete the default suffixes}
9827 .SUFFIXES: .c .o .h # @r{Define our suffix list}
9831 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9832 list of suffixes to be empty.
9835 The variable @code{SUFFIXES} is defined to the default list of suffixes
9836 before @code{make} reads any makefiles. You can change the list of suffixes
9837 with a rule for the special target @code{.SUFFIXES}, but that does not alter
9840 @node Implicit Rule Search, , Suffix Rules, Implicit Rules
9841 @section Implicit Rule Search Algorithm
9842 @cindex implicit rule, search algorithm
9843 @cindex search algorithm, implicit rule
9845 Here is the procedure @code{make} uses for searching for an implicit rule
9846 for a target @var{t}. This procedure is followed for each double-colon
9847 rule with no recipe, for each target of ordinary rules none of which have
9848 a recipe, and for each prerequisite that is not the target of any rule. It
9849 is also followed recursively for prerequisites that come from implicit
9850 rules, in the search for a chain of rules.
9852 Suffix rules are not mentioned in this algorithm because suffix rules are
9853 converted to equivalent pattern rules once the makefiles have been read in.
9855 For an archive member target of the form
9856 @samp{@var{archive}(@var{member})}, the following algorithm is run
9857 twice, first using the entire target name @var{t}, and second using
9858 @samp{(@var{member})} as the target @var{t} if the first run found no
9863 Split @var{t} into a directory part, called @var{d}, and the rest,
9864 called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then
9865 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9868 Make a list of all the pattern rules one of whose targets matches
9869 @var{t} or @var{n}. If the target pattern contains a slash, it is
9870 matched against @var{t}; otherwise, against @var{n}.
9873 If any rule in that list is @emph{not} a match-anything rule, then
9874 remove all nonterminal match-anything rules from the list.
9877 Remove from the list all rules with no recipe.
9880 For each pattern rule in the list:
9884 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
9885 matched by the @samp{%} in the target pattern.@refill
9888 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
9889 the target pattern does not contain a slash, append @var{d} to
9890 the front of each prerequisite name.@refill
9893 Test whether all the prerequisites exist or ought to exist. (If a
9894 file name is mentioned in the makefile as a target or as an explicit
9895 prerequisite, then we say it ought to exist.)
9897 If all prerequisites exist or ought to exist, or there are no prerequisites,
9898 then this rule applies.
9902 If no pattern rule has been found so far, try harder.
9903 For each pattern rule in the list:
9907 If the rule is terminal, ignore it and go on to the next rule.
9910 Compute the prerequisite names as before.
9913 Test whether all the prerequisites exist or ought to exist.
9916 For each prerequisite that does not exist, follow this algorithm
9917 recursively to see if the prerequisite can be made by an implicit
9921 If all prerequisites exist, ought to exist, or can be
9922 made by implicit rules, then this rule applies.
9926 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
9927 applies. In that case, give @var{t} the same recipe that
9928 @code{.DEFAULT} has. Otherwise, there is no recipe for @var{t}.
9931 Once a rule that applies has been found, for each target pattern of
9932 the rule other than the one that matched @var{t} or @var{n}, the
9933 @samp{%} in the pattern is replaced with @var{s} and the resultant
9934 file name is stored until the recipe to remake the target file @var{t}
9935 is executed. After the recipe is executed, each of these stored file
9936 names are entered into the data base and marked as having been updated
9937 and having the same update status as the file @var{t}.
9939 When the recipe of a pattern rule is executed for @var{t}, the
9940 automatic variables are set corresponding to the target and
9941 prerequisites. @xref{Automatic Variables}.
9943 @node Archives, Features, Implicit Rules, Top
9944 @chapter Using @code{make} to Update Archive Files
9947 @dfn{Archive files} are files containing named subfiles called
9948 @dfn{members}; they are maintained with the program @code{ar} and their
9949 main use is as subroutine libraries for linking.
9952 * Archive Members:: Archive members as targets.
9953 * Archive Update:: The implicit rule for archive member targets.
9954 * Archive Pitfalls:: Dangers to watch out for when using archives.
9955 * Archive Suffix Rules:: You can write a special kind of suffix rule
9956 for updating archives.
9959 @node Archive Members, Archive Update, Archives, Archives
9960 @section Archive Members as Targets
9961 @cindex archive member targets
9963 An individual member of an archive file can be used as a target or
9964 prerequisite in @code{make}. You specify the member named @var{member} in
9965 archive file @var{archive} as follows:
9968 @var{archive}(@var{member})
9972 This construct is available only in targets and prerequisites, not in
9973 recipes! Most programs that you might use in recipes do not support
9974 this syntax and cannot act directly on archive members. Only
9975 @code{ar} and other programs specifically designed to operate on
9976 archives can do so. Therefore, valid recipes to update an archive
9977 member target probably must use @code{ar}. For example, this rule
9978 says to create a member @file{hack.o} in archive @file{foolib} by
9979 copying the file @file{hack.o}:
9982 foolib(hack.o) : hack.o
9986 In fact, nearly all archive member targets are updated in just this way
9987 and there is an implicit rule to do it for you. @strong{Please note:} The
9988 @samp{c} flag to @code{ar} is required if the archive file does not
9991 To specify several members in the same archive, you can write all the
9992 member names together between the parentheses. For example:
9995 foolib(hack.o kludge.o)
10002 foolib(hack.o) foolib(kludge.o)
10005 @cindex wildcard, in archive member
10006 You can also use shell-style wildcards in an archive member reference.
10007 @xref{Wildcards, ,Using Wildcard Characters in File Names}. For
10008 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
10009 @file{foolib} archive whose names end in @samp{.o}; perhaps
10010 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
10012 @node Archive Update, Archive Pitfalls, Archive Members, Archives
10013 @section Implicit Rule for Archive Member Targets
10015 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
10016 member named @var{m} in the archive file @var{a}.
10018 When @code{make} looks for an implicit rule for such a target, as a special
10019 feature it considers implicit rules that match @file{(@var{m})}, as well as
10020 those that match the actual target @file{@var{a}(@var{m})}.
10022 This causes one special rule whose target is @file{(%)} to match. This
10023 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
10024 into the archive. For example, it will update the archive member target
10025 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
10026 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
10028 When this rule is chained with others, the result is very powerful.
10029 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
10030 @samp{(} and @samp{)} from being interpreted specially by the shell) in
10031 the presence of a file @file{bar.c} is enough to cause the following
10032 recipe to be run, even without a makefile:
10035 cc -c bar.c -o bar.o
10041 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
10042 file. @xref{Chained Rules, ,Chains of Implicit Rules}.
10044 Implicit rules such as this one are written using the automatic variable
10045 @samp{$%}. @xref{Automatic Variables}.
10047 An archive member name in an archive cannot contain a directory name, but
10048 it may be useful in a makefile to pretend that it does. If you write an
10049 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
10050 automatic updating with this recipe:
10053 ar r foo.a dir/file.o
10057 which has the effect of copying the file @file{dir/file.o} into a member
10058 named @file{file.o}. In connection with such usage, the automatic variables
10059 @code{%D} and @code{%F} may be useful.
10062 * Archive Symbols:: How to update archive symbol directories.
10065 @node Archive Symbols, , Archive Update, Archive Update
10066 @subsection Updating Archive Symbol Directories
10067 @cindex @code{__.SYMDEF}
10068 @cindex updating archive symbol directories
10069 @cindex archive symbol directory updating
10070 @cindex symbol directories, updating archive
10071 @cindex directories, updating archive symbol
10073 An archive file that is used as a library usually contains a special member
10074 named @file{__.SYMDEF} that contains a directory of the external symbol
10075 names defined by all the other members. After you update any other
10076 members, you need to update @file{__.SYMDEF} so that it will summarize the
10077 other members properly. This is done by running the @code{ranlib} program:
10080 ranlib @var{archivefile}
10083 Normally you would put this command in the rule for the archive file,
10084 and make all the members of the archive file prerequisites of that rule.
10088 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
10093 The effect of this is to update archive members @file{x.o}, @file{y.o},
10094 etc., and then update the symbol directory member @file{__.SYMDEF} by
10095 running @code{ranlib}. The rules for updating the members are not shown
10096 here; most likely you can omit them and use the implicit rule which copies
10097 files into the archive, as described in the preceding section.
10099 This is not necessary when using the GNU @code{ar} program, which
10100 updates the @file{__.SYMDEF} member automatically.
10102 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
10103 @section Dangers When Using Archives
10104 @cindex archive, and parallel execution
10105 @cindex parallel execution, and archive update
10106 @cindex archive, and @code{-j}
10107 @cindex @code{-j}, and archive update
10109 It is important to be careful when using parallel execution (the
10110 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
10111 If multiple @code{ar} commands run at the same time on the same archive
10112 file, they will not know about each other and can corrupt the file.
10114 Possibly a future version of @code{make} will provide a mechanism to
10115 circumvent this problem by serializing all recipes that operate on the
10116 same archive file. But for the time being, you must either write your
10117 makefiles to avoid this problem in some other way, or not use @code{-j}.
10119 @node Archive Suffix Rules, , Archive Pitfalls, Archives
10120 @section Suffix Rules for Archive Files
10121 @cindex suffix rule, for archive
10122 @cindex archive, suffix rule for
10123 @cindex library archive, suffix rule for
10124 @cindex @code{.a} (archives)
10126 You can write a special kind of suffix rule for dealing with archive
10127 files. @xref{Suffix Rules}, for a full explanation of suffix rules.
10128 Archive suffix rules are obsolete in GNU @code{make}, because pattern
10129 rules for archives are a more general mechanism (@pxref{Archive
10130 Update}). But they are retained for compatibility with other
10133 To write a suffix rule for archives, you simply write a suffix rule
10134 using the target suffix @samp{.a} (the usual suffix for archive files).
10135 For example, here is the old-fashioned suffix rule to update a library
10136 archive from C source files:
10141 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10148 This works just as if you had written the pattern rule:
10153 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10159 In fact, this is just what @code{make} does when it sees a suffix rule
10160 with @samp{.a} as the target suffix. Any double-suffix rule
10161 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
10162 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
10164 Since you might want to use @samp{.a} as the suffix for some other kind
10165 of file, @code{make} also converts archive suffix rules to pattern rules
10166 in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule
10167 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
10168 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
10170 @node Features, Missing, Archives, Top
10171 @chapter Features of GNU @code{make}
10172 @cindex features of GNU @code{make}
10173 @cindex portability
10174 @cindex compatibility
10176 Here is a summary of the features of GNU @code{make}, for comparison
10177 with and credit to other versions of @code{make}. We consider the
10178 features of @code{make} in 4.2 BSD systems as a baseline. If you are
10179 concerned with writing portable makefiles, you should not use the
10180 features of @code{make} listed here, nor the ones in @ref{Missing}.
10182 Many features come from the version of @code{make} in System V.
10186 The @code{VPATH} variable and its special meaning.
10187 @xref{Directory Search, , Searching Directories for Prerequisites}.
10188 This feature exists in System V @code{make}, but is undocumented.
10189 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
10190 @code{VPATH} feature).@refill
10193 Included makefiles. @xref{Include, ,Including Other Makefiles}.
10194 Allowing multiple files to be included with a single directive is a GNU
10198 Variables are read from and communicated via the environment.
10199 @xref{Environment, ,Variables from the Environment}.
10202 Options passed through the variable @code{MAKEFLAGS} to recursive
10203 invocations of @code{make}.
10204 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10207 The automatic variable @code{$%} is set to the member name
10208 in an archive reference. @xref{Automatic Variables}.
10211 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
10212 and @code{$?} have corresponding forms like @code{$(@@F)} and
10213 @code{$(@@D)}. We have generalized this to @code{$^} as an obvious
10214 extension. @xref{Automatic Variables}.@refill
10217 Substitution variable references.
10218 @xref{Reference, ,Basics of Variable References}.
10221 The command line options @samp{-b} and @samp{-m}, accepted and
10222 ignored. In System V @code{make}, these options actually do something.
10225 Execution of recursive commands to run @code{make} via the variable
10226 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
10227 @xref{Recursion, ,Recursive Use of @code{make}}.
10230 Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix
10231 Rules}. This feature is obsolete in GNU @code{make}, because the
10232 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
10233 Implicit Rules}) allows one pattern rule for installing members in an
10234 archive (@pxref{Archive Update}) to be sufficient.
10237 The arrangement of lines and backslash-newline combinations in
10238 recipes is retained when the recipes are printed, so they appear as
10239 they do in the makefile, except for the stripping of initial
10243 The following features were inspired by various other versions of
10244 @code{make}. In some cases it is unclear exactly which versions inspired
10249 Pattern rules using @samp{%}.
10250 This has been implemented in several versions of @code{make}.
10251 We're not sure who invented it first, but it's been spread around a bit.
10252 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
10255 Rule chaining and implicit intermediate files.
10256 This was implemented by Stu Feldman in his version of @code{make}
10257 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
10258 AT&T Bell Labs in his @code{mk} program (where he terms it
10259 ``transitive closure''). We do not really know if
10260 we got this from either of them or thought it up ourselves at the
10261 same time. @xref{Chained Rules, ,Chains of Implicit Rules}.
10264 The automatic variable @code{$^} containing a list of all prerequisites
10265 of the current target. We did not invent this, but we have no idea who
10266 did. @xref{Automatic Variables}. The automatic variable
10267 @code{$+} is a simple extension of @code{$^}.
10270 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
10271 invented by Andrew Hume in @code{mk}.
10272 @xref{Instead of Execution, ,Instead of Executing Recipes}.
10275 The concept of doing several things at once (parallelism) exists in
10276 many incarnations of @code{make} and similar programs, though not in the
10277 System V or BSD implementations. @xref{Execution, ,Recipe Execution}.
10280 Modified variable references using pattern substitution come from
10281 SunOS 4. @xref{Reference, ,Basics of Variable References}.
10282 This functionality was provided in GNU @code{make} by the
10283 @code{patsubst} function before the alternate syntax was implemented
10284 for compatibility with SunOS 4. It is not altogether clear who
10285 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
10286 4 was released.@refill
10289 The special significance of @samp{+} characters preceding recipe lines
10290 (@pxref{Instead of Execution, ,Instead of Executing Recipes}) is
10291 mandated by @cite{IEEE Standard 1003.2-1992} (POSIX.2).
10294 The @samp{+=} syntax to append to the value of a variable comes from SunOS
10295 4 @code{make}. @xref{Appending, , Appending More Text to Variables}.
10298 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
10299 multiple members in a single archive file comes from SunOS 4 @code{make}.
10300 @xref{Archive Members}.
10303 The @code{-include} directive to include makefiles with no error for a
10304 nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4
10305 @code{make} does not allow multiple makefiles to be specified in one
10306 @code{-include} directive.) The same feature appears with the name
10307 @code{sinclude} in SGI @code{make} and perhaps others.
10310 The remaining features are inventions new in GNU @code{make}:
10314 Use the @samp{-v} or @samp{--version} option to print version and
10315 copyright information.
10318 Use the @samp{-h} or @samp{--help} option to summarize the options to
10322 Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}.
10325 Pass command line variable assignments automatically through the
10326 variable @code{MAKE} to recursive @code{make} invocations.
10327 @xref{Recursion, ,Recursive Use of @code{make}}.
10330 Use the @samp{-C} or @samp{--directory} command option to change
10331 directory. @xref{Options Summary, ,Summary of Options}.
10334 Make verbatim variable definitions with @code{define}.
10335 @xref{Multi-Line, ,Defining Multi-Line Variables}.
10338 Declare phony targets with the special target @code{.PHONY}.
10340 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
10341 different syntax in his @code{mk} program. This seems to be a case of
10342 parallel discovery. @xref{Phony Targets, ,Phony Targets}.
10345 Manipulate text by calling functions.
10346 @xref{Functions, ,Functions for Transforming Text}.
10349 Use the @samp{-o} or @samp{--old-file}
10350 option to pretend a file's modification-time is old.
10351 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
10354 Conditional execution.
10356 This feature has been implemented numerous times in various versions
10357 of @code{make}; it seems a natural extension derived from the features
10358 of the C preprocessor and similar macro languages and is not a
10359 revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}.
10362 Specify a search path for included makefiles.
10363 @xref{Include, ,Including Other Makefiles}.
10366 Specify extra makefiles to read with an environment variable.
10367 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10370 Strip leading sequences of @samp{./} from file names, so that
10371 @file{./@var{file}} and @file{@var{file}} are considered to be the
10375 Use a special search method for library prerequisites written in the
10376 form @samp{-l@var{name}}.
10377 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10380 Allow suffixes for suffix rules
10381 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
10382 characters. In other versions of @code{make}, they must begin with
10383 @samp{.} and not contain any @samp{/} characters.
10386 Keep track of the current level of @code{make} recursion using the
10387 variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}.
10390 Provide any goals given on the command line in the variable
10391 @code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}.
10394 Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}.
10397 Provide selective @code{vpath} search.
10398 @xref{Directory Search, ,Searching Directories for Prerequisites}.
10401 Provide computed variable references.
10402 @xref{Reference, ,Basics of Variable References}.
10405 Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
10406 System V @code{make} has a very, very limited form of this
10407 functionality in that it will check out SCCS files for makefiles.
10410 Various new built-in implicit rules.
10411 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
10414 The built-in variable @samp{MAKE_VERSION} gives the version number of
10416 @vindex MAKE_VERSION
10419 @node Missing, Makefile Conventions, Features, Top
10420 @chapter Incompatibilities and Missing Features
10421 @cindex incompatibilities
10422 @cindex missing features
10423 @cindex features, missing
10425 The @code{make} programs in various other systems support a few features
10426 that are not implemented in GNU @code{make}. The POSIX.2 standard
10427 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
10428 require any of these features.@refill
10432 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
10433 of archive file @var{file}. The member is chosen, not by name, but by
10434 being an object file which defines the linker symbol @var{entry}.@refill
10436 This feature was not put into GNU @code{make} because of the
10437 nonmodularity of putting knowledge into @code{make} of the internal
10438 format of archive file symbol tables.
10439 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
10442 Suffixes (used in suffix rules) that end with the character @samp{~}
10443 have a special meaning to System V @code{make};
10444 they refer to the SCCS file that corresponds
10445 to the file one would get without the @samp{~}. For example, the
10446 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
10447 the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole
10448 series of such suffix rules is required.
10449 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
10451 In GNU @code{make}, this entire series of cases is handled by two
10452 pattern rules for extraction from SCCS, in combination with the
10453 general feature of rule chaining.
10454 @xref{Chained Rules, ,Chains of Implicit Rules}.
10457 In System V and 4.3 BSD @code{make}, files found by @code{VPATH}
10458 search (@pxref{Directory Search, ,Searching Directories for
10459 Prerequisites}) have their names changed inside recipes. We feel it
10460 is much cleaner to always use automatic variables and thus make this
10461 feature obsolete.@refill
10464 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
10465 the prerequisites of a rule has the amazingly strange ``feature'' of
10466 expanding to the full name of the @emph{target of that rule}. We cannot
10467 imagine what went on in the minds of Unix @code{make} developers to do
10468 this; it is utterly inconsistent with the normal definition of @code{$*}.
10469 @vindex * @r{(automatic variable), unsupported bizarre usage}
10472 In some Unix @code{make}s, implicit rule search (@pxref{Implicit
10473 Rules, ,Using Implicit Rules}) is apparently done for @emph{all}
10474 targets, not just those without recipes. This means you can
10485 and Unix @code{make} will intuit that @file{foo.o} depends on
10486 @file{foo.c}.@refill
10488 We feel that such usage is broken. The prerequisite properties of
10489 @code{make} are well-defined (for GNU @code{make}, at least),
10490 and doing such a thing simply does not fit the model.@refill
10493 GNU @code{make} does not include any built-in implicit rules for
10494 compiling or preprocessing EFL programs. If we hear of anyone who is
10495 using EFL, we will gladly add them.
10498 It appears that in SVR4 @code{make}, a suffix rule can be specified
10499 with no recipe, and it is treated as if it had an empty recipe
10500 (@pxref{Empty Recipes}). For example:
10507 will override the built-in @file{.c.a} suffix rule.
10509 We feel that it is cleaner for a rule without a recipe to always simply
10510 add to the prerequisite list for the target. The above example can be
10511 easily rewritten to get the desired behavior in GNU @code{make}:
10518 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
10519 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
10520 Program}). The @samp{-e} flag tells the shell to exit as soon as any
10521 program it runs returns a nonzero status. We feel it is cleaner to
10522 write each line of the recipe to stand on its own and not require this
10526 @comment The makefile standards are in a separate file that is also
10527 @comment included by standards.texi.
10528 @include make-stds.texi
10530 @node Quick Reference, Error Messages, Makefile Conventions, Top
10531 @appendix Quick Reference
10533 This appendix summarizes the directives, text manipulation functions,
10534 and special variables which GNU @code{make} understands.
10535 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
10536 and @ref{Options Summary, ,Summary of Options},
10537 for other summaries.
10539 Here is a summary of the directives GNU @code{make} recognizes:
10542 @item define @var{variable}
10543 @itemx define @var{variable} =
10544 @itemx define @var{variable} :=
10545 @itemx define @var{variable} +=
10546 @itemx define @var{variable} ?=
10548 Define multi-line variables.@*
10551 @item undefine @var{variable}
10552 Undefining variables.@*
10553 @xref{Undefine Directive}.
10555 @item ifdef @var{variable}
10556 @itemx ifndef @var{variable}
10557 @itemx ifeq (@var{a},@var{b})
10558 @itemx ifeq "@var{a}" "@var{b}"
10559 @itemx ifeq '@var{a}' '@var{b}'
10560 @itemx ifneq (@var{a},@var{b})
10561 @itemx ifneq "@var{a}" "@var{b}"
10562 @itemx ifneq '@var{a}' '@var{b}'
10565 Conditionally evaluate part of the makefile.@*
10566 @xref{Conditionals}.
10568 @item include @var{file}
10569 @itemx -include @var{file}
10570 @itemx sinclude @var{file}
10571 Include another makefile.@*
10572 @xref{Include, ,Including Other Makefiles}.
10574 @item override @var{variable-assignment}
10575 Define a variable, overriding any previous definition, even one from
10576 the command line.@*
10577 @xref{Override Directive, ,The @code{override} Directive}.
10580 Tell @code{make} to export all variables to child processes by default.@*
10581 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10583 @item export @var{variable}
10584 @itemx export @var{variable-assignment}
10585 @itemx unexport @var{variable}
10586 Tell @code{make} whether or not to export a particular variable to child
10588 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10590 @item private @var{variable-assignment}
10591 Do not allow this variable assignment to be inherited by prerequisites.@*
10592 @xref{Suppressing Inheritance}.
10594 @item vpath @var{pattern} @var{path}
10595 Specify a search path for files matching a @samp{%} pattern.@*
10596 @xref{Selective Search, , The @code{vpath} Directive}.
10598 @item vpath @var{pattern}
10599 Remove all search paths previously specified for @var{pattern}.
10602 Remove all search paths previously specified in any @code{vpath}
10606 Here is a summary of the built-in functions (@pxref{Functions}):
10609 @item $(subst @var{from},@var{to},@var{text})
10610 Replace @var{from} with @var{to} in @var{text}.@*
10611 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10613 @item $(patsubst @var{pattern},@var{replacement},@var{text})
10614 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10615 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10617 @item $(strip @var{string})
10618 Remove excess whitespace characters from @var{string}.@*
10619 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10621 @item $(findstring @var{find},@var{text})
10622 Locate @var{find} in @var{text}.@*
10623 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10625 @item $(filter @var{pattern}@dots{},@var{text})
10626 Select words in @var{text} that match one of the @var{pattern} words.@*
10627 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10629 @item $(filter-out @var{pattern}@dots{},@var{text})
10630 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10631 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10633 @item $(sort @var{list})
10634 Sort the words in @var{list} lexicographically, removing duplicates.@*
10635 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10637 @item $(word @var{n},@var{text})
10638 Extract the @var{n}th word (one-origin) of @var{text}.@*
10639 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10641 @item $(words @var{text})
10642 Count the number of words in @var{text}.@*
10643 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10645 @item $(wordlist @var{s},@var{e},@var{text})
10646 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10647 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10649 @item $(firstword @var{names}@dots{})
10650 Extract the first word of @var{names}.@*
10651 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10653 @item $(lastword @var{names}@dots{})
10654 Extract the last word of @var{names}.@*
10655 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10657 @item $(dir @var{names}@dots{})
10658 Extract the directory part of each file name.@*
10659 @xref{File Name Functions, ,Functions for File Names}.
10661 @item $(notdir @var{names}@dots{})
10662 Extract the non-directory part of each file name.@*
10663 @xref{File Name Functions, ,Functions for File Names}.
10665 @item $(suffix @var{names}@dots{})
10666 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10667 @xref{File Name Functions, ,Functions for File Names}.
10669 @item $(basename @var{names}@dots{})
10670 Extract the base name (name without suffix) of each file name.@*
10671 @xref{File Name Functions, ,Functions for File Names}.
10673 @item $(addsuffix @var{suffix},@var{names}@dots{})
10674 Append @var{suffix} to each word in @var{names}.@*
10675 @xref{File Name Functions, ,Functions for File Names}.
10677 @item $(addprefix @var{prefix},@var{names}@dots{})
10678 Prepend @var{prefix} to each word in @var{names}.@*
10679 @xref{File Name Functions, ,Functions for File Names}.
10681 @item $(join @var{list1},@var{list2})
10682 Join two parallel lists of words.@*
10683 @xref{File Name Functions, ,Functions for File Names}.
10685 @item $(wildcard @var{pattern}@dots{})
10686 Find file names matching a shell file name pattern (@emph{not} a
10687 @samp{%} pattern).@*
10688 @xref{Wildcard Function, ,The Function @code{wildcard}}.
10690 @item $(realpath @var{names}@dots{})
10691 For each file name in @var{names}, expand to an absolute name that
10692 does not contain any @code{.}, @code{..}, nor symlinks.@*
10693 @xref{File Name Functions, ,Functions for File Names}.
10695 @item $(abspath @var{names}@dots{})
10696 For each file name in @var{names}, expand to an absolute name that
10697 does not contain any @code{.} or @code{..} components, but preserves
10699 @xref{File Name Functions, ,Functions for File Names}.
10701 @item $(error @var{text}@dots{})
10702 When this function is evaluated, @code{make} generates a fatal error
10703 with the message @var{text}.@*
10704 @xref{Make Control Functions, ,Functions That Control Make}.
10706 @item $(warning @var{text}@dots{})
10707 When this function is evaluated, @code{make} generates a warning with
10708 the message @var{text}.@*
10709 @xref{Make Control Functions, ,Functions That Control Make}.
10711 @item $(shell @var{command})
10712 Execute a shell command and return its output.@*
10713 @xref{Shell Function, , The @code{shell} Function}.
10715 @item $(origin @var{variable})
10716 Return a string describing how the @code{make} variable @var{variable} was
10718 @xref{Origin Function, , The @code{origin} Function}.
10720 @item $(flavor @var{variable})
10721 Return a string describing the flavor of the @code{make} variable
10723 @xref{Flavor Function, , The @code{flavor} Function}.
10725 @item $(foreach @var{var},@var{words},@var{text})
10726 Evaluate @var{text} with @var{var} bound to each word in @var{words},
10727 and concatenate the results.@*
10728 @xref{Foreach Function, ,The @code{foreach} Function}.
10730 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
10731 Evaluate the condition @var{condition}; if it's non-empty substitute
10732 the expansion of the @var{then-part} otherwise substitute the
10733 expansion of the @var{else-part}.@*
10734 @xref{Conditional Functions, ,Functions for Conditionals}.
10736 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
10737 Evaluate each condition @var{conditionN} one at a time; substitute the
10738 first non-empty expansion. If all expansions are empty, substitute
10739 the empty string.@*
10740 @xref{Conditional Functions, ,Functions for Conditionals}.
10742 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
10743 Evaluate each condition @var{conditionN} one at a time; if any
10744 expansion results in the empty string substitute the empty string. If
10745 all expansions result in a non-empty string, substitute the expansion
10746 of the last @var{condition}.@*
10747 @xref{Conditional Functions, ,Functions for Conditionals}.
10749 @item $(call @var{var},@var{param},@dots{})
10750 Evaluate the variable @var{var} replacing any references to @code{$(1)},
10751 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
10752 @xref{Call Function, ,The @code{call} Function}.
10754 @item $(eval @var{text})
10755 Evaluate @var{text} then read the results as makefile commands.
10756 Expands to the empty string.@*
10757 @xref{Eval Function, ,The @code{eval} Function}.
10759 @item $(value @var{var})
10760 Evaluates to the contents of the variable @var{var}, with no expansion
10762 @xref{Value Function, ,The @code{value} Function}.
10765 Here is a summary of the automatic variables.
10766 @xref{Automatic Variables},
10767 for full information.
10771 The file name of the target.
10774 The target member name, when the target is an archive member.
10777 The name of the first prerequisite.
10780 The names of all the prerequisites that are
10781 newer than the target, with spaces between them.
10782 For prerequisites which are archive members, only
10783 the named member is used (@pxref{Archives}).
10787 The names of all the prerequisites, with spaces between them. For
10788 prerequisites which are archive members, only the named member is used
10789 (@pxref{Archives}). The value of @code{$^} omits duplicate
10790 prerequisites, while @code{$+} retains them and preserves their order.
10793 The stem with which an implicit rule matches
10794 (@pxref{Pattern Match, ,How Patterns Match}).
10798 The directory part and the file-within-directory part of @code{$@@}.
10802 The directory part and the file-within-directory part of @code{$*}.
10806 The directory part and the file-within-directory part of @code{$%}.
10810 The directory part and the file-within-directory part of @code{$<}.
10814 The directory part and the file-within-directory part of @code{$^}.
10818 The directory part and the file-within-directory part of @code{$+}.
10822 The directory part and the file-within-directory part of @code{$?}.
10825 These variables are used specially by GNU @code{make}:
10830 Makefiles to be read on every invocation of @code{make}.@*
10831 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10835 Directory search path for files not found in the current directory.@*
10836 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10840 The name of the system default command interpreter, usually @file{/bin/sh}.
10841 You can set @code{SHELL} in the makefile to change the shell used to run
10842 recipes. @xref{Execution, ,Recipe Execution}. The @code{SHELL}
10843 variable is handled specially when importing from and exporting to the
10844 environment. @xref{Choosing the Shell}.
10848 On MS-DOS only, the name of the command interpreter that is to be used
10849 by @code{make}. This value takes precedence over the value of
10850 @code{SHELL}. @xref{Execution, ,MAKESHELL variable}.
10854 The name with which @code{make} was invoked. Using this variable in
10855 recipes has special meaning. @xref{MAKE Variable, ,How the
10856 @code{MAKE} Variable Works}.
10860 The number of levels of recursion (sub-@code{make}s).@*
10861 @xref{Variables/Recursion}.
10865 The flags given to @code{make}. You can set this in the environment or
10866 a makefile to set flags.@*
10867 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10869 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly in a
10870 recipe line: its contents may not be quoted correctly for use in the
10871 shell. Always allow recursive @code{make}'s to obtain these values
10872 through the environment from its parent.
10876 The targets given to @code{make} on the command line. Setting this
10877 variable has no effect on the operation of @code{make}.@*
10878 @xref{Goals, ,Arguments to Specify the Goals}.
10882 Set to the pathname of the current working directory (after all
10883 @code{-C} options are processed, if any). Setting this variable has no
10884 effect on the operation of @code{make}.@*
10885 @xref{Recursion, ,Recursive Use of @code{make}}.
10889 The default list of suffixes before @code{make} reads any makefiles.
10892 Defines the naming of the libraries @code{make} searches for, and their
10894 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10897 @node Error Messages, Complex Makefile, Quick Reference, Top
10898 @comment node-name, next, previous, up
10899 @appendix Errors Generated by Make
10901 Here is a list of the more common errors you might see generated by
10902 @code{make}, and some information about what they mean and how to fix
10905 Sometimes @code{make} errors are not fatal, especially in the presence
10906 of a @code{-} prefix on a recipe line, or the @code{-k} command line
10907 option. Errors that are fatal are prefixed with the string
10910 Error messages are all either prefixed with the name of the program
10911 (usually @samp{make}), or, if the error is found in a makefile, the name
10912 of the file and linenumber containing the problem.
10914 In the table below, these common prefixes are left off.
10918 @item [@var{foo}] Error @var{NN}
10919 @itemx [@var{foo}] @var{signal description}
10920 These errors are not really @code{make} errors at all. They mean that a
10921 program that @code{make} invoked as part of a recipe returned a
10922 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
10923 as failure, or it exited in some other abnormal fashion (with a
10924 signal of some type). @xref{Errors, ,Errors in Recipes}.
10926 If no @code{***} is attached to the message, then the subprocess failed
10927 but the rule in the makefile was prefixed with the @code{-} special
10928 character, so @code{make} ignored the error.
10930 @item missing separator. Stop.
10931 @itemx missing separator (did you mean TAB instead of 8 spaces?). Stop.
10932 This means that @code{make} could not understand much of anything
10933 about the makefile line it just read. GNU @code{make} looks for
10934 various separators (@code{:}, @code{=}, recipe prefix characters,
10935 etc.) to indicate what kind of line it's parsing. This message means
10936 it couldn't find a valid one.
10938 One of the most common reasons for this message is that you (or
10939 perhaps your oh-so-helpful editor, as is the case with many MS-Windows
10940 editors) have attempted to indent your recipe lines with spaces
10941 instead of a tab character. In this case, @code{make} will use the
10942 second form of the error above. Remember that every line in the
10943 recipe must begin with a tab character (unless you set
10944 @code{.CMDPREFIX}; @pxref{Special Variables}). Eight spaces do not
10945 count. @xref{Rule Syntax}.
10947 @item recipe commences before first target. Stop.
10948 @itemx missing rule before recipe. Stop.
10949 This means the first thing in the makefile seems to be part of a
10950 recipe: it begins with a recipe prefix character and doesn't appear to
10951 be a legal @code{make} directive (such as a variable assignment).
10952 Recipes must always be associated with a target.
10954 The second form is generated if the line has a semicolon as the first
10955 non-whitespace character; @code{make} interprets this to mean you left
10956 out the "target: prerequisite" section of a rule. @xref{Rule Syntax}.
10958 @item No rule to make target `@var{xxx}'.
10959 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
10960 This means that @code{make} decided it needed to build a target, but
10961 then couldn't find any instructions in the makefile on how to do that,
10962 either explicit or implicit (including in the default rules database).
10964 If you want that file to be built, you will need to add a rule to your
10965 makefile describing how that target can be built. Other possible
10966 sources of this problem are typos in the makefile (if that filename is
10967 wrong) or a corrupted source tree (if that file is not supposed to be
10968 built, but rather only a prerequisite).
10970 @item No targets specified and no makefile found. Stop.
10971 @itemx No targets. Stop.
10972 The former means that you didn't provide any targets to be built on the
10973 command line, and @code{make} couldn't find any makefiles to read in.
10974 The latter means that some makefile was found, but it didn't contain any
10975 default goal and none was given on the command line. GNU @code{make}
10976 has nothing to do in these situations.
10977 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
10979 @item Makefile `@var{xxx}' was not found.
10980 @itemx Included makefile `@var{xxx}' was not found.
10981 A makefile specified on the command line (first form) or included
10982 (second form) was not found.
10984 @item warning: overriding recipe for target `@var{xxx}'
10985 @itemx warning: ignoring old recipe for target `@var{xxx}'
10986 GNU @code{make} allows only one recipe to be specified per target
10987 (except for double-colon rules). If you give a recipe for a target
10988 which already has been defined to have one, this warning is issued and
10989 the second recipe will overwrite the first. @xref{Multiple Rules,
10990 ,Multiple Rules for One Target}.
10992 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
10993 This means that @code{make} detected a loop in the dependency graph:
10994 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
10995 prerequisites, etc., one of them depended on @var{xxx} again.
10997 @item Recursive variable `@var{xxx}' references itself (eventually). Stop.
10998 This means you've defined a normal (recursive) @code{make} variable
10999 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
11000 This is not allowed; either use simply-expanded variables (@code{:=}) or
11001 use the append operator (@code{+=}). @xref{Using Variables, ,How to Use
11004 @item Unterminated variable reference. Stop.
11005 This means you forgot to provide the proper closing parenthesis
11006 or brace in your variable or function reference.
11008 @item insufficient arguments to function `@var{xxx}'. Stop.
11009 This means you haven't provided the requisite number of arguments for
11010 this function. See the documentation of the function for a description
11011 of its arguments. @xref{Functions, ,Functions for Transforming Text}.
11013 @item missing target pattern. Stop.
11014 @itemx multiple target patterns. Stop.
11015 @itemx target pattern contains no `%'. Stop.
11016 @itemx mixed implicit and static pattern rules. Stop.
11017 These are generated for malformed static pattern rules. The first means
11018 there's no pattern in the target section of the rule; the second means
11019 there are multiple patterns in the target section; the third means
11020 the target doesn't contain a pattern character (@code{%}); and the
11021 fourth means that all three parts of the static pattern rule contain
11022 pattern characters (@code{%})--only the first two parts should.
11023 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
11025 @item warning: -jN forced in submake: disabling jobserver mode.
11026 This warning and the next are generated if @code{make} detects error
11027 conditions related to parallel processing on systems where
11028 sub-@code{make}s can communicate (@pxref{Options/Recursion,
11029 ,Communicating Options to a Sub-@code{make}}). This warning is
11030 generated if a recursive invocation of a @code{make} process is forced
11031 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
11032 than one). This could happen, for example, if you set the @code{MAKE}
11033 environment variable to @samp{make -j2}. In this case, the
11034 sub-@code{make} doesn't communicate with other @code{make} processes and
11035 will simply pretend it has two jobs of its own.
11037 @item warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
11038 In order for @code{make} processes to communicate, the parent will pass
11039 information to the child. Since this could result in problems if the
11040 child process isn't actually a @code{make}, the parent will only do this
11041 if it thinks the child is a @code{make}. The parent uses the normal
11042 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
11043 Variable Works}). If the makefile is constructed such that the parent
11044 doesn't know the child is a @code{make} process, then the child will
11045 receive only part of the information necessary. In this case, the child
11046 will generate this warning message and proceed with its build in a
11051 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
11052 @appendix Complex Makefile Example
11054 Here is the makefile for the GNU @code{tar} program. This is a
11055 moderately complex makefile.
11057 Because it is the first target, the default goal is @samp{all}. An
11058 interesting feature of this makefile is that @file{testpad.h} is a
11059 source file automatically created by the @code{testpad} program,
11060 itself compiled from @file{testpad.c}.
11062 If you type @samp{make} or @samp{make all}, then @code{make} creates
11063 the @file{tar} executable, the @file{rmt} daemon that provides
11064 remote tape access, and the @file{tar.info} Info file.
11066 If you type @samp{make install}, then @code{make} not only creates
11067 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
11070 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
11071 files, and the @file{tar}, @file{rmt}, @file{testpad},
11072 @file{testpad.h}, and @file{core} files.
11074 If you type @samp{make distclean}, then @code{make} not only removes
11075 the same files as does @samp{make clean} but also the
11076 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
11077 (Although it is not evident, this makefile (and
11078 @file{config.status}) is generated by the user with the
11079 @code{configure} program, which is provided in the @code{tar}
11080 distribution, but is not shown here.)
11082 If you type @samp{make realclean}, then @code{make} removes the same
11083 files as does @samp{make distclean} and also removes the Info files
11084 generated from @file{tar.texinfo}.
11086 In addition, there are targets @code{shar} and @code{dist} that create
11091 # Generated automatically from Makefile.in by configure.
11092 # Un*x Makefile for GNU tar program.
11093 # Copyright (C) 1991 Free Software Foundation, Inc.
11097 # This program is free software; you can redistribute
11098 # it and/or modify it under the terms of the GNU
11099 # General Public License @dots{}
11106 #### Start of system configuration section. ####
11111 # If you use gcc, you should either run the
11112 # fixincludes script that comes with it or else use
11113 # gcc with the -traditional option. Otherwise ioctl
11114 # calls will be compiled incorrectly on some systems.
11117 INSTALL = /usr/local/bin/install -c
11118 INSTALLDATA = /usr/local/bin/install -c -m 644
11121 # Things you might add to DEFS:
11122 # -DSTDC_HEADERS If you have ANSI C headers and
11124 # -DPOSIX If you have POSIX.1 headers and
11126 # -DBSD42 If you have sys/dir.h (unless
11127 # you use -DPOSIX), sys/file.h,
11128 # and st_blocks in `struct stat'.
11129 # -DUSG If you have System V/ANSI C
11130 # string and memory functions
11131 # and headers, sys/sysmacros.h,
11132 # fcntl.h, getcwd, no valloc,
11133 # and ndir.h (unless
11134 # you use -DDIRENT).
11135 # -DNO_MEMORY_H If USG or STDC_HEADERS but do not
11136 # include memory.h.
11137 # -DDIRENT If USG and you have dirent.h
11138 # instead of ndir.h.
11139 # -DSIGTYPE=int If your signal handlers
11140 # return int, not void.
11141 # -DNO_MTIO If you lack sys/mtio.h
11142 # (magtape ioctls).
11143 # -DNO_REMOTE If you do not have a remote shell
11145 # -DUSE_REXEC To use rexec for remote tape
11146 # operations instead of
11147 # forking rsh or remsh.
11148 # -DVPRINTF_MISSING If you lack vprintf function
11149 # (but have _doprnt).
11150 # -DDOPRNT_MISSING If you lack _doprnt function.
11151 # Also need to define
11152 # -DVPRINTF_MISSING.
11153 # -DFTIME_MISSING If you lack ftime system call.
11154 # -DSTRSTR_MISSING If you lack strstr function.
11155 # -DVALLOC_MISSING If you lack valloc function.
11156 # -DMKDIR_MISSING If you lack mkdir and
11157 # rmdir system calls.
11158 # -DRENAME_MISSING If you lack rename system call.
11159 # -DFTRUNCATE_MISSING If you lack ftruncate
11161 # -DV7 On Version 7 Unix (not
11162 # tested in a long time).
11163 # -DEMUL_OPEN3 If you lack a 3-argument version
11164 # of open, and want to emulate it
11165 # with system calls you do have.
11166 # -DNO_OPEN3 If you lack the 3-argument open
11167 # and want to disable the tar -k
11168 # option instead of emulating open.
11169 # -DXENIX If you have sys/inode.h
11170 # and need it 94 to be included.
11172 DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
11173 -DVPRINTF_MISSING -DBSD42
11174 # Set this to rtapelib.o unless you defined NO_REMOTE,
11175 # in which case make it empty.
11176 RTAPELIB = rtapelib.o
11178 DEF_AR_FILE = /dev/rmt8
11183 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
11184 -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
11185 -DDEFBLOCKING=$(DEFBLOCKING)
11190 prefix = /usr/local
11191 # Prefix for each installed program,
11192 # normally empty or `g'.
11195 # The directory to install tar in.
11196 bindir = $(prefix)/bin
11198 # The directory to install the info files in.
11199 infodir = $(prefix)/info
11202 #### End of system configuration section. ####
11204 SRC1 = tar.c create.c extract.c buffer.c \
11205 getoldopt.c update.c gnu.c mangle.c
11206 SRC2 = version.c list.c names.c diffarch.c \
11207 port.c wildmat.c getopt.c
11208 SRC3 = getopt1.c regex.c getdate.y
11209 SRCS = $(SRC1) $(SRC2) $(SRC3)
11210 OBJ1 = tar.o create.o extract.o buffer.o \
11211 getoldopt.o update.o gnu.o mangle.o
11212 OBJ2 = version.o list.o names.o diffarch.o \
11213 port.o wildmat.o getopt.o
11214 OBJ3 = getopt1.o regex.o getdate.o $(RTAPELIB)
11215 OBJS = $(OBJ1) $(OBJ2) $(OBJ3)
11217 AUX = README COPYING ChangeLog Makefile.in \
11218 makefile.pc configure configure.in \
11219 tar.texinfo tar.info* texinfo.tex \
11220 tar.h port.h open3.h getopt.h regex.h \
11221 rmt.h rmt.c rtapelib.c alloca.c \
11222 msd_dir.h msd_dir.c tcexparg.c \
11223 level-0 level-1 backup-specs testpad.c
11227 all: tar rmt tar.info
11231 $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
11236 $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
11240 tar.info: tar.texinfo
11241 makeinfo tar.texinfo
11247 $(INSTALL) tar $(bindir)/$(binprefix)tar
11248 -test ! -f rmt || $(INSTALL) rmt /etc/rmt
11249 $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
11253 $(OBJS): tar.h port.h testpad.h
11254 regex.o buffer.o tar.o: regex.h
11255 # getdate.y has 8 shift/reduce conflicts.
11265 $(CC) -o $@@ testpad.o
11276 rm -f *.o tar rmt testpad testpad.h core
11282 rm -f TAGS Makefile config.status
11287 realclean: distclean
11293 shar: $(SRCS) $(AUX)
11294 shar $(SRCS) $(AUX) | compress \
11295 > tar-`sed -e '/version_string/!d' \
11296 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11303 dist: $(SRCS) $(AUX)
11305 -e '/version_string/!d' \
11306 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11308 version.c` > .fname
11309 -rm -rf `cat .fname`
11311 ln $(SRCS) $(AUX) `cat .fname`
11312 tar chZf `cat .fname`.tar.Z `cat .fname`
11313 -rm -rf `cat .fname` .fname
11317 tar.zoo: $(SRCS) $(AUX)
11321 for X in $(SRCS) $(AUX) ; do \
11323 sed 's/$$/^M/' $$X \
11324 > tmp.dir/$$X ; done
11325 cd tmp.dir ; zoo aM ../tar.zoo *
11330 @node GNU Free Documentation License, Concept Index, Complex Makefile, Top
11331 @appendixsec GNU Free Documentation License
11332 @cindex FDL, GNU Free Documentation License
11335 @node Concept Index, Name Index, GNU Free Documentation License, Top
11336 @unnumbered Index of Concepts
11340 @node Name Index, , Concept Index, Top
11341 @unnumbered Index of Functions, Variables, & Directives