This file is indexed.

/usr/share/doc/make-doc/make.html/Using-Variables.html is in make-doc 4.2.1-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
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/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.74, last updated 21 May 2016,
of The GNU Make Manual, for GNU make version 4.2.1.

Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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.3 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 the freedom to copy and
modify this GNU manual.  Buying copies from the FSF supports it in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Using Variables (GNU make)</title>

<meta name="description" content="Using Variables (GNU make)">
<meta name="keywords" content="Using Variables (GNU make)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html#Top" rel="up" title="Top">
<link href="Conditionals.html#Conditionals" rel="next" title="Conditionals">
<link href="Recipes.html#Empty-Recipes" rel="prev" title="Empty Recipes">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en">
<a name="Using-Variables"></a>
<div class="header">
<p>
Next: <a href="Conditionals.html#Conditionals" accesskey="n" rel="next">Conditionals</a>, Previous: <a href="Recipes.html#Recipes" accesskey="p" rel="prev">Recipes</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="How-to-Use-Variables"></a>
<h2 class="chapter">6 How to Use Variables</h2>
<a name="index-variable"></a>
<a name="index-value"></a>
<a name="index-recursive-variable-expansion"></a>
<a name="index-simple-variable-expansion"></a>

<p>A <em>variable</em> is a name defined in a makefile to represent a string
of text, called the variable&rsquo;s <em>value</em>.  These values are
substituted by explicit request into targets, prerequisites, recipes,
and other parts of the makefile.  (In some other versions of <code>make</code>,
variables are called <em>macros</em>.)
<a name="index-macro"></a>
</p>
<p>Variables and functions in all parts of a makefile are expanded when
read, except for in recipes, the right-hand sides of variable
definitions using &lsquo;<samp>=</samp>&rsquo;, 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
&lsquo;<samp>:</samp>&rsquo;, &lsquo;<samp>#</samp>&rsquo;, &lsquo;<samp>=</samp>&rsquo;, or whitespace.  However, variable names
containing characters other than letters, numbers, and underscores
should be considered carefully, as in some shells they cannot be
passed through the environment to a sub-<code>make</code>
(see <a href="Recipes.html#Variables_002fRecursion">Communicating Variables to a
Sub-<code>make</code></a>).  Variable names beginning with &lsquo;<samp>.</samp>&rsquo; and an
uppercase letter may be given special meaning in future versions of
<code>make</code>.
</p>
<p>Variable names are case-sensitive.  The names &lsquo;<samp>foo</samp>&rsquo;, &lsquo;<samp>FOO</samp>&rsquo;,
and &lsquo;<samp>Foo</samp>&rsquo; 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 <a href="Running.html#Overriding">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 <a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Reference" accesskey="1">Reference</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">How to use the value of a variable.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Flavors" accesskey="2">Flavors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Variables come in two flavors.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Advanced" accesskey="3">Advanced</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Advanced features for referencing a variable.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Values" accesskey="4">Values</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">All the ways variables get their values.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Setting" accesskey="5">Setting</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">How to set a variable in the makefile.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Appending" accesskey="6">Appending</a>:</td><td>&nbsp;&nbsp;</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">&bull; <a href="#Override-Directive" accesskey="7">Override Directive</a>:</td><td>&nbsp;&nbsp;</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">&bull; <a href="#Multi_002dLine" accesskey="8">Multi-Line</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">An alternate way to set a variable
                                  to a multi-line string.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Undefine-Directive" accesskey="9">Undefine Directive</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">How to undefine a variable so that it appears
                                  as if it was never set.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Environment">Environment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Variable values can come from the environment.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Target_002dspecific">Target-specific</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Variable values can be defined on a per-target
                                  basis.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Pattern_002dspecific">Pattern-specific</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Target-specific variable values can be applied
                                  to a group of targets that match a pattern.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Suppressing-Inheritance">Suppressing Inheritance</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Suppress inheritance of variables.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Special-Variables">Special Variables</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Variables with special meaning or behavior.
</td></tr>
</table>

<hr>
<a name="Reference"></a>
<div class="header">
<p>
Next: <a href="#Flavors" accesskey="n" rel="next">Flavors</a>, Previous: <a href="#Using-Variables" accesskey="p" rel="prev">Using Variables</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Basics-of-Variable-References"></a>
<h3 class="section">6.1 Basics of Variable References</h3>
<a name="index-variables_002c-how-to-reference"></a>
<a name="index-reference-to-variables"></a>
<a name="index-_0024_002c-in-variable-reference"></a>
<a name="index-dollar-sign-_0028_0024_0029_002c-in-variable-reference"></a>

<p>To substitute a variable&rsquo;s value, write a dollar sign followed by the name
of the variable in parentheses or braces: either &lsquo;<samp>$(foo)</samp>&rsquo; or
&lsquo;<samp>${foo}</samp>&rsquo; is a valid reference to the variable <code>foo</code>.  This
special significance of &lsquo;<samp>$</samp>&rsquo; is why you must write &lsquo;<samp>$$</samp>&rsquo; to have
the effect of a single dollar sign in a file name or recipe.
</p>
<p>Variable references can be used in any context: targets, prerequisites,
recipes, 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>
<div class="example">
<pre class="example">objects = program.o foo.o utils.o
program : $(objects)
        cc -o program $(objects)

$(objects) : defs.h
</pre></div>

<p>Variable references work by strict textual substitution.  Thus, the rule
</p>
<div class="example">
<pre class="example">foo = c
prog.o : prog.$(foo)
        $(foo)$(foo) -$(foo) prog.$(foo)
</pre></div>

<p>could be used to compile a C program <samp>prog.c</samp>.  Since spaces before
the variable value are ignored in variable assignments, the value of
<code>foo</code> is precisely &lsquo;<samp>c</samp>&rsquo;.  (Don&rsquo;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
&lsquo;<samp>$x</samp>&rsquo;.  However, this practice is strongly discouraged, except in
the case of the automatic variables (see <a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>).
</p>
<hr>
<a name="Flavors"></a>
<div class="header">
<p>
Next: <a href="#Advanced" accesskey="n" rel="next">Advanced</a>, Previous: <a href="#Reference" accesskey="p" rel="prev">Reference</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-Two-Flavors-of-Variables"></a>
<h3 class="section">6.2 The Two Flavors of Variables</h3>
<a name="index-flavors-of-variables"></a>
<a name="index-recursive-variable-expansion-1"></a>
<a name="index-variables_002c-flavors"></a>
<a name="index-recursively-expanded-variables"></a>
<a name="index-variables_002c-recursively-expanded"></a>

<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="index-_003d"></a>
<p>The first flavor of variable is a <em>recursively expanded</em> variable.
Variables of this sort are defined by lines using &lsquo;<samp>=</samp>&rsquo;
(see <a href="#Setting">Setting Variables</a>) or by the <code>define</code> directive
(see <a href="#Multi_002dLine">Defining Multi-Line Variables</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>
<div class="example">
<pre class="example">foo = $(bar)
bar = $(ugh)
ugh = Huh?

all:;echo $(foo)
</pre></div>

<p>will echo &lsquo;<samp>Huh?</samp>&rsquo;: &lsquo;<samp>$(foo)</samp>&rsquo; expands to &lsquo;<samp>$(bar)</samp>&rsquo; which
expands to &lsquo;<samp>$(ugh)</samp>&rsquo; which finally expands to &lsquo;<samp>Huh?</samp>&rsquo;.
</p>
<p>This flavor of variable is the only sort supported by most other
versions of <code>make</code>.  It has its advantages and its disadvantages.
An advantage (most would say) is that:
</p>
<div class="example">
<pre class="example">CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
</pre></div>

<p>will do what was intended: when &lsquo;<samp>CFLAGS</samp>&rsquo; is expanded in a recipe,
it will expand to &lsquo;<samp>-Ifoo -Ibar -O</samp>&rsquo;.  A major disadvantage is that you
cannot append something on the end of a variable, as in
</p>
<div class="example">
<pre class="example">CFLAGS = $(CFLAGS) -O
</pre></div>

<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="index-loops-in-variable-expansion"></a>
<a name="index-variables_002c-loops-in-expansion"></a>
</p>
<p>Another disadvantage is that any functions
(see <a href="Functions.html#Functions">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="index-simply-expanded-variables"></a>
<a name="index-variables_002c-simply-expanded"></a>
<a name="index-_003a_003d"></a>
<a name="index-_003a_003a_003d"></a>
<p><em>Simply expanded variables</em> are defined by lines using &lsquo;<samp>:=</samp>&rsquo;
or &lsquo;<samp>::=</samp>&rsquo; (see <a href="#Setting">Setting Variables</a>).  Both forms are
equivalent in GNU <code>make</code>; however only the &lsquo;<samp>::=</samp>&rsquo; form is
described by the POSIX standard (support for &lsquo;<samp>::=</samp>&rsquo; was added to
the POSIX standard in 2012, so older versions of <code>make</code> won&rsquo;t
accept this form either).
</p>
<p>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>
<div class="example">
<pre class="example">x := foo
y := $(x) bar
x := later
</pre></div>

<p>is equivalent to
</p>
<div class="example">
<pre class="example">y := foo bar
x := later
</pre></div>

<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
&lsquo;<samp>:=</samp>&rsquo; in conjunction with the <code>shell</code> function.
(See <a href="Functions.html#Shell-Function">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 <a href="Recipes.html#Variables_002fRecursion">Communicating Variables to a
Sub-<code>make</code></a>, for information about <code>MAKELEVEL</code>.)
</p>
<a name="index-MAKELEVEL-1"></a>
<a name="index-MAKE-1"></a>
<div class="example">
<pre class="example">ifeq (0,${MAKELEVEL})
whoami    := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif
</pre></div>

<p>An advantage of this use of &lsquo;<samp>:=</samp>&rsquo; is that a typical
&lsquo;descend into a directory&rsquo; recipe then looks like this:
</p>
<div class="example">
<pre class="example">${subdirs}:
        ${MAKE} -C $@ all
</pre></div>

<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 <a href="Functions.html#Functions">Functions for Transforming Text</a>).
</p>
<a name="index-spaces_002c-in-variable-values"></a>
<a name="index-whitespace_002c-in-variable-values"></a>
<a name="index-variables_002c-spaces-in-values"></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>
<div class="example">
<pre class="example">nullstring :=
space := $(nullstring) # end of the line
</pre></div>

<p>Here the value of the variable <code>space</code> is precisely one space.  The
comment &lsquo;<samp>#&nbsp;end&nbsp;of&nbsp;the&nbsp;line</samp>&rsquo;<!-- /@w --> 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>
<div class="example">
<pre class="example">dir := /foo/bar    # directory to put the frobs in
</pre></div>

<p>Here the value of the variable <code>dir</code> is &lsquo;<samp>/foo/bar&nbsp;&nbsp;&nbsp;&nbsp;</samp>&rsquo;<!-- /@w -->
(with four trailing spaces), which was probably not the intention.
(Imagine something like &lsquo;<samp>$(dir)/file</samp>&rsquo;<!-- /@w --> with this definition!)
</p>
<a name="index-conditional-variable-assignment"></a>
<a name="index-variables_002c-conditional-assignment"></a>
<a name="index-_003f_003d"></a>
<p>There is another assignment operator for variables, &lsquo;<samp>?=</samp>&rsquo;.  This
is called a conditional variable assignment operator, because it only
has an effect if the variable is not yet defined.  This statement:
</p>
<div class="example">
<pre class="example">FOO ?= bar
</pre></div>

<p>is exactly equivalent to this
(see <a href="Functions.html#Origin-Function">The <code>origin</code> Function</a>):
</p>
<div class="example">
<pre class="example">ifeq ($(origin FOO), undefined)
  FOO = bar
endif
</pre></div>

<p>Note that a variable set to an empty value is still defined, so
&lsquo;<samp>?=</samp>&rsquo; will not set that variable.
</p>
<hr>
<a name="Advanced"></a>
<div class="header">
<p>
Next: <a href="#Values" accesskey="n" rel="next">Values</a>, Previous: <a href="#Flavors" accesskey="p" rel="prev">Flavors</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Advanced-Features-for-Reference-to-Variables"></a>
<h3 class="section">6.3 Advanced Features for Reference to Variables</h3>
<a name="index-reference-to-variables-1"></a>

<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">&bull; <a href="#Substitution-Refs" accesskey="1">Substitution Refs</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Referencing a variable with
                                  substitutions on the value.
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Computed-Names" accesskey="2">Computed Names</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Computing the name of the variable to refer to.
</td></tr>
</table>

<hr>
<a name="Substitution-Refs"></a>
<div class="header">
<p>
Next: <a href="#Computed-Names" accesskey="n" rel="next">Computed Names</a>, Previous: <a href="#Advanced" accesskey="p" rel="prev">Advanced</a>, Up: <a href="#Advanced" accesskey="u" rel="up">Advanced</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Substitution-References"></a>
<h4 class="subsection">6.3.1 Substitution References</h4>
<a name="index-modified-variable-reference"></a>
<a name="index-substitution-variable-reference"></a>
<a name="index-variables_002c-modified-reference"></a>
<a name="index-variables_002c-substitution-reference"></a>

<a name="index-variables_002c-substituting-suffix-in"></a>
<a name="index-suffix_002c-substituting-in-variables"></a>
<p>A <em>substitution reference</em> substitutes the value of a variable with
alterations that you specify.  It has the form
&lsquo;<samp>$(<var>var</var>:<var>a</var>=<var>b</var>)</samp>&rsquo; (or
&lsquo;<samp>${<var>var</var>:<var>a</var>=<var>b</var>}</samp>&rsquo;) 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 &ldquo;at the end of a word&rdquo;, 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>
<div class="example">
<pre class="example">foo := a.o b.o c.o
bar := $(foo:.o=.c)
</pre></div>

<p>sets &lsquo;<samp>bar</samp>&rsquo; to &lsquo;<samp>a.c b.c c.c</samp>&rsquo;.  See <a href="#Setting">Setting Variables</a>.
</p>
<p>A substitution reference is actually an abbreviation for use of the
<code>patsubst</code> expansion function (see <a href="Functions.html#Text-Functions">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="index-patsubst"></a>
<p>Another type of substitution reference lets you use the full power of
the <code>patsubst</code> function.  It has the same form
&lsquo;<samp>$(<var>var</var>:<var>a</var>=<var>b</var>)</samp>&rsquo; described above, except that now
<var>a</var> must contain a single &lsquo;<samp>%</samp>&rsquo; character.  This case is
equivalent to &lsquo;<samp>$(patsubst <var>a</var>,<var>b</var>,$(<var>var</var>))</samp>&rsquo;.
See <a href="Functions.html#Text-Functions">Functions for String Substitution and Analysis</a>,
for a description of the <code>patsubst</code> function.
</p>
<div class="example">
<pre class="example">For example:
</pre><pre class="example">

foo := a.o b.o c.o
bar := $(foo:%.o=%.c)
</pre></div>

<p>sets &lsquo;<samp>bar</samp>&rsquo; to &lsquo;<samp>a.c b.c c.c</samp>&rsquo;.
</p>
<hr>
<a name="Computed-Names"></a>
<div class="header">
<p>
Previous: <a href="#Substitution-Refs" accesskey="p" rel="prev">Substitution Refs</a>, Up: <a href="#Advanced" accesskey="u" rel="up">Advanced</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Computed-Variable-Names"></a>
<h4 class="subsection">6.3.2 Computed Variable Names</h4>
<a name="index-nested-variable-reference"></a>
<a name="index-computed-variable-name"></a>
<a name="index-variables_002c-computed-names"></a>
<a name="index-variables_002c-nested-references"></a>
<a name="index-variables_002c-_0024-in-name"></a>
<a name="index-_0024_002c-in-variable-name"></a>
<a name="index-dollar-sign-_0028_0024_0029_002c-in-variable-name"></a>

<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>
<div class="example">
<pre class="example">x = y
y = z
a := $($(x))
</pre></div>

<p>defines <code>a</code> as &lsquo;<samp>z</samp>&rsquo;: the &lsquo;<samp>$(x)</samp>&rsquo; inside &lsquo;<samp>$($(x))</samp>&rsquo; expands
to &lsquo;<samp>y</samp>&rsquo;, so &lsquo;<samp>$($(x))</samp>&rsquo; expands to &lsquo;<samp>$(y)</samp>&rsquo; which in turn expands
to &lsquo;<samp>z</samp>&rsquo;.  Here the name of the variable to reference is not stated
explicitly; it is computed by expansion of &lsquo;<samp>$(x)</samp>&rsquo;.  The reference
&lsquo;<samp>$(x)</samp>&rsquo; 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>
<div class="example">
<pre class="example">x = y
y = z
z = u
a := $($($(x)))
</pre></div>

<p>Here the innermost &lsquo;<samp>$(x)</samp>&rsquo; expands to &lsquo;<samp>y</samp>&rsquo;, so &lsquo;<samp>$($(x))</samp>&rsquo;
expands to &lsquo;<samp>$(y)</samp>&rsquo; which in turn expands to &lsquo;<samp>z</samp>&rsquo;; now we have
&lsquo;<samp>$(z)</samp>&rsquo;, which becomes &lsquo;<samp>u</samp>&rsquo;.
</p>
<p>References to recursively-expanded variables within a variable name are
re-expanded in the usual fashion.  For example:
</p>
<div class="example">
<pre class="example">x = $(y)
y = z
z = Hello
a := $($(x))
</pre></div>

<p>defines <code>a</code> as &lsquo;<samp>Hello</samp>&rsquo;: &lsquo;<samp>$($(x))</samp>&rsquo; becomes &lsquo;<samp>$($(y))</samp>&rsquo;
which becomes &lsquo;<samp>$(z)</samp>&rsquo; which becomes &lsquo;<samp>Hello</samp>&rsquo;.
</p>
<p>Nested variable references can also contain modified references and
function invocations (see <a href="Functions.html#Functions">Functions for Transforming Text</a>),
just like any other reference.
For example, using the <code>subst</code> function
(see <a href="Functions.html#Text-Functions">Functions for String Substitution and Analysis</a>):
</p>
<div class="example">
<pre class="example">x = variable1
variable2 := Hello
y = $(subst 1,2,$(x))
z = y
a := $($($(z)))
</pre></div>

<p>eventually defines <code>a</code> as &lsquo;<samp>Hello</samp>&rsquo;.  It is doubtful that anyone
would ever want to write a nested reference as convoluted as this one, but
it works: &lsquo;<samp>$($($(z)))</samp>&rsquo; expands to &lsquo;<samp>$($(y))</samp>&rsquo; which becomes
&lsquo;<samp>$($(subst 1,2,$(x)))</samp>&rsquo;.  This gets the value &lsquo;<samp>variable1</samp>&rsquo; from
<code>x</code> and changes it by substitution to &lsquo;<samp>variable2</samp>&rsquo;, so that the
entire string becomes &lsquo;<samp>$(variable2)</samp>&rsquo;, a simple variable reference
whose value is &lsquo;<samp>Hello</samp>&rsquo;.
</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>
<div class="example">
<pre class="example">a_dirs := dira dirb
1_dirs := dir1 dir2
</pre><pre class="example">

</pre><pre class="example">a_files := filea fileb
1_files := file1 file2
</pre><pre class="example">

</pre><pre class="example">ifeq &quot;$(use_a)&quot; &quot;yes&quot;
a1 := a
else
a1 := 1
endif
</pre><pre class="example">

</pre><pre class="example">ifeq &quot;$(use_dirs)&quot; &quot;yes&quot;
df := dirs
else
df := files
endif

dirs := $($(a1)_$(df))
</pre></div>

<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>
<div class="example">
<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></div>

<p>defines <code>sources</code> as either &lsquo;<samp>a.c b.c c.c</samp>&rsquo; or &lsquo;<samp>1.c 2.c 3.c</samp>&rsquo;,
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>
<div class="example">
<pre class="example">ifdef do_sort
func := sort
else
func := strip
endif
</pre><pre class="example">

</pre><pre class="example">bar := a d b g q c
</pre><pre class="example">

</pre><pre class="example">foo := $($(func) $(bar))
</pre></div>

<p>attempts to give &lsquo;<samp>foo</samp>&rsquo; the value of the variable &lsquo;<samp>sort a d b g
q c</samp>&rsquo; or &lsquo;<samp>strip a d b g q c</samp>&rsquo;, rather than giving &lsquo;<samp>a d b g q c</samp>&rsquo;
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>
<div class="example">
<pre class="example">dir = foo
$(dir)_sources := $(wildcard $(dir)/*.c)
define $(dir)_print =
lpr $($(dir)_sources)
endef
</pre></div>

<p>This example defines the variables &lsquo;<samp>dir</samp>&rsquo;, &lsquo;<samp>foo_sources</samp>&rsquo;, and
&lsquo;<samp>foo_print</samp>&rsquo;.
</p>
<p>Note that <em>nested variable references</em> are quite different from
<em>recursively expanded variables</em>
(see <a href="#Flavors">The Two Flavors of Variables</a>), though both are
used together in complex ways when doing makefile programming.
</p>
<hr>
<a name="Values"></a>
<div class="header">
<p>
Next: <a href="#Setting" accesskey="n" rel="next">Setting</a>, Previous: <a href="#Advanced" accesskey="p" rel="prev">Advanced</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="How-Variables-Get-Their-Values"></a>
<h3 class="section">6.4 How Variables Get Their Values</h3>
<a name="index-variables_002c-how-they-get-their-values"></a>
<a name="index-value_002c-how-a-variable-gets-it"></a>

<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 <a href="Running.html#Overriding">Overriding Variables</a>.

</li><li> You can specify a value in the makefile, either
with an assignment (see <a href="#Setting">Setting Variables</a>) or with a
verbatim definition (see <a href="#Multi_002dLine">Defining Multi-Line Variables</a>).

</li><li> Variables in the environment become <code>make</code> variables.
See <a href="#Environment">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 <a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>.

</li><li> Several variables have constant initial values.
See <a href="Implicit-Rules.html#Implicit-Variables">Variables Used by Implicit Rules</a>.
</li></ul>

<hr>
<a name="Setting"></a>
<div class="header">
<p>
Next: <a href="#Appending" accesskey="n" rel="next">Appending</a>, Previous: <a href="#Values" accesskey="p" rel="prev">Values</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Setting-Variables"></a>
<h3 class="section">6.5 Setting Variables</h3>
<a name="index-setting-variables"></a>
<a name="index-variables_002c-setting"></a>
<a name="index-_003d-1"></a>
<a name="index-_003a_003d-1"></a>
<a name="index-_003a_003a_003d-1"></a>
<a name="index-_003f_003d-1"></a>
<a name="index-_0021_003d"></a>

<p>To set a variable from the makefile, write a line starting with the
variable name followed by &lsquo;<samp>=</samp>&rsquo;, &lsquo;<samp>:=</samp>&rsquo;, or &lsquo;<samp>::=</samp>&rsquo;.  Whatever
follows the &lsquo;<samp>=</samp>&rsquo;, &lsquo;<samp>:=</samp>&rsquo;, or &lsquo;<samp>::=</samp>&rsquo; on the line becomes the
value.  For example,
</p>
<div class="example">
<pre class="example">objects = main.o foo.o bar.o utils.o
</pre></div>

<p>defines a variable named <code>objects</code>.  Whitespace around the variable
name and immediately after the &lsquo;<samp>=</samp>&rsquo; is ignored.
</p>
<p>Variables defined with &lsquo;<samp>=</samp>&rsquo; are <em>recursively expanded</em>
variables.  Variables defined with &lsquo;<samp>:=</samp>&rsquo; or &lsquo;<samp>::=</samp>&rsquo; are
<em>simply expanded</em> variables; these definitions can contain
variable references which will be expanded before the definition is
made.  See <a href="#Flavors">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 memory on the computer.  You can split the value of a
variable into multiple physical lines for readability
(see <a href="Makefiles.html#Splitting-Lines">Splitting Long Lines</a>).
</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 <a href="Implicit-Rules.html#Implicit-Variables">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 <a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>).
</p>
<p>If you&rsquo;d like a variable to be set to a value only if it&rsquo;s not already
set, then you can use the shorthand operator &lsquo;<samp>?=</samp>&rsquo; instead of
&lsquo;<samp>=</samp>&rsquo;.  These two settings of the variable &lsquo;<samp>FOO</samp>&rsquo; are identical
(see <a href="Functions.html#Origin-Function">The <code>origin</code> Function</a>):
</p>
<div class="example">
<pre class="example">FOO ?= bar
</pre></div>

<p>and
</p>
<div class="example">
<pre class="example">ifeq ($(origin FOO), undefined)
FOO = bar
endif
</pre></div>

<p>The shell assignment operator &lsquo;<samp>!=</samp>&rsquo; can be used to execute a
shell script and set a variable to its output.  This operator first
evaluates the right-hand side, then passes that result to the shell
for execution.  If the result of the execution ends in a newline, that
one newline is removed; all other newlines are replaced by spaces.
The resulting string is then placed into the named
recursively-expanded variable.  For example:
</p>
<div class="example">
<pre class="example">hash != printf '\043'
file_list != find . -name '*.c'
</pre></div>

<p>If the result of the execution could produce a <code>$</code>, and you don&rsquo;t
intend what follows that to be interpreted as a make variable or
function reference, then you must replace every <code>$</code> with
<code>$$</code> as part of the execution.  Alternatively, you can set a
simply expanded variable to the result of running a program using the
<code>shell</code> function call.  See <a href="Functions.html#Shell-Function">The <code>shell</code>
Function</a>.  For example:
</p>
<div class="example">
<pre class="example">hash := $(shell printf '\043')
var := $(shell find . -name &quot;*.c&quot;)
</pre></div>

<p>As with the <code>shell</code> function, the exit status of the just-invoked
shell script is stored in the <code>.SHELLSTATUS</code> variable.
</p>

<hr>
<a name="Appending"></a>
<div class="header">
<p>
Next: <a href="#Override-Directive" accesskey="n" rel="next">Override Directive</a>, Previous: <a href="#Setting" accesskey="p" rel="prev">Setting</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Appending-More-Text-to-Variables"></a>
<h3 class="section">6.6 Appending More Text to Variables</h3>
<a name="index-_002b_003d"></a>
<a name="index-appending-to-variables"></a>
<a name="index-variables_002c-appending-to"></a>

<p>Often it is useful to add more text to the value of a variable already defined.
You do this with a line containing &lsquo;<samp>+=</samp>&rsquo;, like this:
</p>
<div class="example">
<pre class="example">objects += another.o
</pre></div>

<p>This takes the value of the variable <code>objects</code>, and adds the text
&lsquo;<samp>another.o</samp>&rsquo; to it (preceded by a single space).  Thus:
</p>
<div class="example">
<pre class="example">objects = main.o foo.o bar.o utils.o
objects += another.o
</pre></div>

<p>sets <code>objects</code> to &lsquo;<samp>main.o foo.o bar.o utils.o another.o</samp>&rsquo;.
</p>
<p>Using &lsquo;<samp>+=</samp>&rsquo; is similar to:
</p>
<div class="example">
<pre class="example">objects = main.o foo.o bar.o utils.o
objects := $(objects) another.o
</pre></div>

<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, &lsquo;<samp>+=</samp>&rsquo;
acts just like normal &lsquo;<samp>=</samp>&rsquo;: it defines a recursively-expanded
variable.  However, when there <em>is</em> a previous definition, exactly
what &lsquo;<samp>+=</samp>&rsquo; does depends on what flavor of variable you defined
originally.  See <a href="#Flavors">The Two Flavors of Variables</a>, for an
explanation of the two flavors of variables.
</p>
<p>When you add to a variable&rsquo;s value with &lsquo;<samp>+=</samp>&rsquo;, <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 &lsquo;<samp>:=</samp>&rsquo; or
&lsquo;<samp>::=</samp>&rsquo;, making it a simply-expanded variable, &lsquo;<samp>+=</samp>&rsquo; adds to
that simply-expanded definition, and expands the new text before
appending it to the old value just as &lsquo;<samp>:=</samp>&rsquo; does (see
<a href="#Setting">Setting Variables</a>, for a full explanation of
&lsquo;<samp>:=</samp>&rsquo; or &lsquo;<samp>::=</samp>&rsquo;).  In fact,
</p>
<div class="example">
<pre class="example">variable := value
variable += more
</pre></div>

<p>is exactly equivalent to:
</p>
<div class="example">
<pre class="example">variable := value
variable := $(variable) more
</pre></div>

<p>On the other hand, when you use &lsquo;<samp>+=</samp>&rsquo; with a variable that you defined
first to be recursively-expanded using plain &lsquo;<samp>=</samp>&rsquo;, <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 <a href="#Flavors">The Two Flavors
of Variables</a>).  When you use &lsquo;<samp>+=</samp>&rsquo; on a recursively-expanded variable,
it is this unexpanded text to which <code>make</code> appends the new text you
specify.
</p>
<div class="example">
<pre class="example">variable = value
variable += more
</pre></div>

<p>is roughly equivalent to:
</p>
<div class="example">
<pre class="example">temp = value
variable = $(temp) more
</pre></div>

<p>except that of course it never defines a variable called <code>temp</code>.
The importance of this comes when the variable&rsquo;s old value contains
variable references.  Take this common example:
</p>
<div class="example">
<pre class="example">CFLAGS = $(includes) -O
&hellip;
CFLAGS += -pg # enable profiling
</pre></div>

<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 <a href="Implicit-Rules.html#Catalogue-of-Rules">Catalogue of Built-In Rules</a>.)
Using &lsquo;<samp>=</samp>&rsquo; for the definition makes <code>CFLAGS</code> a recursively-expanded
variable, meaning &lsquo;<samp>$(includes)&nbsp;<span class="nolinebreak">-O</span></samp>&rsquo;<!-- /@w --> 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 &lsquo;<samp>+=</samp>&rsquo;, we might do it like this:
</p>
<div class="example">
<pre class="example">CFLAGS := $(CFLAGS) -pg # enable profiling
</pre></div>

<p>This is pretty close, but not quite what we want.  Using &lsquo;<samp>:=</samp>&rsquo;
redefines <code>CFLAGS</code> as a simply-expanded variable; this means
<code>make</code> expands the text &lsquo;<samp>$(CFLAGS)&nbsp;<span class="nolinebreak">-pg</span></samp>&rsquo;<!-- /@w --> before setting the
variable.  If <code>includes</code> is not yet defined, we get &lsquo;<samp>&nbsp;<span class="nolinebreak">-O</span>&nbsp;<span class="nolinebreak">-pg</span></samp>&rsquo;<!-- /@w -->, and a later definition of <code>includes</code> will have no effect.
Conversely, by using &lsquo;<samp>+=</samp>&rsquo; we set <code>CFLAGS</code> to the
<em>unexpanded</em> value &lsquo;<samp>$(includes)&nbsp;<span class="nolinebreak">-O</span>&nbsp;<span class="nolinebreak">-pg</span></samp>&rsquo;<!-- /@w -->.  Thus we preserve
the reference to <code>includes</code>, so if that variable gets defined at
any later point, a reference like &lsquo;<samp>$(CFLAGS)</samp>&rsquo; still uses its
value.
</p>
<hr>
<a name="Override-Directive"></a>
<div class="header">
<p>
Next: <a href="#Multi_002dLine" accesskey="n" rel="next">Multi-Line</a>, Previous: <a href="#Appending" accesskey="p" rel="prev">Appending</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-override-Directive"></a>
<h3 class="section">6.7 The <code>override</code> Directive</h3>
<a name="index-override"></a>
<a name="index-overriding-with-override"></a>
<a name="index-variables_002c-overriding"></a>

<p>If a variable has been set with a command argument
(see <a href="Running.html#Overriding">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>
<div class="example">
<pre class="example">override <var>variable</var> = <var>value</var>
</pre></div>

<p>or
</p>
<div class="example">
<pre class="example">override <var>variable</var> := <var>value</var>
</pre></div>

<p>To append more text to a variable defined on the command line, use:
</p>
<div class="example">
<pre class="example">override <var>variable</var> += <var>more text</var>
</pre></div>

<p>See <a href="#Appending">Appending More Text to Variables</a>.
</p>
<p>Variable assignments marked with the <code>override</code> flag have a
higher priority than all other assignments, except another
<code>override</code>.  Subsequent assignments or appends to this variable
which are not marked <code>override</code> will be ignored.
</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 &lsquo;<samp>-g</samp>&rsquo; 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>
<div class="example">
<pre class="example">override CFLAGS += -g
</pre></div>

<p>You can also use <code>override</code> directives with <code>define</code> directives.
This is done as you might expect:
</p>
<div class="example">
<pre class="example">override define foo =
bar
endef
</pre></div>

<p>See <a href="#Multi_002dLine">Defining Multi-Line Variables</a>.
</p>
<hr>
<a name="Multi_002dLine"></a>
<div class="header">
<p>
Next: <a href="#Undefine-Directive" accesskey="n" rel="next">Undefine Directive</a>, Previous: <a href="#Override-Directive" accesskey="p" rel="prev">Override Directive</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Defining-Multi_002dLine-Variables"></a>
<h3 class="section">6.8 Defining Multi-Line Variables</h3>
<a name="index-define"></a>
<a name="index-endef"></a>
<a name="index-multi_002dline-variable-definition"></a>
<a name="index-variables_002c-multi_002dline"></a>
<a name="index-verbatim-variable-definition"></a>
<a name="index-defining-variables-verbatim"></a>
<a name="index-variables_002c-defining-verbatim"></a>

<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 <a href="Recipes.html#Canned-Recipes">Defining Canned Recipes</a>), and also sections of makefile syntax to
use with <code>eval</code> (see <a href="Functions.html#Eval-Function">Eval Function</a>).
</p>
<p>The <code>define</code> directive is followed on the same line by the name
of the variable being defined and an (optional) assignment operator,
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 any other variable definition.  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 omit the variable assignment operator if you prefer.  If
omitted, <code>make</code> assumes it to be &lsquo;<samp>=</samp>&rsquo; and creates a
recursively-expanded variable (see <a href="#Flavors">The Two Flavors of Variables</a>).
When using a &lsquo;<samp>+=</samp>&rsquo; operator, the value is appended to the previous
value as with any other append operation: with a single space
separating the old and new values.
</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 the recipe
prefix character are considered part of a recipe, so any <code>define</code>
or <code>endef</code> strings appearing on such a line will not be
considered <code>make</code> directives.
</p>
<div class="example">
<pre class="example">define two-lines =
echo foo
echo $(bar)
endef
</pre></div>

<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&rsquo;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 recipe, the previous example is functionally equivalent
to this:
</p>
<div class="example">
<pre class="example">two-lines = echo foo; echo $(bar)
</pre></div>

<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 sub-shell
for each line.  See <a href="Recipes.html#Execution">Recipe 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>
<div class="example">
<pre class="example">override define two-lines =
foo
$(bar)
endef
</pre></div>

<p>See <a href="#Override-Directive">The <code>override</code> Directive</a>.
</p>
<hr>
<a name="Undefine-Directive"></a>
<div class="header">
<p>
Next: <a href="#Environment" accesskey="n" rel="next">Environment</a>, Previous: <a href="#Multi_002dLine" accesskey="p" rel="prev">Multi-Line</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Undefining-Variables"></a>
<h3 class="section">6.9 Undefining Variables</h3>
<a name="index-undefine"></a>
<a name="index-undefining-variable"></a>

<p>If you want to clear a variable, setting its value to empty is usually
sufficient. Expanding such a variable will yield the same result (empty
string) regardless of whether it was set or not. However, if you are
using the <code>flavor</code> (see <a href="Functions.html#Flavor-Function">Flavor Function</a>) and
<code>origin</code> (see <a href="Functions.html#Origin-Function">Origin Function</a>) functions, there is a difference
between a variable that was never set and a variable with an empty value.
In such situations you may want to use the <code>undefine</code> directive to
make a variable appear as if it was never set. For example:
</p>
<div class="example">
<pre class="example">foo := foo
bar = bar

undefine foo
undefine bar

$(info $(origin foo))
$(info $(flavor bar))
</pre></div>

<p>This example will print &ldquo;undefined&rdquo; for both variables.
</p>
<p>If you want to undefine a command-line variable definition, you can use
the <code>override</code> directive together with <code>undefine</code>, similar to
how this is done for variable definitions:
</p>
<div class="example">
<pre class="example">override undefine CFLAGS
</pre></div>

<hr>
<a name="Environment"></a>
<div class="header">
<p>
Next: <a href="#Target_002dspecific" accesskey="n" rel="next">Target-specific</a>, Previous: <a href="#Undefine-Directive" accesskey="p" rel="prev">Undefine Directive</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Variables-from-the-Environment"></a>
<h3 class="section">6.10 Variables from the Environment</h3>

<a name="index-variables_002c-environment-1"></a>
<a name="index-environment"></a>
<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
&lsquo;<samp>-e</samp>&rsquo; flag is specified, then values from the environment override
assignments in the makefile.  See <a href="Running.html#Options-Summary">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 recipe, variables defined in the
makefile are placed into the environment of each shell.  This allows
you to pass values to sub-<code>make</code> invocations (see <a href="Recipes.html#Recursion">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 <a href="Recipes.html#Variables_002fRecursion">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="index-SHELL_002c-import-from-environment"></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&rsquo;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="Recipes.html#Choosing-the-Shell">Choosing the Shell</a>.
</p>
<hr>
<a name="Target_002dspecific"></a>
<div class="header">
<p>
Next: <a href="#Pattern_002dspecific" accesskey="n" rel="next">Pattern-specific</a>, Previous: <a href="#Environment" accesskey="p" rel="prev">Environment</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Target_002dspecific-Variable-Values"></a>
<h3 class="section">6.11 Target-specific Variable Values</h3>
<a name="index-target_002dspecific-variables"></a>
<a name="index-variables_002c-target_002dspecific"></a>

<p>Variable values in <code>make</code> are usually global; that is, they are the
same regardless of where they are evaluated (unless they&rsquo;re reset, of
course).  One exception to that is automatic variables
(see <a href="Implicit-Rules.html#Automatic-Variables">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&rsquo;s recipe (and in other target-specific assignments).
</p>
<p>Set a target-specific variable value like this:
</p>
<div class="example">
<pre class="example"><var>target</var> &hellip; : <var>variable-assignment</var>
</pre></div>

<p>Target-specific variable assignments can be prefixed with any or all of the
special keywords <code>export</code>, <code>override</code>, or <code>private</code>;
these apply their normal behavior to this instance of the variable only.
</p>
<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 (&lsquo;<samp>=</samp>&rsquo;), simple (&lsquo;<samp>:=</samp>&rsquo; or &lsquo;<samp>::=</samp>&rsquo;), appending
(&lsquo;<samp>+=</samp>&rsquo;), or conditional (&lsquo;<samp>?=</samp>&rsquo;).  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 &ldquo;global&rdquo; value: the two variables do not have to
have the same flavor (recursive vs. simple).
</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 &lsquo;<samp>-e</samp>&rsquo; 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>
<div class="example">
<pre class="example">prog : CFLAGS = -g
prog : prog.o foo.o bar.o
</pre></div>

<p>will set <code>CFLAGS</code> to &lsquo;<samp>-g</samp>&rsquo; in the recipe for <samp>prog</samp>, but
it will also set <code>CFLAGS</code> to &lsquo;<samp>-g</samp>&rsquo; in the recipes that create
<samp>prog.o</samp>, <samp>foo.o</samp>, and <samp>bar.o</samp>, and any recipes 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>
<a name="Pattern_002dspecific"></a>
<div class="header">
<p>
Next: <a href="#Suppressing-Inheritance" accesskey="n" rel="next">Suppressing Inheritance</a>, Previous: <a href="#Target_002dspecific" accesskey="p" rel="prev">Target-specific</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Pattern_002dspecific-Variable-Values"></a>
<h3 class="section">6.12 Pattern-specific Variable Values</h3>
<a name="index-pattern_002dspecific-variables"></a>
<a name="index-variables_002c-pattern_002dspecific"></a>

<p>In addition to target-specific variable values
(see <a href="#Target_002dspecific">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.
</p>
<p>Set a pattern-specific variable value like this:
</p>
<div class="example">
<pre class="example"><var>pattern</var> &hellip; : <var>variable-assignment</var>
</pre></div>
<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>
<div class="example">
<pre class="example">%.o : CFLAGS = -O
</pre></div>

<p>will assign <code>CFLAGS</code> the value of &lsquo;<samp>-O</samp>&rsquo; for all targets
matching the pattern <code>%.o</code>.
</p>
<p>If a target matches more than one pattern, the matching pattern-specific
variables with longer stems are interpreted first. This results in more
specific variables taking precedence over the more generic ones, for
example:
</p>
<div class="example">
<pre class="example">%.o: %.c
        $(CC) -c $(CFLAGS) $(CPPFLAGS) $&lt; -o $@

lib/%.o: CFLAGS := -fPIC -g
%.o: CFLAGS := -g

all: foo.o lib/bar.o
</pre></div>

<p>In this example the first definition of the <code>CFLAGS</code> variable
will be used to update <samp>lib/bar.o</samp> even though the second one
also applies to this target. Pattern-specific variables which result
in the same stem length are considered in the order in which they
were defined in the makefile.
</p>
<p>Pattern-specific variables are searched after any target-specific
variables defined explicitly for that target, and before target-specific
variables defined for the parent target.
</p>
<hr>
<a name="Suppressing-Inheritance"></a>
<div class="header">
<p>
Next: <a href="#Special-Variables" accesskey="n" rel="next">Special Variables</a>, Previous: <a href="#Pattern_002dspecific" accesskey="p" rel="prev">Pattern-specific</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Suppressing-Inheritance-1"></a>
<h3 class="section">6.13 Suppressing Inheritance</h3>
<a name="index-private"></a>
<a name="index-suppressing-inheritance"></a>
<a name="index-inheritance_002c-suppressing"></a>

<p>As described in previous sections, <code>make</code> variables are inherited
by prerequisites.  This capability allows you to modify the behavior
of a prerequisite based on which targets caused it to be rebuilt.  For
example, you might set a target-specific variable on a <code>debug</code>
target, then running &lsquo;<samp>make debug</samp>&rsquo; will cause that variable to be
inherited by all prerequisites of <code>debug</code>, while just running
&lsquo;<samp>make all</samp>&rsquo; (for example) would not have that assignment.
</p>
<p>Sometimes, however, you may not want a variable to be inherited.  For
these situations, <code>make</code> provides the <code>private</code> modifier.
Although this modifier can be used with any variable assignment, it
makes the most sense with target- and pattern-specific variables.  Any
variable marked <code>private</code> will be visible to its local target but
will not be inherited by prerequisites of that target.  A global
variable marked <code>private</code> will be visible in the global scope but
will not be inherited by any target, and hence will not be visible
in any recipe.
</p>
<p>As an example, consider this makefile:
</p><div class="example">
<pre class="example">EXTRA_CFLAGS =

prog: private EXTRA_CFLAGS = -L/usr/local/lib
prog: a.o b.o
</pre></div>

<p>Due to the <code>private</code> modifier, <code>a.o</code> and <code>b.o</code> will not
inherit the <code>EXTRA_CFLAGS</code> variable assignment from the
<code>prog</code> target.
</p>
<hr>
<a name="Special-Variables"></a>
<div class="header">
<p>
Previous: <a href="#Suppressing-Inheritance" accesskey="p" rel="prev">Suppressing Inheritance</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Other-Special-Variables"></a>
<h3 class="section">6.14 Other Special Variables</h3>
<a name="index-makefiles_002c-and-special-variables"></a>
<a name="index-special-variables"></a>

<p>GNU <code>make</code> supports some variables that have special properties.
</p>
<dl compact="compact">
<dd>
<a name="index-MAKEFILE_005fLIST-_0028list-of-parsed-makefiles_0029"></a>
<a name="index-makefiles_002c-and-MAKEFILE_005fLIST-variable"></a>
<a name="index-including-_0028MAKEFILE_005fLIST-variable_0029"></a>
</dd>
<dt><code>MAKEFILE_LIST</code></dt>
<dd><p>Contains the name of each makefile that is parsed by <code>make</code>, in
the order in which it was parsed.  The name is appended just
before <code>make</code> begins to parse the makefile.  Thus, if the first
thing a makefile does is examine the last word in this variable, it
will be the name of the current makefile.  Once the current makefile
has used <code>include</code>, however, the last word will be the
just-included makefile.
</p>
<p>If a makefile named <code>Makefile</code> has this content:
</p>
<div class="example">
<pre class="example">name1 := $(lastword $(MAKEFILE_LIST))

include inc.mk

name2 := $(lastword $(MAKEFILE_LIST))

all:
        @echo name1 = $(name1)
        @echo name2 = $(name2)
</pre></div>

<p>then you would expect to see this output:
</p>
<div class="example">
<pre class="example">name1 = Makefile
name2 = inc.mk
</pre></div>

<a name="index-_002eDEFAULT_005fGOAL-_0028define-default-goal_0029"></a>
</dd>
<dt><code>.DEFAULT_GOAL</code></dt>
<dd><p>Sets the default goal to be used if no targets were specified on the
command line (see <a href="Running.html#Goals">Arguments to Specify the Goals</a>).  The
<code>.DEFAULT_GOAL</code> variable allows you to discover the current
default goal, restart the default goal selection algorithm by clearing
its value, or to explicitly set the default goal.  The following
example illustrates these cases:
</p>
<div class="example">
<pre class="example"># Query the default goal.
ifeq ($(.DEFAULT_GOAL),)
  $(warning no default goal is set)
endif

.PHONY: foo
foo: ; @echo $@

$(warning default goal is $(.DEFAULT_GOAL))

# Reset the default goal.
.DEFAULT_GOAL :=

.PHONY: bar
bar: ; @echo $@

$(warning default goal is $(.DEFAULT_GOAL))

# Set our own.
.DEFAULT_GOAL := foo
</pre></div>

<p>This makefile prints:
</p>
<div class="example">
<pre class="example">no default goal is set
default goal is foo
default goal is bar
foo
</pre></div>

<p>Note that assigning more than one target name to <code>.DEFAULT_GOAL</code> is
invalid and will result in an error.
</p>
<a name="index-MAKE_005fRESTARTS-_0028number-of-times-make-has-restarted_0029"></a>
</dd>
<dt><code>MAKE_RESTARTS</code></dt>
<dd><p>This variable is set only if this instance of <code>make</code> has
restarted (see <a href="Makefiles.html#Remaking-Makefiles">How Makefiles Are Remade</a>): it
will contain the number of times this instance has restarted.  Note
this is not the same as recursion (counted by the <code>MAKELEVEL</code>
variable).  You should not set, modify, or export this variable.
</p>
<a name="index-MAKE_005fTERMOUT-_0028whether-stdout-is-a-terminal_0029"></a>
<a name="index-MAKE_005fTERMERR-_0028whether-stderr-is-a-terminal_0029"></a>
</dd>
<dt><code>MAKE_TERMOUT</code></dt>
<dt><code>MAKE_TERMERR</code></dt>
<dd><p>When <code>make</code> starts it will check whether stdout and stderr will
show their output on a terminal.  If so, it will set
<code>MAKE_TERMOUT</code> and <code>MAKE_TERMERR</code>, respectively, to the name
of the terminal device (or <code>true</code> if this cannot be determined).
If set these variables will be marked for export.  These variables
will not be changed by <code>make</code> and they will not be modified if
already set.
</p>
<p>These values can be used (particularly in combination with output
synchronization (see <a href="Recipes.html#Parallel-Output">Output During Parallel
Execution</a>) to determine whether <code>make</code> itself is writing to a
terminal; they can be tested to decide whether to force recipe
commands to generate colorized output for example.
</p>
<p>If you invoke a sub-<code>make</code> and redirect its stdout or stderr it
is your responsibility to reset or unexport these variables as well,
if your makefiles rely on them.
</p>
<a name="index-_002eRECIPEPREFIX-_0028change-the-recipe-prefix-character_0029"></a>
</dd>
<dt><code>.RECIPEPREFIX</code></dt>
<dd><p>The first character of the value of this variable is used as the
character make assumes is introducing a recipe line.  If the variable
is empty (as it is by default) that character is the standard tab
character.  For example, this is a valid makefile:
</p>
<div class="example">
<pre class="example">.RECIPEPREFIX = &gt;
all:
&gt; @echo Hello, world
</pre></div>

<p>The value of <code>.RECIPEPREFIX</code> can be changed multiple times; once set
it stays in effect for all rules parsed until it is modified.
</p>
<a name="index-_002eVARIABLES-_0028list-of-variables_0029"></a>
</dd>
<dt><code>.VARIABLES</code></dt>
<dd><p>Expands to a list of the <em>names</em> of all global variables defined
so far.  This includes variables which have empty values, as well as
built-in variables (see <a href="Implicit-Rules.html#Implicit-Variables">Variables Used by
Implicit Rules</a>), but does not include any variables which are only
defined in a target-specific context.  Note that any value you assign
to this variable will be ignored; it will always return its special
value.
</p>

<a name="index-_002eFEATURES-_0028list-of-supported-features_0029"></a>
</dd>
<dt><code>.FEATURES</code></dt>
<dd><p>Expands to a list of special features supported by this version of
<code>make</code>.  Possible values include, but are not limited to:
</p>
<dl compact="compact">
<dt>&lsquo;<samp>archives</samp>&rsquo;</dt>
<dd><p>Supports <code>ar</code> (archive) files using special file name syntax.
See <a href="Archives.html#Archives">Using <code>make</code> to Update Archive Files</a>.
</p>
</dd>
<dt>&lsquo;<samp>check-symlink</samp>&rsquo;</dt>
<dd><p>Supports the <code>-L</code> (<code>--check-symlink-times</code>) flag.
See <a href="Running.html#Options-Summary">Summary of Options</a>.
</p>
</dd>
<dt>&lsquo;<samp>else-if</samp>&rsquo;</dt>
<dd><p>Supports &ldquo;else if&rdquo; non-nested conditionals.  See <a href="Conditionals.html#Conditional-Syntax">Syntax of Conditionals</a>.
</p>
</dd>
<dt>&lsquo;<samp>jobserver</samp>&rsquo;</dt>
<dd><p>Supports &ldquo;job server&rdquo; enhanced parallel builds.  See <a href="Recipes.html#Parallel">Parallel Execution</a>.
</p>
</dd>
<dt>&lsquo;<samp>oneshell</samp>&rsquo;</dt>
<dd><p>Supports the <code>.ONESHELL</code> special target.  See <a href="Recipes.html#One-Shell">Using
One Shell</a>.
</p>
</dd>
<dt>&lsquo;<samp>order-only</samp>&rsquo;</dt>
<dd><p>Supports order-only prerequisites.  See <a href="Rules.html#Prerequisite-Types">Types
of Prerequisites</a>.
</p>
</dd>
<dt>&lsquo;<samp>second-expansion</samp>&rsquo;</dt>
<dd><p>Supports secondary expansion of prerequisite lists.
</p>
</dd>
<dt>&lsquo;<samp>shortest-stem</samp>&rsquo;</dt>
<dd><p>Uses the &ldquo;shortest stem&rdquo; method of choosing which pattern, of
multiple applicable options, will be used.  See <a href="Implicit-Rules.html#Pattern-Match">How
Patterns Match</a>.
</p>
</dd>
<dt>&lsquo;<samp>target-specific</samp>&rsquo;</dt>
<dd><p>Supports target-specific and pattern-specific variable assignments.
See <a href="#Target_002dspecific">Target-specific Variable Values</a>.
</p>
</dd>
<dt>&lsquo;<samp>undefine</samp>&rsquo;</dt>
<dd><p>Supports the <code>undefine</code> directive.  See <a href="#Undefine-Directive">Undefine Directive</a>.
</p>
</dd>
<dt>&lsquo;<samp>guile</samp>&rsquo;</dt>
<dd><p>Has GNU Guile available as an embedded extension language.
See <a href="Extending-make.html#Guile-Integration">GNU Guile Integration</a>.
</p>
</dd>
<dt>&lsquo;<samp>load</samp>&rsquo;</dt>
<dd><p>Supports dynamically loadable objects for creating custom extensions.
See <a href="Extending-make.html#Loading-Objects">Loading Dynamic Objects</a>.
</p></dd>
</dl>

<a name="index-_002eINCLUDE_005fDIRS-_0028list-of-include-directories_0029"></a>
</dd>
<dt><code>.INCLUDE_DIRS</code></dt>
<dd><p>Expands to a list of directories that <code>make</code> searches for
included makefiles (see <a href="Makefiles.html#Include">Including Other Makefiles</a>).
</p>
</dd>
</dl>

<hr>
<div class="header">
<p>
Previous: <a href="#Suppressing-Inheritance" accesskey="p" rel="prev">Suppressing Inheritance</a>, Up: <a href="#Using-Variables" accesskey="u" rel="up">Using Variables</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>