/usr/share/doc/make-doc/make_6.html is in make-doc 3.81-5.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This file documents the GNU make utility, which determines
automatically which pieces of a large program need to be recompiled,
and issues the commands to recompile them.
This is Edition 0.70, last updated 1 April 2006,
of The GNU Make Manual, for GNU make version 3.81.
Copyright C 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below. A copy of the
license is included in the section entitled "GNU Free Documentation
License."
(a) The FSF's Back-Cover Text is: "You have freedom to copy and modify
this GNU Manual, like GNU software. Copies published by the Free
Software Foundation raise funds for GNU development."
-->
<!-- Created on August, 17 2009 by texi2html 1.78 -->
<!--
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>GNU make: 6. How to Use Variables</title>
<meta name="description" content="GNU make: 6. How to Use Variables">
<meta name="keywords" content="GNU make: 6. How to Use Variables">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.78">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Using-Variables"></a>
<a name="SEC74"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="make_5.html#SEC73" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC75" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make_5.html#SEC56" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h1 class="chapter"> 6. How to Use Variables </h1>
<p>A <em>variable</em> is a name defined in a makefile to represent a string
of text, called the variable's <em>value</em>. These values are
substituted by explicit request into targets, prerequisites, commands,
and other parts of the makefile. (In some other versions of <code>make</code>,
variables are called <em>macros</em>.)
<a name="IDX240"></a>
</p>
<p>Variables and functions in all parts of a makefile are expanded when
read, except for the shell commands in rules, the right-hand sides of
variable definitions using ‘<samp>=</samp>’, and the bodies of variable
definitions using the <code>define</code> directive.
</p>
<p>Variables can represent lists of file names, options to pass to compilers,
programs to run, directories to look in for source files, directories to
write output in, or anything else you can imagine.
</p>
<p>A variable name may be any sequence of characters not containing ‘<samp>:</samp>’,
‘<samp>#</samp>’, ‘<samp>=</samp>’, or leading or trailing whitespace. However,
variable names containing characters other than letters, numbers, and
underscores should be avoided, as they may be given special meanings in the
future, and with some shells they cannot be passed through the environment to a
sub-<code>make</code>
(see section <a href="make_5.html#SEC69">Communicating Variables to a Sub-<code>make</code></a>).
</p>
<p>Variable names are case-sensitive. The names ‘<samp>foo</samp>’, ‘<samp>FOO</samp>’,
and ‘<samp>Foo</samp>’ all refer to different variables.
</p>
<p>It is traditional to use upper case letters in variable names, but we
recommend using lower case letters for variable names that serve internal
purposes in the makefile, and reserving upper case for parameters that
control implicit rules or for parameters that the user should override with
command options (see section <a href="make_9.html#SEC110">Overriding Variables</a>).
</p>
<p>A few variables have names that are a single punctuation character or
just a few characters. These are the <em>automatic variables</em>, and
they have particular specialized uses. See section <a href="make_10.html#SEC121">Automatic Variables</a>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#SEC75">6.1 Basics of Variable References</a></td><td> </td><td align="left" valign="top"> How to use the value of a variable.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC76">6.2 The Two Flavors of Variables</a></td><td> </td><td align="left" valign="top"> Variables come in two flavors.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC77">6.3 Advanced Features for Reference to Variables</a></td><td> </td><td align="left" valign="top"> Advanced features for referencing a variable.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC80">6.4 How Variables Get Their Values</a></td><td> </td><td align="left" valign="top"> All the ways variables get their values.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC81">6.5 Setting Variables</a></td><td> </td><td align="left" valign="top"> How to set a variable in the makefile.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC82">6.6 Appending More Text to Variables</a></td><td> </td><td align="left" valign="top"> How to append more text to the old value
of a variable.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC83">6.7 The <code>override</code> Directive</a></td><td> </td><td align="left" valign="top"> How to set a variable in the makefile even if
the user has set it with a command argument.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC84">6.8 Defining Variables Verbatim</a></td><td> </td><td align="left" valign="top"> An alternate way to set a variable
to a verbatim string.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC85">6.9 Variables from the Environment</a></td><td> </td><td align="left" valign="top"> Variable values can come from the environment.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC86">6.10 Target-specific Variable Values</a></td><td> </td><td align="left" valign="top"> Variable values can be defined on a per-target
basis.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC87">6.11 Pattern-specific Variable Values</a></td><td> </td><td align="left" valign="top"> Target-specific variable values can be applied
to a group of targets that match a pattern.
</td></tr>
</table>
<hr size="6">
<a name="Reference"></a>
<a name="SEC75"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC74" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC76" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.1 Basics of Variable References </h2>
<p>To substitute a variable's value, write a dollar sign followed by the name
of the variable in parentheses or braces: either ‘<samp>$(foo)</samp>’ or
‘<samp>${foo}</samp>’ is a valid reference to the variable <code>foo</code>. This
special significance of ‘<samp>$</samp>’ is why you must write ‘<samp>$$</samp>’ to have
the effect of a single dollar sign in a file name or command.
</p>
<p>Variable references can be used in any context: targets, prerequisites,
commands, most directives, and new variable values. Here is an
example of a common case, where a variable holds the names of all the
object files in a program:
</p>
<table><tr><td> </td><td><pre class="example">objects = program.o foo.o utils.o
program : $(objects)
cc -o program $(objects)
$(objects) : defs.h
</pre></td></tr></table>
<p>Variable references work by strict textual substitution. Thus, the rule
</p>
<table><tr><td> </td><td><pre class="example">foo = c
prog.o : prog.$(foo)
$(foo)$(foo) -$(foo) prog.$(foo)
</pre></td></tr></table>
<p>could be used to compile a C program ‘<tt>prog.c</tt>’. Since spaces before
the variable value are ignored in variable assignments, the value of
<code>foo</code> is precisely ‘<samp>c</samp>’. (Don't actually write your makefiles
this way!)
</p>
<p>A dollar sign followed by a character other than a dollar sign,
open-parenthesis or open-brace treats that single character as the
variable name. Thus, you could reference the variable <code>x</code> with
‘<samp>$x</samp>’. However, this practice is strongly discouraged, except in
the case of the automatic variables (see section <a href="make_10.html#SEC121">Automatic Variables</a>).
</p>
<hr size="6">
<a name="Flavors"></a>
<a name="SEC76"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC75" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC77" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.2 The Two Flavors of Variables </h2>
<p>There are two ways that a variable in GNU <code>make</code> can have a value;
we call them the two <em>flavors</em> of variables. The two flavors are
distinguished in how they are defined and in what they do when expanded.
</p>
<a name="IDX241"></a>
<p>The first flavor of variable is a <em>recursively expanded</em> variable.
Variables of this sort are defined by lines using ‘<samp>=</samp>’
(see section <a href="#SEC81">Setting Variables</a>) or by the <code>define</code> directive
(see section <a href="#SEC84">Defining Variables Verbatim</a>). The value you specify
is installed verbatim; if it contains references to other variables,
these references are expanded whenever this variable is substituted (in
the course of expanding some other string). When this happens, it is
called <em>recursive expansion</em>.
</p>
<p>For example,
</p>
<table><tr><td> </td><td><pre class="example">foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:;echo $(foo)
</pre></td></tr></table>
<p>will echo ‘<samp>Huh?</samp>’: ‘<samp>$(foo)</samp>’ expands to ‘<samp>$(bar)</samp>’ which
expands to ‘<samp>$(ugh)</samp>’ which finally expands to ‘<samp>Huh?</samp>’.
</p>
<p>This flavor of variable is the only sort supported by other versions of
<code>make</code>. It has its advantages and its disadvantages. An advantage
(most would say) is that:
</p>
<table><tr><td> </td><td><pre class="example">CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
</pre></td></tr></table>
<p>will do what was intended: when ‘<samp>CFLAGS</samp>’ is expanded in a command,
it will expand to ‘<samp>-Ifoo -Ibar -O</samp>’. A major disadvantage is that you
cannot append something on the end of a variable, as in
</p>
<table><tr><td> </td><td><pre class="example">CFLAGS = $(CFLAGS) -O
</pre></td></tr></table>
<p>because it will cause an infinite loop in the variable expansion.
(Actually <code>make</code> detects the infinite loop and reports an error.)
<a name="IDX242"></a>
<a name="IDX243"></a>
</p>
<p>Another disadvantage is that any functions
(see section <a href="make_8.html#SEC92">Functions for Transforming Text</a>)
referenced in the definition will be executed every time the variable is
expanded. This makes <code>make</code> run slower; worse, it causes the
<code>wildcard</code> and <code>shell</code> functions to give unpredictable results
because you cannot easily control when they are called, or even how many
times.
</p>
<p>To avoid all the problems and inconveniences of recursively expanded
variables, there is another flavor: simply expanded variables.
</p>
<a name="IDX244"></a>
<a name="IDX245"></a>
<a name="IDX246"></a>
<p><em>Simply expanded variables</em> are defined by lines using ‘<samp>:=</samp>’
(see section <a href="#SEC81">Setting Variables</a>).
The value of a simply expanded variable is scanned
once and for all, expanding any references to other variables and
functions, when the variable is defined. The actual value of the simply
expanded variable is the result of expanding the text that you write.
It does not contain any references to other variables; it contains their
values <em>as of the time this variable was defined</em>. Therefore,
</p>
<table><tr><td> </td><td><pre class="example">x := foo
y := $(x) bar
x := later
</pre></td></tr></table>
<p>is equivalent to
</p>
<table><tr><td> </td><td><pre class="example">y := foo bar
x := later
</pre></td></tr></table>
<p>When a simply expanded variable is referenced, its value is substituted
verbatim.
</p>
<p>Here is a somewhat more complicated example, illustrating the use of
‘<samp>:=</samp>’ in conjunction with the <code>shell</code> function.
(See section <a href="make_8.html#SEC103">The <code>shell</code> Function</a>.) This example
also shows use of the variable <code>MAKELEVEL</code>, which is changed
when it is passed down from level to level.
(See section <a href="make_5.html#SEC69">Communicating Variables to a Sub-<code>make</code></a>, for information about <code>MAKELEVEL</code>.)
</p>
<a name="IDX247"></a>
<a name="IDX248"></a>
<table><tr><td> </td><td><pre class="example">ifeq (0,${MAKELEVEL})
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif
</pre></td></tr></table>
<p>An advantage of this use of ‘<samp>:=</samp>’ is that a typical
`descend into a directory' command then looks like this:
</p>
<table><tr><td> </td><td><pre class="example">${subdirs}:
${MAKE} -C $@ all
</pre></td></tr></table>
<p>Simply expanded variables generally make complicated makefile programming
more predictable because they work like variables in most programming
languages. They allow you to redefine a variable using its own value (or
its value processed in some way by one of the expansion functions) and to
use the expansion functions much more efficiently
(see section <a href="make_8.html#SEC92">Functions for Transforming Text</a>).
</p>
<a name="IDX249"></a>
<a name="IDX250"></a>
<a name="IDX251"></a>
<p>You can also use them to introduce controlled leading whitespace into
variable values. Leading whitespace characters are discarded from your
input before substitution of variable references and function calls;
this means you can include leading spaces in a variable value by
protecting them with variable references, like this:
</p>
<table><tr><td> </td><td><pre class="example">nullstring :=
space := $(nullstring) # end of the line
</pre></td></tr></table>
<p>Here the value of the variable <code>space</code> is precisely one space. The
comment ‘<samp># end of the line</samp>’ is included here just for clarity.
Since trailing space characters are <em>not</em> stripped from variable
values, just a space at the end of the line would have the same effect
(but be rather hard to read). If you put whitespace at the end of a
variable value, it is a good idea to put a comment like that at the end
of the line to make your intent clear. Conversely, if you do <em>not</em>
want any whitespace characters at the end of your variable value, you
must remember not to put a random comment on the end of the line after
some whitespace, such as this:
</p>
<table><tr><td> </td><td><pre class="example">dir := /foo/bar # directory to put the frobs in
</pre></td></tr></table>
<p>Here the value of the variable <code>dir</code> is ‘<samp>/foo/bar </samp>’
(with four trailing spaces), which was probably not the intention.
(Imagine something like ‘<samp>$(dir)/file</samp>’ with this definition!)
</p>
<a name="IDX252"></a>
<a name="IDX253"></a>
<a name="IDX254"></a>
<p>There is another assignment operator for variables, ‘<samp>?=</samp>’. This
is called a conditional variable assignment operator, because it only
has an effect if the variable is not yet defined. This statement:
</p>
<table><tr><td> </td><td><pre class="example">FOO ?= bar
</pre></td></tr></table>
<p>is exactly equivalent to this
(see section <a href="make_8.html#SEC101">The <code>origin</code> Function</a>):
</p>
<table><tr><td> </td><td><pre class="example">ifeq ($(origin FOO), undefined)
FOO = bar
endif
</pre></td></tr></table>
<p>Note that a variable set to an empty value is still defined, so
‘<samp>?=</samp>’ will not set that variable.
</p>
<hr size="6">
<a name="Advanced"></a>
<a name="SEC77"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC76" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC78" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.3 Advanced Features for Reference to Variables </h2>
<p>This section describes some advanced features you can use to reference
variables in more flexible ways.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#SEC78">6.3.1 Substitution References</a></td><td> </td><td align="left" valign="top"> Referencing a variable with
substitutions on the value.
</td></tr>
<tr><td align="left" valign="top"><a href="#SEC79">6.3.2 Computed Variable Names</a></td><td> </td><td align="left" valign="top"> Computing the name of the variable to refer to.
</td></tr>
</table>
<hr size="6">
<a name="Substitution-Refs"></a>
<a name="SEC78"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC77" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC79" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC77" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection"> 6.3.1 Substitution References </h3>
<p>A <em>substitution reference</em> substitutes the value of a variable with
alterations that you specify. It has the form
‘<samp>$(<var>var</var>:<var>a</var>=<var>b</var>)</samp>’ (or
‘<samp>${<var>var</var>:<var>a</var>=<var>b</var>}</samp>’) and its meaning is to take the value
of the variable <var>var</var>, replace every <var>a</var> at the end of a word with
<var>b</var> in that value, and substitute the resulting string.
</p>
<p>When we say “at the end of a word”, we mean that <var>a</var> must appear
either followed by whitespace or at the end of the value in order to be
replaced; other occurrences of <var>a</var> in the value are unaltered. For
example:
</p>
<table><tr><td> </td><td><pre class="example">foo := a.o b.o c.o
bar := $(foo:.o=.c)
</pre></td></tr></table>
<p>sets ‘<samp>bar</samp>’ to ‘<samp>a.c b.c c.c</samp>’. See section <a href="#SEC81">Setting Variables</a>.
</p>
<p>A substitution reference is actually an abbreviation for use of the
<code>patsubst</code> expansion function (see section <a href="make_8.html#SEC94">Functions for String Substitution and Analysis</a>). We provide
substitution references as well as <code>patsubst</code> for compatibility with
other implementations of <code>make</code>.
</p>
<a name="IDX255"></a>
<p>Another type of substitution reference lets you use the full power of
the <code>patsubst</code> function. It has the same form
‘<samp>$(<var>var</var>:<var>a</var>=<var>b</var>)</samp>’ described above, except that now
<var>a</var> must contain a single ‘<samp>%</samp>’ character. This case is
equivalent to ‘<samp>$(patsubst <var>a</var>,<var>b</var>,$(<var>var</var>))</samp>’.
See section <a href="make_8.html#SEC94">Functions for String Substitution and Analysis</a>,
for a description of the <code>patsubst</code> function.
</p>
<table><tr><td> </td><td><pre class="example">For example:
foo := a.o b.o c.o
bar := $(foo:%.o=%.c)
</pre></td></tr></table>
<p>sets ‘<samp>bar</samp>’ to ‘<samp>a.c b.c c.c</samp>’.
</p>
<hr size="6">
<a name="Computed-Names"></a>
<a name="SEC79"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC78" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC80" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC77" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection"> 6.3.2 Computed Variable Names </h3>
<p>Computed variable names are a complicated concept needed only for
sophisticated makefile programming. For most purposes you need not
consider them, except to know that making a variable with a dollar sign
in its name might have strange results. However, if you are the type
that wants to understand everything, or you are actually interested in
what they do, read on.
</p>
<p>Variables may be referenced inside the name of a variable. This is
called a <em>computed variable name</em> or a <em>nested variable
reference</em>. For example,
</p>
<table><tr><td> </td><td><pre class="example">x = y
y = z
a := $($(x))
</pre></td></tr></table>
<p>defines <code>a</code> as ‘<samp>z</samp>’: the ‘<samp>$(x)</samp>’ inside ‘<samp>$($(x))</samp>’ expands
to ‘<samp>y</samp>’, so ‘<samp>$($(x))</samp>’ expands to ‘<samp>$(y)</samp>’ which in turn expands
to ‘<samp>z</samp>’. Here the name of the variable to reference is not stated
explicitly; it is computed by expansion of ‘<samp>$(x)</samp>’. The reference
‘<samp>$(x)</samp>’ here is nested within the outer variable reference.
</p>
<p>The previous example shows two levels of nesting, but any number of levels
is possible. For example, here are three levels:
</p>
<table><tr><td> </td><td><pre class="example">x = y
y = z
z = u
a := $($($(x)))
</pre></td></tr></table>
<p>Here the innermost ‘<samp>$(x)</samp>’ expands to ‘<samp>y</samp>’, so ‘<samp>$($(x))</samp>’
expands to ‘<samp>$(y)</samp>’ which in turn expands to ‘<samp>z</samp>’; now we have
‘<samp>$(z)</samp>’, which becomes ‘<samp>u</samp>’.
</p>
<p>References to recursively-expanded variables within a variable name are
reexpanded in the usual fashion. For example:
</p>
<table><tr><td> </td><td><pre class="example">x = $(y)
y = z
z = Hello
a := $($(x))
</pre></td></tr></table>
<p>defines <code>a</code> as ‘<samp>Hello</samp>’: ‘<samp>$($(x))</samp>’ becomes ‘<samp>$($(y))</samp>’
which becomes ‘<samp>$(z)</samp>’ which becomes ‘<samp>Hello</samp>’.
</p>
<p>Nested variable references can also contain modified references and
function invocations (see section <a href="make_8.html#SEC92">Functions for Transforming Text</a>),
just like any other reference.
For example, using the <code>subst</code> function
(see section <a href="make_8.html#SEC94">Functions for String Substitution and Analysis</a>):
</p>
<table><tr><td> </td><td><pre class="example">x = variable1
variable2 := Hello
y = $(subst 1,2,$(x))
z = y
a := $($($(z)))
</pre></td></tr></table>
<p>eventually defines <code>a</code> as ‘<samp>Hello</samp>’. It is doubtful that anyone
would ever want to write a nested reference as convoluted as this one, but
it works: ‘<samp>$($($(z)))</samp>’ expands to ‘<samp>$($(y))</samp>’ which becomes
‘<samp>$($(subst 1,2,$(x)))</samp>’. This gets the value ‘<samp>variable1</samp>’ from
<code>x</code> and changes it by substitution to ‘<samp>variable2</samp>’, so that the
entire string becomes ‘<samp>$(variable2)</samp>’, a simple variable reference
whose value is ‘<samp>Hello</samp>’.
</p>
<p>A computed variable name need not consist entirely of a single variable
reference. It can contain several variable references, as well as some
invariant text. For example,
</p>
<table><tr><td> </td><td><pre class="example">a_dirs := dira dirb
1_dirs := dir1 dir2
a_files := filea fileb
1_files := file1 file2
ifeq "$(use_a)" "yes"
a1 := a
else
a1 := 1
endif
ifeq "$(use_dirs)" "yes"
df := dirs
else
df := files
endif
dirs := $($(a1)_$(df))
</pre></td></tr></table>
<p>will give <code>dirs</code> the same value as <code>a_dirs</code>, <code>1_dirs</code>,
<code>a_files</code> or <code>1_files</code> depending on the settings of <code>use_a</code>
and <code>use_dirs</code>.
</p>
<p>Computed variable names can also be used in substitution references:
</p>
<table><tr><td> </td><td><pre class="example">a_objects := a.o b.o c.o
1_objects := 1.o 2.o 3.o
sources := $($(a1)_objects:.o=.c)
</pre></td></tr></table>
<p>defines <code>sources</code> as either ‘<samp>a.c b.c c.c</samp>’ or ‘<samp>1.c 2.c 3.c</samp>’,
depending on the value of <code>a1</code>.
</p>
<p>The only restriction on this sort of use of nested variable references
is that they cannot specify part of the name of a function to be called.
This is because the test for a recognized function name is done before
the expansion of nested references. For example,
</p>
<table><tr><td> </td><td><pre class="example">ifdef do_sort
func := sort
else
func := strip
endif
bar := a d b g q c
foo := $($(func) $(bar))
</pre></td></tr></table>
<p>attempts to give ‘<samp>foo</samp>’ the value of the variable ‘<samp>sort a d b g
q c</samp>’ or ‘<samp>strip a d b g q c</samp>’, rather than giving ‘<samp>a d b g q c</samp>’
as the argument to either the <code>sort</code> or the <code>strip</code> function.
This restriction could be removed in the future if that change is shown
to be a good idea.
</p>
<p>You can also use computed variable names in the left-hand side of a
variable assignment, or in a <code>define</code> directive, as in:
</p>
<table><tr><td> </td><td><pre class="example">dir = foo
$(dir)_sources := $(wildcard $(dir)/*.c)
define $(dir)_print
lpr $($(dir)_sources)
endef
</pre></td></tr></table>
<p>This example defines the variables ‘<samp>dir</samp>’, ‘<samp>foo_sources</samp>’, and
‘<samp>foo_print</samp>’.
</p>
<p>Note that <em>nested variable references</em> are quite different from
<em>recursively expanded variables</em>
(see section <a href="#SEC76">The Two Flavors of Variables</a>), though both are
used together in complex ways when doing makefile programming.
</p>
<hr size="6">
<a name="Values"></a>
<a name="SEC80"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC79" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC81" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.4 How Variables Get Their Values </h2>
<p>Variables can get values in several different ways:
</p>
<ul>
<li>
You can specify an overriding value when you run <code>make</code>.
See section <a href="make_9.html#SEC110">Overriding Variables</a>.
</li><li>
You can specify a value in the makefile, either
with an assignment (see section <a href="#SEC81">Setting Variables</a>) or with a
verbatim definition (see section <a href="#SEC84">Defining Variables Verbatim</a>).
</li><li>
Variables in the environment become <code>make</code> variables.
See section <a href="#SEC85">Variables from the Environment</a>.
</li><li>
Several <em>automatic</em> variables are given new values for each rule.
Each of these has a single conventional use.
See section <a href="make_10.html#SEC121">Automatic Variables</a>.
</li><li>
Several variables have constant initial values.
See section <a href="make_10.html#SEC116">Variables Used by Implicit Rules</a>.
</li></ul>
<hr size="6">
<a name="Setting"></a>
<a name="SEC81"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC80" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC82" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.5 Setting Variables </h2>
<p>To set a variable from the makefile, write a line starting with the
variable name followed by ‘<samp>=</samp>’ or ‘<samp>:=</samp>’. Whatever follows the
‘<samp>=</samp>’ or ‘<samp>:=</samp>’ on the line becomes the value. For example,
</p>
<table><tr><td> </td><td><pre class="example">objects = main.o foo.o bar.o utils.o
</pre></td></tr></table>
<p>defines a variable named <code>objects</code>. Whitespace around the variable
name and immediately after the ‘<samp>=</samp>’ is ignored.
</p>
<p>Variables defined with ‘<samp>=</samp>’ are <em>recursively expanded</em> variables.
Variables defined with ‘<samp>:=</samp>’ are <em>simply expanded</em> variables; these
definitions can contain variable references which will be expanded before
the definition is made. See section <a href="#SEC76">The Two Flavors of Variables</a>.
</p>
<p>The variable name may contain function and variable references, which
are expanded when the line is read to find the actual variable name to use.
</p>
<p>There is no limit on the length of the value of a variable except the
amount of swapping space on the computer. When a variable definition is
long, it is a good idea to break it into several lines by inserting
backslash-newline at convenient places in the definition. This will not
affect the functioning of <code>make</code>, but it will make the makefile easier
to read.
</p>
<p>Most variable names are considered to have the empty string as a value if
you have never set them. Several variables have built-in initial values
that are not empty, but you can set them in the usual ways
(see section <a href="make_10.html#SEC116">Variables Used by Implicit Rules</a>).
Several special variables are set
automatically to a new value for each rule; these are called the
<em>automatic</em> variables (see section <a href="make_10.html#SEC121">Automatic Variables</a>).
</p>
<p>If you'd like a variable to be set to a value only if it's not already
set, then you can use the shorthand operator ‘<samp>?=</samp>’ instead of
‘<samp>=</samp>’. These two settings of the variable ‘<samp>FOO</samp>’ are identical
(see section <a href="make_8.html#SEC101">The <code>origin</code> Function</a>):
</p>
<table><tr><td> </td><td><pre class="example">FOO ?= bar
</pre></td></tr></table>
<p>and
</p>
<table><tr><td> </td><td><pre class="example">ifeq ($(origin FOO), undefined)
FOO = bar
endif
</pre></td></tr></table>
<hr size="6">
<a name="Appending"></a>
<a name="SEC82"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC81" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC83" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.6 Appending More Text to Variables </h2>
<p>Often it is useful to add more text to the value of a variable already defined.
You do this with a line containing ‘<samp>+=</samp>’, like this:
</p>
<table><tr><td> </td><td><pre class="example">objects += another.o
</pre></td></tr></table>
<p>This takes the value of the variable <code>objects</code>, and adds the text
‘<samp>another.o</samp>’ to it (preceded by a single space). Thus:
</p>
<table><tr><td> </td><td><pre class="example">objects = main.o foo.o bar.o utils.o
objects += another.o
</pre></td></tr></table>
<p>sets <code>objects</code> to ‘<samp>main.o foo.o bar.o utils.o another.o</samp>’.
</p>
<p>Using ‘<samp>+=</samp>’ is similar to:
</p>
<table><tr><td> </td><td><pre class="example">objects = main.o foo.o bar.o utils.o
objects := $(objects) another.o
</pre></td></tr></table>
<p>but differs in ways that become important when you use more complex values.
</p>
<p>When the variable in question has not been defined before, ‘<samp>+=</samp>’
acts just like normal ‘<samp>=</samp>’: it defines a recursively-expanded
variable. However, when there <em>is</em> a previous definition, exactly
what ‘<samp>+=</samp>’ does depends on what flavor of variable you defined
originally. See section <a href="#SEC76">The Two Flavors of Variables</a>, for an
explanation of the two flavors of variables.
</p>
<p>When you add to a variable's value with ‘<samp>+=</samp>’, <code>make</code> acts
essentially as if you had included the extra text in the initial
definition of the variable. If you defined it first with ‘<samp>:=</samp>’,
making it a simply-expanded variable, ‘<samp>+=</samp>’ adds to that
simply-expanded definition, and expands the new text before appending it
to the old value just as ‘<samp>:=</samp>’ does
(see <a href="#SEC81">Setting Variables</a>, for a full explanation of ‘<samp>:=</samp>’).
In fact,
</p>
<table><tr><td> </td><td><pre class="example">variable := value
variable += more
</pre></td></tr></table>
<p>is exactly equivalent to:
</p>
<table><tr><td> </td><td><pre class="example">variable := value
variable := $(variable) more
</pre></td></tr></table>
<p>On the other hand, when you use ‘<samp>+=</samp>’ with a variable that you defined
first to be recursively-expanded using plain ‘<samp>=</samp>’, <code>make</code> does
something a bit different. Recall that when you define a
recursively-expanded variable, <code>make</code> does not expand the value you set
for variable and function references immediately. Instead it stores the text
verbatim, and saves these variable and function references to be expanded
later, when you refer to the new variable (see section <a href="#SEC76">The Two Flavors of Variables</a>). When you use ‘<samp>+=</samp>’ on a recursively-expanded variable,
it is this unexpanded text to which <code>make</code> appends the new text you
specify.
</p>
<table><tr><td> </td><td><pre class="example">variable = value
variable += more
</pre></td></tr></table>
<p>is roughly equivalent to:
</p>
<table><tr><td> </td><td><pre class="example">temp = value
variable = $(temp) more
</pre></td></tr></table>
<p>except that of course it never defines a variable called <code>temp</code>.
The importance of this comes when the variable's old value contains
variable references. Take this common example:
</p>
<table><tr><td> </td><td><pre class="example">CFLAGS = $(includes) -O
…
CFLAGS += -pg # enable profiling
</pre></td></tr></table>
<p>The first line defines the <code>CFLAGS</code> variable with a reference to another
variable, <code>includes</code>. (<code>CFLAGS</code> is used by the rules for C
compilation; see section <a href="make_10.html#SEC115">Catalogue of Implicit Rules</a>.)
Using ‘<samp>=</samp>’ for the definition makes <code>CFLAGS</code> a recursively-expanded
variable, meaning ‘<samp>$(includes) -O</samp>’ is <em>not</em> expanded when
<code>make</code> processes the definition of <code>CFLAGS</code>. Thus, <code>includes</code>
need not be defined yet for its value to take effect. It only has to be
defined before any reference to <code>CFLAGS</code>. If we tried to append to the
value of <code>CFLAGS</code> without using ‘<samp>+=</samp>’, we might do it like this:
</p>
<table><tr><td> </td><td><pre class="example">CFLAGS := $(CFLAGS) -pg # enable profiling
</pre></td></tr></table>
<p>This is pretty close, but not quite what we want. Using ‘<samp>:=</samp>’
redefines <code>CFLAGS</code> as a simply-expanded variable; this means
<code>make</code> expands the text ‘<samp>$(CFLAGS) -pg</samp>’ before setting the
variable. If <code>includes</code> is not yet defined, we get ‘<samp> -O
-pg</samp>’, and a later definition of <code>includes</code> will have no effect.
Conversely, by using ‘<samp>+=</samp>’ we set <code>CFLAGS</code> to the
<em>unexpanded</em> value ‘<samp>$(includes) -O -pg</samp>’. Thus we preserve
the reference to <code>includes</code>, so if that variable gets defined at
any later point, a reference like ‘<samp>$(CFLAGS)</samp>’ still uses its
value.
</p>
<hr size="6">
<a name="Override-Directive"></a>
<a name="SEC83"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC82" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC84" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.7 The <code>override</code> Directive </h2>
<p>If a variable has been set with a command argument
(see section <a href="make_9.html#SEC110">Overriding Variables</a>),
then ordinary assignments in the makefile are ignored. If you want to set
the variable in the makefile even though it was set with a command
argument, you can use an <code>override</code> directive, which is a line that
looks like this:
</p>
<table><tr><td> </td><td><pre class="example">override <var>variable</var> = <var>value</var>
</pre></td></tr></table>
<p>or
</p>
<table><tr><td> </td><td><pre class="example">override <var>variable</var> := <var>value</var>
</pre></td></tr></table>
<p>To append more text to a variable defined on the command line, use:
</p>
<table><tr><td> </td><td><pre class="example">override <var>variable</var> += <var>more text</var>
</pre></td></tr></table>
<p>See section <a href="#SEC82">Appending More Text to Variables</a>.
</p>
<p>The <code>override</code> directive was not invented for escalation in the war
between makefiles and command arguments. It was invented so you can alter
and add to values that the user specifies with command arguments.
</p>
<p>For example, suppose you always want the ‘<samp>-g</samp>’ switch when you run the
C compiler, but you would like to allow the user to specify the other
switches with a command argument just as usual. You could use this
<code>override</code> directive:
</p>
<table><tr><td> </td><td><pre class="example">override CFLAGS += -g
</pre></td></tr></table>
<p>You can also use <code>override</code> directives with <code>define</code> directives.
This is done as you might expect:
</p>
<table><tr><td> </td><td><pre class="example">override define foo
bar
endef
</pre></td></tr></table>
<p>See section <a href="#SEC84">Defining Variables Verbatim</a>.
</p>
<hr size="6">
<a name="Defining"></a>
<a name="SEC84"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC83" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC85" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.8 Defining Variables Verbatim </h2>
<p>Another way to set the value of a variable is to use the <code>define</code>
directive. This directive has an unusual syntax which allows newline
characters to be included in the value, which is convenient for defining
both canned sequences of commands
(see section <a href="make_5.html#SEC72">Defining Canned Command Sequences</a>), and also
sections of makefile syntax to use with <code>eval</code> (see section <a href="make_8.html#SEC100">The <code>eval</code> Function</a>).
</p>
<p>The <code>define</code> directive is followed on the same line by the name of the
variable and nothing more. The value to give the variable appears on the
following lines. The end of the value is marked by a line containing just
the word <code>endef</code>. Aside from this difference in syntax, <code>define</code>
works just like ‘<samp>=</samp>’: it creates a recursively-expanded variable
(see section <a href="#SEC76">The Two Flavors of Variables</a>).
The variable name may contain function and variable references, which
are expanded when the directive is read to find the actual variable name
to use.
</p>
<p>You may nest <code>define</code> directives: <code>make</code> will keep track of
nested directives and report an error if they are not all properly
closed with <code>endef</code>. Note that lines beginning with tab
characters are considered part of a command script, so any
<code>define</code> or <code>endef</code> strings appearing on such a line will
not be considered <code>make</code> operators.
</p>
<table><tr><td> </td><td><pre class="example">define two-lines
echo foo
echo $(bar)
endef
</pre></td></tr></table>
<p>The value in an ordinary assignment cannot contain a newline; but the
newlines that separate the lines of the value in a <code>define</code> become
part of the variable's value (except for the final newline which precedes
the <code>endef</code> and is not considered part of the value).
</p>
<p>When used in a command script, the previous example is functionally
equivalent to this:
</p>
<table><tr><td> </td><td><pre class="example">two-lines = echo foo; echo $(bar)
</pre></td></tr></table>
<p>since two commands separated by semicolon behave much like two separate
shell commands. However, note that using two separate lines means
<code>make</code> will invoke the shell twice, running an independent subshell
for each line. See section <a href="make_5.html#SEC61">Command Execution</a>.
</p>
<p>If you want variable definitions made with <code>define</code> to take
precedence over command-line variable definitions, you can use the
<code>override</code> directive together with <code>define</code>:
</p>
<table><tr><td> </td><td><pre class="example">override define two-lines
foo
$(bar)
endef
</pre></td></tr></table>
<p>See section <a href="#SEC83">The <code>override</code> Directive</a>.
</p>
<hr size="6">
<a name="Environment"></a>
<a name="SEC85"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC84" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC86" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.9 Variables from the Environment </h2>
<p>Variables in <code>make</code> can come from the environment in which
<code>make</code> is run. Every environment variable that <code>make</code> sees
when it starts up is transformed into a <code>make</code> variable with the
same name and value. However, an explicit assignment in the makefile,
or with a command argument, overrides the environment. (If the
‘<samp>-e</samp>’ flag is specified, then values from the environment override
assignments in the makefile. See section <a href="make_9.html#SEC112">Summary of Options</a>. But this is not recommended practice.)
</p>
<p>Thus, by setting the variable <code>CFLAGS</code> in your environment, you can
cause all C compilations in most makefiles to use the compiler switches you
prefer. This is safe for variables with standard or conventional meanings
because you know that no makefile will use them for other things. (Note
this is not totally reliable; some makefiles set <code>CFLAGS</code> explicitly
and therefore are not affected by the value in the environment.)
</p>
<p>When <code>make</code> runs a command script, variables defined in the
makefile are placed into the environment of that command. This allows
you to pass values to sub-<code>make</code> invocations (see section <a href="make_5.html#SEC67">Recursive Use of <code>make</code></a>). By default, only variables that came
from the environment or the command line are passed to recursive
invocations. You can use the <code>export</code> directive to pass other
variables. See section <a href="make_5.html#SEC69">Communicating Variables to a Sub-<code>make</code></a>, for full details.
</p>
<p>Other use of variables from the environment is not recommended. It is not
wise for makefiles to depend for their functioning on environment variables
set up outside their control, since this would cause different users to get
different results from the same makefile. This is against the whole
purpose of most makefiles.
</p>
<a name="IDX256"></a>
<p>Such problems would be especially likely with the variable
<code>SHELL</code>, which is normally present in the environment to specify
the user's choice of interactive shell. It would be very undesirable
for this choice to affect <code>make</code>; so, <code>make</code> handles the
<code>SHELL</code> environment variable in a special way; see <a href="make_5.html#SEC62">Choosing the Shell</a>.
</p>
<hr size="6">
<a name="Target_002dspecific"></a>
<a name="SEC86"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC85" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#SEC87" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.10 Target-specific Variable Values </h2>
<p>Variable values in <code>make</code> are usually global; that is, they are the
same regardless of where they are evaluated (unless they're reset, of
course). One exception to that is automatic variables
(see section <a href="make_10.html#SEC121">Automatic Variables</a>).
</p>
<p>The other exception is <em>target-specific variable values</em>. This
feature allows you to define different values for the same variable,
based on the target that <code>make</code> is currently building. As with
automatic variables, these values are only available within the context
of a target's command script (and in other target-specific assignments).
</p>
<p>Set a target-specific variable value like this:
</p>
<table><tr><td> </td><td><pre class="example"><var>target</var> … : <var>variable-assignment</var>
</pre></td></tr></table>
<p>or like this:
</p>
<table><tr><td> </td><td><pre class="example"><var>target</var> … : override <var>variable-assignment</var>
</pre></td></tr></table>
<p>or like this:
</p>
<table><tr><td> </td><td><pre class="example"><var>target</var> … : export <var>variable-assignment</var>
</pre></td></tr></table>
<p>Multiple <var>target</var> values create a target-specific variable value for
each member of the target list individually.
</p>
<p>The <var>variable-assignment</var> can be any valid form of assignment;
recursive (‘<samp>=</samp>’), static (‘<samp>:=</samp>’), appending (‘<samp>+=</samp>’), or
conditional (‘<samp>?=</samp>’). All variables that appear within the
<var>variable-assignment</var> are evaluated within the context of the
target: thus, any previously-defined target-specific variable values
will be in effect. Note that this variable is actually distinct from
any “global” value: the two variables do not have to have the same
flavor (recursive vs. static).
</p>
<p>Target-specific variables have the same priority as any other makefile
variable. Variables provided on the command-line (and in the
environment if the ‘<samp>-e</samp>’ option is in force) will take precedence.
Specifying the <code>override</code> directive will allow the target-specific
variable value to be preferred.
</p>
<p>There is one more special feature of target-specific variables: when
you define a target-specific variable that variable value is also in
effect for all prerequisites of this target, and all their
prerequisites, etc. (unless those prerequisites override that variable
with their own target-specific variable value). So, for example, a
statement like this:
</p>
<table><tr><td> </td><td><pre class="example">prog : CFLAGS = -g
prog : prog.o foo.o bar.o
</pre></td></tr></table>
<p>will set <code>CFLAGS</code> to ‘<samp>-g</samp>’ in the command script for
‘<tt>prog</tt>’, but it will also set <code>CFLAGS</code> to ‘<samp>-g</samp>’ in the
command scripts that create ‘<tt>prog.o</tt>’, ‘<tt>foo.o</tt>’, and
‘<tt>bar.o</tt>’, and any command scripts which create their
prerequisites.
</p>
<p>Be aware that a given prerequisite will only be built once per
invocation of make, at most. If the same file is a prerequisite of
multiple targets, and each of those targets has a different value for
the same target-specific variable, then the first target to be built
will cause that prerequisite to be built and the prerequisite will
inherit the target-specific value from the first target. It will
ignore the target-specific values from any other targets.
</p>
<hr size="6">
<a name="Pattern_002dspecific"></a>
<a name="SEC87"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC86" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#SEC74" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section"> 6.11 Pattern-specific Variable Values </h2>
<p>In addition to target-specific variable values
(see section <a href="#SEC86">Target-specific Variable Values</a>), GNU
<code>make</code> supports pattern-specific variable values. In this form,
the variable is defined for any target that matches the pattern
specified. If a target matches more than one pattern, all the
matching pattern-specific variables are interpreted in the order in
which they were defined in the makefile, and collected together into
one set. Variables defined in this way are searched after any
target-specific variables defined explicitly for that target, and
before target-specific variables defined for the parent target.
</p>
<p>Set a pattern-specific variable value like this:
</p>
<table><tr><td> </td><td><pre class="example"><var>pattern</var> … : <var>variable-assignment</var>
</pre></td></tr></table>
<p>or like this:
</p>
<table><tr><td> </td><td><pre class="example"><var>pattern</var> … : override <var>variable-assignment</var>
</pre></td></tr></table>
<p>where <var>pattern</var> is a %-pattern. As with target-specific variable
values, multiple <var>pattern</var> values create a pattern-specific variable
value for each pattern individually. The <var>variable-assignment</var> can
be any valid form of assignment. Any command-line variable setting will
take precedence, unless <code>override</code> is specified.
</p>
<p>For example:
</p>
<table><tr><td> </td><td><pre class="example">%.o : CFLAGS = -O
</pre></td></tr></table>
<p>will assign <code>CFLAGS</code> the value of ‘<samp>-O</samp>’ for all targets
matching the pattern <code>%.o</code>.
</p>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#SEC74" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="make_7.html#SEC88" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="make.html#SEC_Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="make_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="make_19.html#SEC148" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="make_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>Manoj Srivastava</em> on <em>August, 17 2009</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.78</em></a>.
</font>
<br>
</p>
</body>
</html>
|