/usr/share/doc/make-doc/make.html/Rules.html is in make-doc 4.1-2.
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 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 | <!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.73, last updated 15 January 2016,
of The GNU Make Manual, for GNU make version 4.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 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.0, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU make: Rules</title>
<meta name="description" content="GNU make: Rules">
<meta name="keywords" content="GNU make: Rules">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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="Recipes.html#Recipes" rel="next" title="Recipes">
<link href="Makefiles.html#Secondary-Expansion" rel="prev" title="Secondary Expansion">
<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.nocodebreak {white-space: nowrap}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: serif; 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="Rules"></a>
<div class="header">
<p>
Next: <a href="Recipes.html#Recipes" accesskey="n" rel="next">Recipes</a>, Previous: <a href="Makefiles.html#Makefiles" accesskey="p" rel="prev">Makefiles</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<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="Writing-Rules"></a>
<h2 class="chapter">4 Writing Rules</h2>
<a name="index-writing-rules"></a>
<a name="index-rule_002c-how-to-write"></a>
<a name="index-target"></a>
<a name="index-prerequisite"></a>
<p>A <em>rule</em> appears in the makefile and says when and how to remake
certain files, called the rule’s <em>targets</em> (most often only one per rule).
It lists the other files that are the <em>prerequisites</em> of the target, and
the <em>recipe</em> to use to create or update the target.
</p>
<a name="index-default-goal-1"></a>
<a name="index-goal_002c-default-1"></a>
<p>The order of rules is not significant, except for determining the
<em>default goal</em>: the target for <code>make</code> to consider, if you do
not otherwise specify one. The default goal is the target of the first
rule in the first makefile. If the first rule has multiple targets,
only the first target is taken as the default. There are two
exceptions: a target starting with a period is not a default unless it
contains one or more slashes, ‘<samp>/</samp>’, as well; and, a target that
defines a pattern rule has no effect on the default goal.
(See <a href="Implicit-Rules.html#Pattern-Rules">Defining and Redefining Pattern Rules</a>.)
</p>
<p>Therefore, we usually write the makefile so that the first rule is the
one for compiling the entire program or all the programs described by
the makefile (often with a target called ‘<samp>all</samp>’).
See <a href="Running.html#Goals">Arguments to Specify the Goals</a>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Rule-Example" accesskey="1">Rule Example</a>:</td><td> </td><td align="left" valign="top">An example explained.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Rule-Syntax" accesskey="2">Rule Syntax</a>:</td><td> </td><td align="left" valign="top">General syntax explained.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Prerequisite-Types" accesskey="3">Prerequisite Types</a>:</td><td> </td><td align="left" valign="top">There are two types of prerequisites.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Wildcards" accesskey="4">Wildcards</a>:</td><td> </td><td align="left" valign="top">Using wildcard characters such as ‘*’.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Directory-Search" accesskey="5">Directory Search</a>:</td><td> </td><td align="left" valign="top">Searching other directories for source files.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Phony-Targets" accesskey="6">Phony Targets</a>:</td><td> </td><td align="left" valign="top">Using a target that is not a real file’s name.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Force-Targets" accesskey="7">Force Targets</a>:</td><td> </td><td align="left" valign="top">You can use a target without a recipe
or prerequisites to mark other targets
as phony.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Empty-Targets" accesskey="8">Empty Targets</a>:</td><td> </td><td align="left" valign="top">When only the date matters and the
files are empty.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Special-Targets" accesskey="9">Special Targets</a>:</td><td> </td><td align="left" valign="top">Targets with special built-in meanings.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Multiple-Targets">Multiple Targets</a>:</td><td> </td><td align="left" valign="top">When to make use of several targets in a rule.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Multiple-Rules">Multiple Rules</a>:</td><td> </td><td align="left" valign="top">How to use several rules with the same target.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Static-Pattern">Static Pattern</a>:</td><td> </td><td align="left" valign="top">Static pattern rules apply to multiple targets
and can vary the prerequisites according to
the target name.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Double_002dColon">Double-Colon</a>:</td><td> </td><td align="left" valign="top">How to use a special kind of rule to allow
several independent rules for one target.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Automatic-Prerequisites">Automatic Prerequisites</a>:</td><td> </td><td align="left" valign="top">How to automatically generate rules giving
prerequisites from source files themselves.
</td></tr>
</table>
<hr>
<a name="Rule-Example"></a>
<div class="header">
<p>
Next: <a href="#Rule-Syntax" accesskey="n" rel="next">Rule Syntax</a>, Previous: <a href="#Rules" accesskey="p" rel="prev">Rules</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Rule-Example-1"></a>
<h3 class="section">4.1 Rule Example</h3>
<p>Here is an example of a rule:
</p>
<div class="example">
<pre class="example">foo.o : foo.c defs.h # module for twiddling the frobs
cc -c -g foo.c
</pre></div>
<p>Its target is <samp>foo.o</samp> and its prerequisites are <samp>foo.c</samp> and
<samp>defs.h</samp>. It has one command in the recipe: ‘<samp>cc -c -g foo.c</samp>’.
The recipe starts with a tab to identify it as a recipe.
</p>
<p>This rule says two things:
</p>
<ul>
<li> How to decide whether <samp>foo.o</samp> is out of date: it is out of date
if it does not exist, or if either <samp>foo.c</samp> or <samp>defs.h</samp> is
more recent than it.
</li><li> How to update the file <samp>foo.o</samp>: by running <code>cc</code> as stated.
The recipe does not explicitly mention <samp>defs.h</samp>, but we presume
that <samp>foo.c</samp> includes it, and that that is why <samp>defs.h</samp> was
added to the prerequisites.
</li></ul>
<hr>
<a name="Rule-Syntax"></a>
<div class="header">
<p>
Next: <a href="#Prerequisite-Types" accesskey="n" rel="next">Prerequisite Types</a>, Previous: <a href="#Rule-Example" accesskey="p" rel="prev">Rule Example</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Rule-Syntax-1"></a>
<h3 class="section">4.2 Rule Syntax</h3>
<a name="index-rule-syntax"></a>
<a name="index-syntax-of-rules"></a>
<p>In general, a rule looks like this:
</p>
<div class="example">
<pre class="example"><var>targets</var> : <var>prerequisites</var>
<var>recipe</var>
…
</pre></div>
<p>or like this:
</p>
<div class="example">
<pre class="example"><var>targets</var> : <var>prerequisites</var> ; <var>recipe</var>
<var>recipe</var>
…
</pre></div>
<a name="index-targets"></a>
<a name="index-rule-targets"></a>
<p>The <var>targets</var> are file names, separated by spaces. Wildcard
characters may be used (see <a href="#Wildcards">Using Wildcard Characters
in File Names</a>) and a name of the form <samp><var>a</var>(<var>m</var>)</samp>
represents member <var>m</var> in archive file <var>a</var>
(see <a href="Archives.html#Archive-Members">Archive Members as Targets</a>).
Usually there is only one
target per rule, but occasionally there is a reason to have more
(see <a href="#Multiple-Targets">Multiple Targets in a Rule</a>).
</p>
<a name="index-recipes"></a>
<a name="index-tab-character-_0028in-commands_0029"></a>
<p>The <var>recipe</var> lines start with a tab character (or the first
character in the value of the <code>.RECIPEPREFIX</code> variable;
see <a href="Using-Variables.html#Special-Variables">Special Variables</a>). The first recipe line may appear on the line
after the prerequisites, with a tab character, or may appear on the
same line, with a semicolon. Either way, the effect is the same.
There are other differences in the syntax of recipes.
See <a href="Recipes.html#Recipes">Writing Recipes in Rules</a>.
</p>
<a name="index-dollar-sign-_0028_0024_0029_002c-in-rules"></a>
<a name="index-_0024_002c-in-rules"></a>
<a name="index-rules_002c-and-_0024"></a>
<p>Because dollar signs are used to start <code>make</code> variable
references, if you really want a dollar sign in a target or
prerequisite you must write two of them, ‘<samp>$$</samp>’ (see <a href="Using-Variables.html#Using-Variables">How to Use Variables</a>). If you have enabled secondary
expansion (see <a href="Makefiles.html#Secondary-Expansion">Secondary Expansion</a>) and you want a literal dollar
sign in the prerequisites list, you must actually write <em>four</em>
dollar signs (‘<samp>$$$$</samp>’).
</p>
<p>You may split a long line by inserting a backslash followed by a
newline, but this is not required, as <code>make</code> places no limit on
the length of a line in a makefile.
</p>
<p>A rule tells <code>make</code> two things: when the targets are out of date,
and how to update them when necessary.
</p>
<a name="index-prerequisites"></a>
<a name="index-rule-prerequisites"></a>
<p>The criterion for being out of date is specified in terms of the
<var>prerequisites</var>, which consist of file names separated by spaces.
(Wildcards and archive members (see <a href="Archives.html#Archives">Archives</a>) are allowed here too.)
A target is out of date if it does not exist or if it is older than any
of the prerequisites (by comparison of last-modification times). The
idea is that the contents of the target file are computed based on
information in the prerequisites, so if any of the prerequisites changes,
the contents of the existing target file are no longer necessarily
valid.
</p>
<p>How to update is specified by a <var>recipe</var>. This is one or more
lines to be executed by the shell (normally ‘<samp>sh</samp>’), but with some
extra features (see <a href="Recipes.html#Recipes">Writing Recipes in Rules</a>).
</p>
<hr>
<a name="Prerequisite-Types"></a>
<div class="header">
<p>
Next: <a href="#Wildcards" accesskey="n" rel="next">Wildcards</a>, Previous: <a href="#Rule-Syntax" accesskey="p" rel="prev">Rule Syntax</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Types-of-Prerequisites"></a>
<h3 class="section">4.3 Types of Prerequisites</h3>
<a name="index-prerequisite-types"></a>
<a name="index-types-of-prerequisites"></a>
<a name="index-prerequisites_002c-normal"></a>
<a name="index-normal-prerequisites"></a>
<a name="index-prerequisites_002c-order_002donly"></a>
<a name="index-order_002donly-prerequisites"></a>
<p>There are actually two different types of prerequisites understood by
GNU <code>make</code>: normal prerequisites such as described in the
previous section, and <em>order-only</em> prerequisites. A normal
prerequisite makes two statements: first, it imposes an order in which
recipes will be invoked: the recipes for all prerequisites of a target
will be completed before the recipe for the target is run. Second, it
imposes a dependency relationship: if any prerequisite is newer than
the target, then the target is considered out-of-date and must be
rebuilt.
</p>
<p>Normally, this is exactly what you want: if a target’s prerequisite is
updated, then the target should also be updated.
</p>
<p>Occasionally, however, you have a situation where you want to impose a
specific ordering on the rules to be invoked <em>without</em> forcing
the target to be updated if one of those rules is executed. In that
case, you want to define <em>order-only</em> prerequisites. Order-only
prerequisites can be specified by placing a pipe symbol (<code>|</code>)
in the prerequisites list: any prerequisites to the left of the pipe
symbol are normal; any prerequisites to the right are order-only:
</p>
<div class="example">
<pre class="example"><var>targets</var> : <var>normal-prerequisites</var> | <var>order-only-prerequisites</var>
</pre></div>
<p>The normal prerequisites section may of course be empty. Also, you
may still declare multiple lines of prerequisites for the same target:
they are appended appropriately (normal prerequisites are appended to
the list of normal prerequisites; order-only prerequisites are
appended to the list of order-only prerequisites). Note that if you
declare the same file to be both a normal and an order-only
prerequisite, the normal prerequisite takes precedence (since they
have a strict superset of the behavior of an order-only prerequisite).
</p>
<p>Consider an example where your targets are to be placed in a separate
directory, and that directory might not exist before <code>make</code> is
run. In this situation, you want the directory to be created before
any targets are placed into it but, because the timestamps on
directories change whenever a file is added, removed, or renamed, we
certainly don’t want to rebuild all the targets whenever the
directory’s timestamp changes. One way to manage this is with
order-only prerequisites: make the directory an order-only
prerequisite on all the targets:
</p>
<div class="example">
<pre class="example">OBJDIR := objdir
OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
$(OBJDIR)/%.o : %.c
$(COMPILE.c) $(OUTPUT_OPTION) $<
all: $(OBJS)
$(OBJS): | $(OBJDIR)
$(OBJDIR):
mkdir $(OBJDIR)
</pre></div>
<p>Now the rule to create the <samp>objdir</samp> directory will be run, if
needed, before any ‘<samp>.o</samp>’ is built, but no ‘<samp>.o</samp>’ will be built
because the <samp>objdir</samp> directory timestamp changed.
</p>
<hr>
<a name="Wildcards"></a>
<div class="header">
<p>
Next: <a href="#Directory-Search" accesskey="n" rel="next">Directory Search</a>, Previous: <a href="#Prerequisite-Types" accesskey="p" rel="prev">Prerequisite Types</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Using-Wildcard-Characters-in-File-Names"></a>
<h3 class="section">4.4 Using Wildcard Characters in File Names</h3>
<a name="index-wildcard"></a>
<a name="index-file-name-with-wildcards"></a>
<a name="index-globbing-_0028wildcards_0029"></a>
<a name="index-_002a-_0028wildcard-character_0029"></a>
<a name="index-_003f-_0028wildcard-character_0029"></a>
<a name="index-_005b_2026_005d-_0028wildcard-characters_0029"></a>
<p>A single file name can specify many files using <em>wildcard characters</em>.
The wildcard characters in <code>make</code> are ‘<samp>*</samp>’, ‘<samp>?</samp>’ and
‘<samp>[…]</samp>’, the same as in the Bourne shell. For example, <samp>*.c</samp>
specifies a list of all the files (in the working directory) whose names
end in ‘<samp>.c</samp>’.
</p>
<a name="index-_007e-_0028tilde_0029"></a>
<a name="index-tilde-_0028_007e_0029"></a>
<a name="index-home-directory"></a>
<p>The character ‘<samp>~</samp>’ at the beginning of a file name also has special
significance. If alone, or followed by a slash, it represents your home
directory. For example <samp>~/bin</samp> expands to <samp>/home/you/bin</samp>.
If the ‘<samp>~</samp>’ is followed by a word, the string represents the home
directory of the user named by that word. For example <samp>~john/bin</samp>
expands to <samp>/home/john/bin</samp>. On systems which don’t have a home
directory for each user (such as MS-DOS or MS-Windows), this
functionality can be simulated by setting the environment variable
<var>HOME</var>.
</p>
<p>Wildcard expansion is performed by <code>make</code> automatically in
targets and in prerequisites. In recipes, the shell is responsible
for wildcard expansion. In other contexts, wildcard expansion happens
only if you request it explicitly with the <code>wildcard</code> function.
</p>
<p>The special significance of a wildcard character can be turned off by
preceding it with a backslash. Thus, <samp>foo\*bar</samp> would refer to a
specific file whose name consists of ‘<samp>foo</samp>’, an asterisk, and
‘<samp>bar</samp>’.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Wildcard-Examples" accesskey="1">Wildcard Examples</a>:</td><td> </td><td align="left" valign="top">Several examples.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Wildcard-Pitfall" accesskey="2">Wildcard Pitfall</a>:</td><td> </td><td align="left" valign="top">Problems to avoid.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Wildcard-Function" accesskey="3">Wildcard Function</a>:</td><td> </td><td align="left" valign="top">How to cause wildcard expansion where
it does not normally take place.
</td></tr>
</table>
<hr>
<a name="Wildcard-Examples"></a>
<div class="header">
<p>
Next: <a href="#Wildcard-Pitfall" accesskey="n" rel="next">Wildcard Pitfall</a>, Previous: <a href="#Wildcards" accesskey="p" rel="prev">Wildcards</a>, Up: <a href="#Wildcards" accesskey="u" rel="up">Wildcards</a> [<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="Wildcard-Examples-1"></a>
<h4 class="subsection">4.4.1 Wildcard Examples</h4>
<p>Wildcards can be used in the recipe of a rule, where they are expanded
by the shell. For example, here is a rule to delete all the object files:
</p>
<div class="example">
<pre class="example">clean:
rm -f *.o
</pre></div>
<a name="index-rm-_0028shell-command_0029-1"></a>
<p>Wildcards are also useful in the prerequisites of a rule. With the
following rule in the makefile, ‘<samp>make print</samp>’ will print all the
‘<samp>.c</samp>’ files that have changed since the last time you printed them:
</p>
<div class="example">
<pre class="example">print: *.c
lpr -p $?
touch print
</pre></div>
<a name="index-print-target"></a>
<a name="index-lpr-_0028shell-command_0029"></a>
<a name="index-touch-_0028shell-command_0029"></a>
<p>This rule uses <samp>print</samp> as an empty target file; see <a href="#Empty-Targets">Empty Target Files to Record Events</a>. (The automatic variable
‘<samp>$?</samp>’ is used to print only those files that have changed; see
<a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>.)
</p>
<p>Wildcard expansion does not happen when you define a variable. Thus, if
you write this:
</p>
<div class="example">
<pre class="example">objects = *.o
</pre></div>
<p>then the value of the variable <code>objects</code> is the actual string
‘<samp>*.o</samp>’. However, if you use the value of <code>objects</code> in a
target or prerequisite, wildcard expansion will take place there. If
you use the value of <code>objects</code> in a recipe, the shell may perform
wildcard expansion when the recipe runs. To set <code>objects</code> to the
expansion, instead use:
</p>
<div class="example">
<pre class="example">objects := $(wildcard *.o)
</pre></div>
<p>See <a href="#Wildcard-Function">Wildcard Function</a>.
</p>
<hr>
<a name="Wildcard-Pitfall"></a>
<div class="header">
<p>
Next: <a href="#Wildcard-Function" accesskey="n" rel="next">Wildcard Function</a>, Previous: <a href="#Wildcard-Examples" accesskey="p" rel="prev">Wildcard Examples</a>, Up: <a href="#Wildcards" accesskey="u" rel="up">Wildcards</a> [<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="Pitfalls-of-Using-Wildcards"></a>
<h4 class="subsection">4.4.2 Pitfalls of Using Wildcards</h4>
<a name="index-wildcard-pitfalls"></a>
<a name="index-pitfalls-of-wildcards"></a>
<a name="index-mistakes-with-wildcards"></a>
<a name="index-errors-with-wildcards"></a>
<a name="index-problems-with-wildcards"></a>
<p>Now here is an example of a naive way of using wildcard expansion, that
does not do what you would intend. Suppose you would like to say that the
executable file <samp>foo</samp> is made from all the object files in the
directory, and you write this:
</p>
<div class="example">
<pre class="example">objects = *.o
foo : $(objects)
cc -o foo $(CFLAGS) $(objects)
</pre></div>
<p>The value of <code>objects</code> is the actual string ‘<samp>*.o</samp>’. Wildcard
expansion happens in the rule for <samp>foo</samp>, so that each <em>existing</em>
‘<samp>.o</samp>’ file becomes a prerequisite of <samp>foo</samp> and will be recompiled if
necessary.
</p>
<p>But what if you delete all the ‘<samp>.o</samp>’ files? When a wildcard matches
no files, it is left as it is, so then <samp>foo</samp> will depend on the
oddly-named file <samp>*.o</samp>. Since no such file is likely to exist,
<code>make</code> will give you an error saying it cannot figure out how to
make <samp>*.o</samp>. This is not what you want!
</p>
<p>Actually it is possible to obtain the desired result with wildcard
expansion, but you need more sophisticated techniques, including the
<code>wildcard</code> function and string substitution.
See <a href="#Wildcard-Function">The Function <code>wildcard</code></a>.
</p>
<a name="index-wildcards-and-MS_002dDOS_002fMS_002dWindows-backslashes"></a>
<a name="index-backslashes-in-pathnames-and-wildcard-expansion"></a>
<p>Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
separate directories in pathnames, like so:
</p>
<div class="example">
<pre class="example"> c:\foo\bar\baz.c
</pre></div>
<p>This is equivalent to the Unix-style <samp>c:/foo/bar/baz.c</samp> (the
<samp>c:</samp> part is the so-called drive letter). When <code>make</code> runs on
these systems, it supports backslashes as well as the Unix-style forward
slashes in pathnames. However, this support does <em>not</em> include the
wildcard expansion, where backslash is a quote character. Therefore,
you <em>must</em> use Unix-style slashes in these cases.
</p>
<hr>
<a name="Wildcard-Function"></a>
<div class="header">
<p>
Previous: <a href="#Wildcard-Pitfall" accesskey="p" rel="prev">Wildcard Pitfall</a>, Up: <a href="#Wildcards" accesskey="u" rel="up">Wildcards</a> [<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-Function-wildcard"></a>
<h4 class="subsection">4.4.3 The Function <code>wildcard</code></h4>
<a name="index-wildcard-1"></a>
<p>Wildcard expansion happens automatically in rules. But wildcard expansion
does not normally take place when a variable is set, or inside the
arguments of a function. If you want to do wildcard expansion in such
places, you need to use the <code>wildcard</code> function, like this:
</p>
<div class="example">
<pre class="example">$(wildcard <var>pattern</var>…)
</pre></div>
<p>This string, used anywhere in a makefile, is replaced by a
space-separated list of names of existing files that match one of the
given file name patterns. If no existing file name matches a pattern,
then that pattern is omitted from the output of the <code>wildcard</code>
function. Note that this is different from how unmatched wildcards
behave in rules, where they are used verbatim rather than ignored
(see <a href="#Wildcard-Pitfall">Wildcard Pitfall</a>).
</p>
<p>One use of the <code>wildcard</code> function is to get a list of all the C source
files in a directory, like this:
</p>
<div class="example">
<pre class="example">$(wildcard *.c)
</pre></div>
<p>We can change the list of C source files into a list of object files by
replacing the ‘<samp>.c</samp>’ suffix with ‘<samp>.o</samp>’ in the result, like this:
</p>
<div class="example">
<pre class="example">$(patsubst %.c,%.o,$(wildcard *.c))
</pre></div>
<p>(Here we have used another function, <code>patsubst</code>.
See <a href="Functions.html#Text-Functions">Functions for String Substitution and Analysis</a>.)
</p>
<p>Thus, a makefile to compile all C source files in the directory and then
link them together could be written as follows:
</p>
<div class="example">
<pre class="example">objects := $(patsubst %.c,%.o,$(wildcard *.c))
foo : $(objects)
cc -o foo $(objects)
</pre></div>
<p>(This takes advantage of the implicit rule for compiling C programs, so
there is no need to write explicit rules for compiling the files.
See <a href="Using-Variables.html#Flavors">The Two Flavors of Variables</a>, for an explanation of
‘<samp>:=</samp>’, which is a variant of ‘<samp>=</samp>’.)
</p>
<hr>
<a name="Directory-Search"></a>
<div class="header">
<p>
Next: <a href="#Phony-Targets" accesskey="n" rel="next">Phony Targets</a>, Previous: <a href="#Wildcards" accesskey="p" rel="prev">Wildcards</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Searching-Directories-for-Prerequisites"></a>
<h3 class="section">4.5 Searching Directories for Prerequisites</h3>
<a name="index-VPATH"></a>
<a name="index-vpath-1"></a>
<a name="index-vpath"></a>
<a name="index-search-path-for-prerequisites-_0028VPATH_0029"></a>
<a name="index-directory-search-_0028VPATH_0029"></a>
<p>For large systems, it is often desirable to put sources in a separate
directory from the binaries. The <em>directory search</em> features of
<code>make</code> facilitate this by searching several directories
automatically to find a prerequisite. When you redistribute the files
among directories, you do not need to change the individual rules,
just the search paths.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#General-Search" accesskey="1">General Search</a>:</td><td> </td><td align="left" valign="top">Specifying a search path that applies
to every prerequisite.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Selective-Search" accesskey="2">Selective Search</a>:</td><td> </td><td align="left" valign="top">Specifying a search path
for a specified class of names.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Search-Algorithm" accesskey="3">Search Algorithm</a>:</td><td> </td><td align="left" valign="top">When and how search paths are applied.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Recipes_002fSearch" accesskey="4">Recipes/Search</a>:</td><td> </td><td align="left" valign="top">How to write recipes that work together
with search paths.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Implicit_002fSearch" accesskey="5">Implicit/Search</a>:</td><td> </td><td align="left" valign="top">How search paths affect implicit rules.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Libraries_002fSearch" accesskey="6">Libraries/Search</a>:</td><td> </td><td align="left" valign="top">Directory search for link libraries.
</td></tr>
</table>
<hr>
<a name="General-Search"></a>
<div class="header">
<p>
Next: <a href="#Selective-Search" accesskey="n" rel="next">Selective Search</a>, Previous: <a href="#Directory-Search" accesskey="p" rel="prev">Directory Search</a>, Up: <a href="#Directory-Search" accesskey="u" rel="up">Directory Search</a> [<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="VPATH_003a-Search-Path-for-All-Prerequisites"></a>
<h4 class="subsection">4.5.1 <code>VPATH</code>: Search Path for All Prerequisites</h4>
<a name="index-VPATH-1"></a>
<p>The value of the <code>make</code> variable <code>VPATH</code> specifies a list of
directories that <code>make</code> should search. Most often, the
directories are expected to contain prerequisite files that are not in the
current directory; however, <code>make</code> uses <code>VPATH</code> as a search
list for both prerequisites and targets of rules.
</p>
<p>Thus, if a file that is listed as a target or prerequisite does not exist
in the current directory, <code>make</code> searches the directories listed in
<code>VPATH</code> for a file with that name. If a file is found in one of
them, that file may become the prerequisite (see below). Rules may then
specify the names of files in the prerequisite list as if they all
existed in the current directory. See <a href="#Recipes_002fSearch">Writing Recipes with Directory Search</a>.
</p>
<p>In the <code>VPATH</code> variable, directory names are separated by colons or
blanks. The order in which directories are listed is the order followed
by <code>make</code> in its search. (On MS-DOS and MS-Windows, semi-colons
are used as separators of directory names in <code>VPATH</code>, since the
colon can be used in the pathname itself, after the drive letter.)
</p>
<p>For example,
</p>
<div class="example">
<pre class="example">VPATH = src:../headers
</pre></div>
<p>specifies a path containing two directories, <samp>src</samp> and
<samp>../headers</samp>, which <code>make</code> searches in that order.
</p>
<p>With this value of <code>VPATH</code>, the following rule,
</p>
<div class="example">
<pre class="example">foo.o : foo.c
</pre></div>
<p>is interpreted as if it were written like this:
</p>
<div class="example">
<pre class="example">foo.o : src/foo.c
</pre></div>
<p>assuming the file <samp>foo.c</samp> does not exist in the current directory but
is found in the directory <samp>src</samp>.
</p>
<hr>
<a name="Selective-Search"></a>
<div class="header">
<p>
Next: <a href="#Search-Algorithm" accesskey="n" rel="next">Search Algorithm</a>, Previous: <a href="#General-Search" accesskey="p" rel="prev">General Search</a>, Up: <a href="#Directory-Search" accesskey="u" rel="up">Directory Search</a> [<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-vpath-Directive"></a>
<h4 class="subsection">4.5.2 The <code>vpath</code> Directive</h4>
<a name="index-vpath-2"></a>
<p>Similar to the <code>VPATH</code> variable, but more selective, is the
<code>vpath</code> directive (note lower case), which allows you to specify a
search path for a particular class of file names: those that match a
particular pattern. Thus you can supply certain search directories for
one class of file names and other directories (or none) for other file
names.
</p>
<p>There are three forms of the <code>vpath</code> directive:
</p>
<dl compact="compact">
<dt><code>vpath <var>pattern</var> <var>directories</var></code></dt>
<dd><p>Specify the search path <var>directories</var> for file names that match
<var>pattern</var>.
</p>
<p>The search path, <var>directories</var>, is a list of directories to be
searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
blanks, just like the search path used in the <code>VPATH</code> variable.
</p>
</dd>
<dt><code>vpath <var>pattern</var></code></dt>
<dd><p>Clear out the search path associated with <var>pattern</var>.
</p>
</dd>
<dt><code>vpath</code></dt>
<dd>
<p>Clear all search paths previously specified with <code>vpath</code> directives.
</p></dd>
</dl>
<p>A <code>vpath</code> pattern is a string containing a ‘<samp>%</samp>’ character. The
string must match the file name of a prerequisite that is being searched
for, the ‘<samp>%</samp>’ character matching any sequence of zero or more
characters (as in pattern rules; see <a href="Implicit-Rules.html#Pattern-Rules">Defining and
Redefining Pattern Rules</a>). For example, <code>%.h</code> matches files that
end in <code>.h</code>. (If there is no ‘<samp>%</samp>’, the pattern must match the
prerequisite exactly, which is not useful very often.)
</p>
<a name="index-_0025_002c-quoting-in-vpath"></a>
<a name="index-_0025_002c-quoting-with-_005c-_0028backslash_0029"></a>
<a name="index-_005c-_0028backslash_0029_002c-to-quote-_0025"></a>
<a name="index-backslash-_0028_005c_0029_002c-to-quote-_0025"></a>
<a name="index-quoting-_0025_002c-in-vpath"></a>
<p>‘<samp>%</samp>’ characters in a <code>vpath</code> directive’s pattern can be quoted
with preceding backslashes (‘<samp>\</samp>’). Backslashes that would otherwise
quote ‘<samp>%</samp>’ characters can be quoted with more backslashes.
Backslashes that quote ‘<samp>%</samp>’ characters or other backslashes are
removed from the pattern before it is compared to file names. Backslashes
that are not in danger of quoting ‘<samp>%</samp>’ characters go unmolested.
</p>
<p>When a prerequisite fails to exist in the current directory, if the
<var>pattern</var> in a <code>vpath</code> directive matches the name of the
prerequisite file, then the <var>directories</var> in that directive are searched
just like (and before) the directories in the <code>VPATH</code> variable.
</p>
<p>For example,
</p>
<div class="example">
<pre class="example">vpath %.h ../headers
</pre></div>
<p>tells <code>make</code> to look for any prerequisite whose name ends in <samp>.h</samp>
in the directory <samp>../headers</samp> if the file is not found in the current
directory.
</p>
<p>If several <code>vpath</code> patterns match the prerequisite file’s name, then
<code>make</code> processes each matching <code>vpath</code> directive one by one,
searching all the directories mentioned in each directive. <code>make</code>
handles multiple <code>vpath</code> directives in the order in which they
appear in the makefile; multiple directives with the same pattern are
independent of each other.
</p>
<p>Thus,
</p>
<div class="example">
<pre class="example">vpath %.c foo
vpath % blish
vpath %.c bar
</pre></div>
<p>will look for a file ending in ‘<samp>.c</samp>’ in <samp>foo</samp>, then
<samp>blish</samp>, then <samp>bar</samp>, while
</p>
<div class="example">
<pre class="example">vpath %.c foo:bar
vpath % blish
</pre></div>
<p>will look for a file ending in ‘<samp>.c</samp>’ in <samp>foo</samp>, then
<samp>bar</samp>, then <samp>blish</samp>.
</p>
<hr>
<a name="Search-Algorithm"></a>
<div class="header">
<p>
Next: <a href="#Recipes_002fSearch" accesskey="n" rel="next">Recipes/Search</a>, Previous: <a href="#Selective-Search" accesskey="p" rel="prev">Selective Search</a>, Up: <a href="#Directory-Search" accesskey="u" rel="up">Directory Search</a> [<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-Directory-Searches-are-Performed"></a>
<h4 class="subsection">4.5.3 How Directory Searches are Performed</h4>
<a name="index-algorithm-for-directory-search"></a>
<a name="index-directory-search-algorithm"></a>
<p>When a prerequisite is found through directory search, regardless of type
(general or selective), the pathname located may not be the one that
<code>make</code> actually provides you in the prerequisite list. Sometimes
the path discovered through directory search is thrown away.
</p>
<p>The algorithm <code>make</code> uses to decide whether to keep or abandon a
path found via directory search is as follows:
</p>
<ol>
<li> If a target file does not exist at the path specified in the makefile,
directory search is performed.
</li><li> If the directory search is successful, that path is kept and this file
is tentatively stored as the target.
</li><li> All prerequisites of this target are examined using this same method.
</li><li> After processing the prerequisites, the target may or may not need to be
rebuilt:
<ol>
<li> If the target does <em>not</em> need to be rebuilt, the path to the file
found during directory search is used for any prerequisite lists which
contain this target. In short, if <code>make</code> doesn’t need to rebuild
the target then you use the path found via directory search.
</li><li> If the target <em>does</em> need to be rebuilt (is out-of-date), the
pathname found during directory search is <em>thrown away</em>, and the
target is rebuilt using the file name specified in the makefile. In
short, if <code>make</code> must rebuild, then the target is rebuilt locally,
not in the directory found via directory search.
</li></ol>
</li></ol>
<p>This algorithm may seem complex, but in practice it is quite often
exactly what you want.
</p>
<a name="index-traditional-directory-search-_0028GPATH_0029"></a>
<a name="index-directory-search_002c-traditional-_0028GPATH_0029"></a>
<p>Other versions of <code>make</code> use a simpler algorithm: if the file does
not exist, and it is found via directory search, then that pathname is
always used whether or not the target needs to be built. Thus, if the
target is rebuilt it is created at the pathname discovered during
directory search.
</p>
<a name="index-GPATH"></a>
<p>If, in fact, this is the behavior you want for some or all of your
directories, you can use the <code>GPATH</code> variable to indicate this to
<code>make</code>.
</p>
<p><code>GPATH</code> has the same syntax and format as <code>VPATH</code> (that is, a
space- or colon-delimited list of pathnames). If an out-of-date target
is found by directory search in a directory that also appears in
<code>GPATH</code>, then that pathname is not thrown away. The target is
rebuilt using the expanded path.
</p>
<hr>
<a name="Recipes_002fSearch"></a>
<div class="header">
<p>
Next: <a href="#Implicit_002fSearch" accesskey="n" rel="next">Implicit/Search</a>, Previous: <a href="#Search-Algorithm" accesskey="p" rel="prev">Search Algorithm</a>, Up: <a href="#Directory-Search" accesskey="u" rel="up">Directory Search</a> [<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="Writing-Recipes-with-Directory-Search"></a>
<h4 class="subsection">4.5.4 Writing Recipes with Directory Search</h4>
<a name="index-recipes_002c-and-directory-search"></a>
<a name="index-directory-search-_0028VPATH_0029_002c-and-recipes"></a>
<p>When a prerequisite is found in another directory through directory search,
this cannot change the recipe of the rule; they will execute as written.
Therefore, you must write the recipe with care so that it will look for
the prerequisite in the directory where <code>make</code> finds it.
</p>
<p>This is done with the <em>automatic variables</em> such as ‘<samp>$^</samp>’
(see <a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>).
For instance, the value of ‘<samp>$^</samp>’ is a
list of all the prerequisites of the rule, including the names of
the directories in which they were found, and the value of
‘<samp>$@</samp>’ is the target. Thus:
</p>
<div class="example">
<pre class="example">foo.o : foo.c
cc -c $(CFLAGS) $^ -o $@
</pre></div>
<p>(The variable <code>CFLAGS</code> exists so you can specify flags for C
compilation by implicit rules; we use it here for consistency so it will
affect all C compilations uniformly;
see <a href="Implicit-Rules.html#Implicit-Variables">Variables Used by Implicit Rules</a>.)
</p>
<p>Often the prerequisites include header files as well, which you do not
want to mention in the recipe. The automatic variable ‘<samp>$<</samp>’ is
just the first prerequisite:
</p>
<div class="example">
<pre class="example">VPATH = src:../headers
foo.o : foo.c defs.h hack.h
cc -c $(CFLAGS) $< -o $@
</pre></div>
<hr>
<a name="Implicit_002fSearch"></a>
<div class="header">
<p>
Next: <a href="#Libraries_002fSearch" accesskey="n" rel="next">Libraries/Search</a>, Previous: <a href="#Recipes_002fSearch" accesskey="p" rel="prev">Recipes/Search</a>, Up: <a href="#Directory-Search" accesskey="u" rel="up">Directory Search</a> [<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="Directory-Search-and-Implicit-Rules"></a>
<h4 class="subsection">4.5.5 Directory Search and Implicit Rules</h4>
<a name="index-VPATH_002c-and-implicit-rules"></a>
<a name="index-directory-search-_0028VPATH_0029_002c-and-implicit-rules"></a>
<a name="index-search-path-for-prerequisites-_0028VPATH_0029_002c-and-implicit-rules"></a>
<a name="index-implicit-rule_002c-and-directory-search"></a>
<a name="index-implicit-rule_002c-and-VPATH"></a>
<a name="index-rule_002c-implicit_002c-and-directory-search"></a>
<a name="index-rule_002c-implicit_002c-and-VPATH"></a>
<p>The search through the directories specified in <code>VPATH</code> or with
<code>vpath</code> also happens during consideration of implicit rules
(see <a href="Implicit-Rules.html#Implicit-Rules">Using Implicit Rules</a>).
</p>
<p>For example, when a file <samp>foo.o</samp> has no explicit rule, <code>make</code>
considers implicit rules, such as the built-in rule to compile
<samp>foo.c</samp> if that file exists. If such a file is lacking in the
current directory, the appropriate directories are searched for it. If
<samp>foo.c</samp> exists (or is mentioned in the makefile) in any of the
directories, the implicit rule for C compilation is applied.
</p>
<p>The recipes of implicit rules normally use automatic variables as a
matter of necessity; consequently they will use the file names found by
directory search with no extra effort.
</p>
<hr>
<a name="Libraries_002fSearch"></a>
<div class="header">
<p>
Previous: <a href="#Implicit_002fSearch" accesskey="p" rel="prev">Implicit/Search</a>, Up: <a href="#Directory-Search" accesskey="u" rel="up">Directory Search</a> [<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="Directory-Search-for-Link-Libraries"></a>
<h4 class="subsection">4.5.6 Directory Search for Link Libraries</h4>
<a name="index-link-libraries_002c-and-directory-search"></a>
<a name="index-libraries-for-linking_002c-directory-search"></a>
<a name="index-directory-search-_0028VPATH_0029_002c-and-link-libraries"></a>
<a name="index-VPATH_002c-and-link-libraries"></a>
<a name="index-search-path-for-prerequisites-_0028VPATH_0029_002c-and-link-libraries"></a>
<a name="index-_002dl-_0028library-search_0029"></a>
<a name="index-link-libraries_002c-patterns-matching"></a>
<a name="index-_002eLIBPATTERNS_002c-and-link-libraries"></a>
<a name="index-_002eLIBPATTERNS"></a>
<p>Directory search applies in a special way to libraries used with the
linker. This special feature comes into play when you write a prerequisite
whose name is of the form ‘<samp>-l<var>name</var></samp>’. (You can tell something
strange is going on here because the prerequisite is normally the name of a
file, and the <em>file name</em> of a library generally looks like
<samp>lib<var>name</var>.a</samp>, not like ‘<samp>-l<var>name</var></samp>’.)
</p>
<p>When a prerequisite’s name has the form ‘<samp>-l<var>name</var></samp>’, <code>make</code>
handles it specially by searching for the file <samp>lib<var>name</var>.so</samp>,
and, if it is not found, for the file <samp>lib<var>name</var>.a</samp> in the current
directory, in directories specified by matching <code>vpath</code>
search paths and the <code>VPATH</code> search path, and then in the
directories <samp>/lib</samp>, <samp>/usr/lib</samp>, and <samp><var>prefix</var>/lib</samp>
(normally <samp>/usr/local/lib</samp>, but MS-DOS/MS-Windows versions of
<code>make</code> behave as if <var>prefix</var> is defined to be the root of the
DJGPP installation tree).
</p>
<p>For example, if there is a <samp>/usr/lib/libcurses.a</samp> library on your
system (and no <samp>/usr/lib/libcurses.so</samp> file), then
</p>
<div class="example">
<pre class="example">foo : foo.c -lcurses
cc $^ -o $@
</pre></div>
<p>would cause the command ‘<samp>cc foo.c /usr/lib/libcurses.a -o foo</samp>’ to
be executed when <samp>foo</samp> is older than <samp>foo.c</samp> or than
<samp>/usr/lib/libcurses.a</samp>.
</p>
<p>Although the default set of files to be searched for is
<samp>lib<var>name</var>.so</samp> and <samp>lib<var>name</var>.a</samp>, this is customizable
via the <code>.LIBPATTERNS</code> variable. Each word in the value of this
variable is a pattern string. When a prerequisite like
‘<samp>-l<var>name</var></samp>’ is seen, <code>make</code> will replace the percent in
each pattern in the list with <var>name</var> and perform the above directory
searches using each library file name.
</p>
<p>The default value for <code>.LIBPATTERNS</code> is ‘<samp>lib%.so lib%.a</samp>’,
which provides the default behavior described above.
</p>
<p>You can turn off link library expansion completely by setting this
variable to an empty value.
</p>
<hr>
<a name="Phony-Targets"></a>
<div class="header">
<p>
Next: <a href="#Force-Targets" accesskey="n" rel="next">Force Targets</a>, Previous: <a href="#Directory-Search" accesskey="p" rel="prev">Directory Search</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Phony-Targets-1"></a>
<h3 class="section">4.6 Phony Targets</h3>
<a name="index-phony-targets"></a>
<a name="index-targets_002c-phony"></a>
<a name="index-targets-without-a-file"></a>
<p>A phony target is one that is not really the name of a file; rather it
is just a name for a recipe to be executed when you make an explicit
request. There are two reasons to use a phony target: to avoid a
conflict with a file of the same name, and to improve performance.
</p>
<p>If you write a rule whose recipe will not create the target file, the
recipe will be executed every time the target comes up for remaking.
Here is an example:
</p>
<div class="example">
<pre class="example">clean:
rm *.o temp
</pre></div>
<p>Because the <code>rm</code> command does not create a file named <samp>clean</samp>,
probably no such file will ever exist. Therefore, the <code>rm</code> command
will be executed every time you say ‘<samp>make clean</samp>’.
<a name="index-rm-_0028shell-command_0029-2"></a>
</p>
<a name="index-_002ePHONY"></a>
<p>In this example, the <samp>clean</samp> target will not work properly if a
file named <samp>clean</samp> is ever created in this directory. Since it
has no prerequisites, <samp>clean</samp> would always be considered up to
date and its recipe would not be executed. To avoid this problem you
can explicitly declare the target to be phony by making it a
prerequisite of the special target <code>.PHONY</code>
(see <a href="#Special-Targets">Special Built-in Target Names</a>) as follows:
</p>
<div class="example">
<pre class="example">.PHONY: clean
clean:
rm *.o temp
</pre></div>
<p>Once this is done, ‘<samp>make clean</samp>’ will run the recipe regardless of
whether there is a file named <samp>clean</samp>.
</p>
<p>Phony targets are also useful in conjunction with recursive
invocations of <code>make</code> (see <a href="Recipes.html#Recursion">Recursive Use of <code>make</code></a>).
In this situation the makefile will often contain a variable which
lists a number of sub-directories to be built. A simplistic way to
handle this is to define one rule with a recipe that loops over the
sub-directories, like this:
</p>
<div class="example">
<pre class="example">SUBDIRS = foo bar baz
subdirs:
for dir in $(SUBDIRS); do \
$(MAKE) -C $$dir; \
done
</pre></div>
<p>There are problems with this method, however. First, any error
detected in a sub-make is ignored by this rule, so it will continue
to build the rest of the directories even when one fails. This can be
overcome by adding shell commands to note the error and exit, but then
it will do so even if <code>make</code> is invoked with the <code>-k</code>
option, which is unfortunate. Second, and perhaps more importantly,
you cannot take advantage of <code>make</code>’s ability to build targets in
parallel (see <a href="Recipes.html#Parallel">Parallel Execution</a>), since there is only
one rule.
</p>
<p>By declaring the sub-directories as <code>.PHONY</code> targets (you must do
this as the sub-directory obviously always exists; otherwise it won’t
be built) you can remove these problems:
</p>
<div class="example">
<pre class="example">SUBDIRS = foo bar baz
.PHONY: subdirs $(SUBDIRS)
subdirs: $(SUBDIRS)
$(SUBDIRS):
$(MAKE) -C $@
foo: baz
</pre></div>
<p>Here we’ve also declared that the <samp>foo</samp> sub-directory cannot be
built until after the <samp>baz</samp> sub-directory is complete; this kind of
relationship declaration is particularly important when attempting
parallel builds.
</p>
<p>The implicit rule search (see <a href="Implicit-Rules.html#Implicit-Rules">Implicit Rules</a>) is skipped for
<code>.PHONY</code> targets. This is why declaring a target as
<code>.PHONY</code> is good for performance, even if you are not worried
about the actual file existing.
</p>
<p>A phony target should not be a prerequisite of a real target file; if it
is, its recipe will be run every time <code>make</code> goes to update that
file. As long as a phony target is never a prerequisite of a real
target, the phony target recipe will be executed only when the phony
target is a specified goal (see <a href="Running.html#Goals">Arguments to Specify the
Goals</a>).
</p>
<p>Phony targets can have prerequisites. When one directory contains multiple
programs, it is most convenient to describe all of the programs in one
makefile <samp>./Makefile</samp>. Since the target remade by default will be the
first one in the makefile, it is common to make this a phony target named
‘<samp>all</samp>’ and give it, as prerequisites, all the individual programs. For
example:
</p>
<div class="example">
<pre class="example">all : prog1 prog2 prog3
.PHONY : all
prog1 : prog1.o utils.o
cc -o prog1 prog1.o utils.o
prog2 : prog2.o
cc -o prog2 prog2.o
prog3 : prog3.o sort.o utils.o
cc -o prog3 prog3.o sort.o utils.o
</pre></div>
<p>Now you can say just ‘<samp>make</samp>’ to remake all three programs, or
specify as arguments the ones to remake (as in ‘<samp>make prog1
prog3</samp>’). Phoniness is not inherited: the prerequisites of a phony
target are not themselves phony, unless explicitly declared to be so.
</p>
<p>When one phony target is a prerequisite of another, it serves as a subroutine
of the other. For example, here ‘<samp>make cleanall</samp>’ will delete the
object files, the difference files, and the file <samp>program</samp>:
</p>
<div class="example">
<pre class="example">.PHONY: cleanall cleanobj cleandiff
cleanall : cleanobj cleandiff
rm program
cleanobj :
rm *.o
cleandiff :
rm *.diff
</pre></div>
<hr>
<a name="Force-Targets"></a>
<div class="header">
<p>
Next: <a href="#Empty-Targets" accesskey="n" rel="next">Empty Targets</a>, Previous: <a href="#Phony-Targets" accesskey="p" rel="prev">Phony Targets</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Rules-without-Recipes-or-Prerequisites"></a>
<h3 class="section">4.7 Rules without Recipes or Prerequisites</h3>
<a name="index-force-targets"></a>
<a name="index-targets_002c-force"></a>
<a name="index-FORCE"></a>
<a name="index-rule_002c-no-recipe-or-prerequisites"></a>
<p>If a rule has no prerequisites or recipe, and the target of the rule
is a nonexistent file, then <code>make</code> imagines this target to have
been updated whenever its rule is run. This implies that all targets
depending on this one will always have their recipe run.
</p>
<p>An example will illustrate this:
</p>
<div class="example">
<pre class="example">clean: FORCE
rm $(objects)
FORCE:
</pre></div>
<p>Here the target ‘<samp>FORCE</samp>’ satisfies the special conditions, so the
target <samp>clean</samp> that depends on it is forced to run its recipe.
There is nothing special about the name ‘<samp>FORCE</samp>’, but that is one
name commonly used this way.
</p>
<p>As you can see, using ‘<samp>FORCE</samp>’ this way has the same results as using
‘<samp>.PHONY: clean</samp>’.
</p>
<p>Using ‘<samp>.PHONY</samp>’ is more explicit and more efficient. However,
other versions of <code>make</code> do not support ‘<samp>.PHONY</samp>’; thus
‘<samp>FORCE</samp>’ appears in many makefiles. See <a href="#Phony-Targets">Phony Targets</a>.
</p>
<hr>
<a name="Empty-Targets"></a>
<div class="header">
<p>
Next: <a href="#Special-Targets" accesskey="n" rel="next">Special Targets</a>, Previous: <a href="#Force-Targets" accesskey="p" rel="prev">Force Targets</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Empty-Target-Files-to-Record-Events"></a>
<h3 class="section">4.8 Empty Target Files to Record Events</h3>
<a name="index-empty-targets"></a>
<a name="index-targets_002c-empty"></a>
<a name="index-recording-events-with-empty-targets"></a>
<p>The <em>empty target</em> is a variant of the phony target; it is used to hold
recipes for an action that you request explicitly from time to time.
Unlike a phony target, this target file can really exist; but the file’s
contents do not matter, and usually are empty.
</p>
<p>The purpose of the empty target file is to record, with its
last-modification time, when the rule’s recipe was last executed. It
does so because one of the commands in the recipe is a <code>touch</code>
command to update the target file.
</p>
<p>The empty target file should have some prerequisites (otherwise it
doesn’t make sense). When you ask to remake the empty target, the
recipe is executed if any prerequisite is more recent than the target;
in other words, if a prerequisite has changed since the last time you
remade the target. Here is an example:
</p>
<div class="example">
<pre class="example">print: foo.c bar.c
lpr -p $?
touch print
</pre></div>
<a name="index-print-target-1"></a>
<a name="index-lpr-_0028shell-command_0029-1"></a>
<a name="index-touch-_0028shell-command_0029-1"></a>
<p>With this rule, ‘<samp>make print</samp>’ will execute the <code>lpr</code> command if
either source file has changed since the last ‘<samp>make print</samp>’. The
automatic variable ‘<samp>$?</samp>’ is used to print only those files that have
changed (see <a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>).
</p>
<hr>
<a name="Special-Targets"></a>
<div class="header">
<p>
Next: <a href="#Multiple-Targets" accesskey="n" rel="next">Multiple Targets</a>, Previous: <a href="#Empty-Targets" accesskey="p" rel="prev">Empty Targets</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Special-Built_002din-Target-Names"></a>
<h3 class="section">4.9 Special Built-in Target Names</h3>
<a name="index-special-targets"></a>
<a name="index-built_002din-special-targets"></a>
<a name="index-targets_002c-built_002din-special"></a>
<p>Certain names have special meanings if they appear as targets.
</p>
<dl compact="compact">
<dd><a name="index-_002ePHONY-1"></a>
</dd>
<dt><code>.PHONY</code></dt>
<dd>
<p>The prerequisites of the special target <code>.PHONY</code> are considered to
be phony targets. When it is time to consider such a target,
<code>make</code> will run its recipe unconditionally, regardless of
whether a file with that name exists or what its last-modification
time is. See <a href="#Phony-Targets">Phony Targets</a>.
</p>
<a name="index-_002eSUFFIXES"></a>
</dd>
<dt><code>.SUFFIXES</code></dt>
<dd>
<p>The prerequisites of the special target <code>.SUFFIXES</code> are the list
of suffixes to be used in checking for suffix rules.
See <a href="Implicit-Rules.html#Suffix-Rules">Old-Fashioned Suffix Rules</a>.
</p>
<a name="index-_002eDEFAULT"></a>
</dd>
<dt><code>.DEFAULT</code></dt>
<dd>
<p>The recipe specified for <code>.DEFAULT</code> is used for any target for
which no rules are found (either explicit rules or implicit rules).
See <a href="Implicit-Rules.html#Last-Resort">Last Resort</a>. If a <code>.DEFAULT</code> recipe is specified, every
file mentioned as a prerequisite, but not as a target in a rule, will have
that recipe executed on its behalf. See <a href="Implicit-Rules.html#Implicit-Rule-Search">Implicit Rule Search Algorithm</a>.
</p>
<a name="index-_002ePRECIOUS"></a>
</dd>
<dt><code>.PRECIOUS</code></dt>
<dd><a name="index-precious-targets"></a>
<a name="index-preserving-with-_002ePRECIOUS"></a>
<p>The targets which <code>.PRECIOUS</code> depends on are given the following
special treatment: if <code>make</code> is killed or interrupted during the
execution of their recipes, the target is not deleted.
See <a href="Recipes.html#Interrupts">Interrupting or Killing <code>make</code></a>. Also, if the
target is an intermediate file, it will not be deleted after it is no
longer needed, as is normally done. See <a href="Implicit-Rules.html#Chained-Rules">Chains of
Implicit Rules</a>. In this latter respect it overlaps with the
<code>.SECONDARY</code> special target.
</p>
<p>You can also list the target pattern of an implicit rule (such as
‘<samp>%.o</samp>’) as a prerequisite file of the special target <code>.PRECIOUS</code>
to preserve intermediate files created by rules whose target patterns
match that file’s name.
</p>
<a name="index-_002eINTERMEDIATE"></a>
</dd>
<dt><code>.INTERMEDIATE</code></dt>
<dd><a name="index-intermediate-targets_002c-explicit"></a>
<p>The targets which <code>.INTERMEDIATE</code> depends on are treated as
intermediate files. See <a href="Implicit-Rules.html#Chained-Rules">Chains of Implicit Rules</a>.
<code>.INTERMEDIATE</code> with no prerequisites has no effect.
</p>
<a name="index-_002eSECONDARY"></a>
</dd>
<dt><code>.SECONDARY</code></dt>
<dd><a name="index-secondary-targets"></a>
<a name="index-preserving-with-_002eSECONDARY"></a>
<p>The targets which <code>.SECONDARY</code> depends on are treated as
intermediate files, except that they are never automatically deleted.
See <a href="Implicit-Rules.html#Chained-Rules">Chains of Implicit Rules</a>.
</p>
<p><code>.SECONDARY</code> with no prerequisites causes all targets to be treated
as secondary (i.e., no target is removed because it is considered
intermediate).
</p>
<a name="index-_002eSECONDEXPANSION-1"></a>
</dd>
<dt><code>.SECONDEXPANSION</code></dt>
<dd>
<p>If <code>.SECONDEXPANSION</code> is mentioned as a target anywhere in the
makefile, then all prerequisite lists defined <em>after</em> it appears
will be expanded a second time after all makefiles have been read in.
See <a href="Makefiles.html#Secondary-Expansion">Secondary Expansion</a>.
</p>
<a name="index-_002eDELETE_005fON_005fERROR"></a>
</dd>
<dt><code>.DELETE_ON_ERROR</code></dt>
<dd><a name="index-removing-targets-on-failure"></a>
<p>If <code>.DELETE_ON_ERROR</code> is mentioned as a target anywhere in the
makefile, then <code>make</code> will delete the target of a rule if it has
changed and its recipe exits with a nonzero exit status, just as it
does when it receives a signal. See <a href="Recipes.html#Errors">Errors in Recipes</a>.
</p>
<a name="index-_002eIGNORE"></a>
</dd>
<dt><code>.IGNORE</code></dt>
<dd>
<p>If you specify prerequisites for <code>.IGNORE</code>, then <code>make</code> will
ignore errors in execution of the recipe for those particular files.
The recipe for <code>.IGNORE</code> (if any) is ignored.
</p>
<p>If mentioned as a target with no prerequisites, <code>.IGNORE</code> says to
ignore errors in execution of recipes for all files. This usage of
‘<samp>.IGNORE</samp>’ is supported only for historical compatibility. Since
this affects every recipe in the makefile, it is not very useful; we
recommend you use the more selective ways to ignore errors in specific
recipes. See <a href="Recipes.html#Errors">Errors in Recipes</a>.
</p>
<a name="index-_002eLOW_005fRESOLUTION_005fTIME"></a>
</dd>
<dt><code>.LOW_RESOLUTION_TIME</code></dt>
<dd>
<p>If you specify prerequisites for <code>.LOW_RESOLUTION_TIME</code>,
<code>make</code> assumes that these files are created by commands that
generate low resolution time stamps. The recipe for the
<code>.LOW_RESOLUTION_TIME</code> target are ignored.
</p>
<p>The high resolution file time stamps of many modern file systems
lessen the chance of <code>make</code> incorrectly concluding that a file
is up to date. Unfortunately, some hosts do not provide a way to set a
high resolution file time stamp, so commands like ‘<samp>cp -p</samp>’ that
explicitly set a file’s time stamp must discard its sub-second part.
If a file is created by such a command, you should list it as a
prerequisite of <code>.LOW_RESOLUTION_TIME</code> so that <code>make</code>
does not mistakenly conclude that the file is out of date. For
example:
</p>
<div class="example">
<pre class="example">.LOW_RESOLUTION_TIME: dst
dst: src
cp -p src dst
</pre></div>
<p>Since ‘<samp>cp -p</samp>’ discards the sub-second part of <samp>src</samp>’s time
stamp, <samp>dst</samp> is typically slightly older than <samp>src</samp> even when
it is up to date. The <code>.LOW_RESOLUTION_TIME</code> line causes
<code>make</code> to consider <samp>dst</samp> to be up to date if its time stamp
is at the start of the same second that <samp>src</samp>’s time stamp is in.
</p>
<p>Due to a limitation of the archive format, archive member time stamps
are always low resolution. You need not list archive members as
prerequisites of <code>.LOW_RESOLUTION_TIME</code>, as <code>make</code> does this
automatically.
</p>
<a name="index-_002eSILENT"></a>
</dd>
<dt><code>.SILENT</code></dt>
<dd>
<p>If you specify prerequisites for <code>.SILENT</code>, then <code>make</code> will
not print the recipe used to remake those particular files before
executing them. The recipe for <code>.SILENT</code> is ignored.
</p>
<p>If mentioned as a target with no prerequisites, <code>.SILENT</code> says not
to print any recipes before executing them. This usage of
‘<samp>.SILENT</samp>’ is supported only for historical compatibility. We
recommend you use the more selective ways to silence specific recipes.
See <a href="Recipes.html#Echoing">Recipe Echoing</a>. If you want to silence all recipes
for a particular run of <code>make</code>, use the ‘<samp>-s</samp>’ or
‘<samp><span class="nolinebreak">--silent</span></samp>’<!-- /@w --> option (see <a href="Running.html#Options-Summary">Options Summary</a>).
</p>
<a name="index-_002eEXPORT_005fALL_005fVARIABLES"></a>
</dd>
<dt><code>.EXPORT_ALL_VARIABLES</code></dt>
<dd>
<p>Simply by being mentioned as a target, this tells <code>make</code> to
export all variables to child processes by default.
See <a href="Recipes.html#Variables_002fRecursion">Communicating Variables to a
Sub-<code>make</code></a>.
</p>
<a name="index-_002eNOTPARALLEL"></a>
</dd>
<dt><code>.NOTPARALLEL</code></dt>
<dd><a name="index-parallel-execution_002c-overriding"></a>
<p>If <code>.NOTPARALLEL</code> is mentioned as a target, then this invocation
of <code>make</code> will be run serially, even if the ‘<samp>-j</samp>’ option is
given. Any recursively invoked <code>make</code> command will still run
recipes in parallel (unless its makefile also contains this target).
Any prerequisites on this target are ignored.
</p>
<a name="index-_002eONESHELL"></a>
</dd>
<dt><code>.ONESHELL</code></dt>
<dd><a name="index-recipe-execution_002c-single-invocation"></a>
<p>If <code>.ONESHELL</code> is mentioned as a target, then when a target is
built all lines of the recipe will be given to a single invocation of
the shell rather than each line being invoked separately
(see <a href="Recipes.html#Execution">Recipe Execution</a>).
</p>
<a name="index-_002ePOSIX"></a>
</dd>
<dt><code>.POSIX</code></dt>
<dd><a name="index-POSIX_002dconforming-mode_002c-setting"></a>
<p>If <code>.POSIX</code> is mentioned as a target, then the makefile will be
parsed and run in POSIX-conforming mode. This does <em>not</em> mean
that only POSIX-conforming makefiles will be accepted: all advanced
GNU <code>make</code> features are still available. Rather, this target
causes <code>make</code> to behave as required by POSIX in those areas
where <code>make</code>’s default behavior differs.
</p>
<p>In particular, if this target is mentioned then recipes will be
invoked as if the shell had been passed the <code>-e</code> flag: the first
failing command in a recipe will cause the recipe to fail immediately.
</p></dd>
</dl>
<p>Any defined implicit rule suffix also counts as a special target if it
appears as a target, and so does the concatenation of two suffixes, such
as ‘<samp>.c.o</samp>’. These targets are suffix rules, an obsolete way of
defining implicit rules (but a way still widely used). In principle, any
target name could be special in this way if you break it in two and add
both pieces to the suffix list. In practice, suffixes normally begin with
‘<samp>.</samp>’, so these special target names also begin with ‘<samp>.</samp>’.
See <a href="Implicit-Rules.html#Suffix-Rules">Old-Fashioned Suffix Rules</a>.
</p>
<hr>
<a name="Multiple-Targets"></a>
<div class="header">
<p>
Next: <a href="#Multiple-Rules" accesskey="n" rel="next">Multiple Rules</a>, Previous: <a href="#Special-Targets" accesskey="p" rel="prev">Special Targets</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Multiple-Targets-in-a-Rule"></a>
<h3 class="section">4.10 Multiple Targets in a Rule</h3>
<a name="index-multiple-targets"></a>
<a name="index-several-targets-in-a-rule"></a>
<a name="index-targets_002c-multiple"></a>
<a name="index-rule_002c-with-multiple-targets"></a>
<p>A rule with multiple targets is equivalent to writing many rules, each with
one target, and all identical aside from that. The same recipe applies to
all the targets, but its effect may vary because you can substitute the
actual target name into the recipe using ‘<samp>$@</samp>’. The rule contributes
the same prerequisites to all the targets also.
</p>
<p>This is useful in two cases.
</p>
<ul>
<li> You want just prerequisites, no recipe. For example:
<div class="example">
<pre class="example">kbd.o command.o files.o: command.h
</pre></div>
<p>gives an additional prerequisite to each of the three object files
mentioned.
</p>
</li><li> Similar recipes work for all the targets. The recipes do not need
to be absolutely identical, since the automatic variable ‘<samp>$@</samp>’
can be used to substitute the particular target to be remade into the
commands (see <a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>). For example:
<div class="example">
<pre class="example">bigoutput littleoutput : text.g
generate text.g -$(subst output,,$@) > $@
</pre></div>
<a name="index-subst"></a>
<p>is equivalent to
</p>
<div class="example">
<pre class="example">bigoutput : text.g
generate text.g -big > bigoutput
littleoutput : text.g
generate text.g -little > littleoutput
</pre></div>
<p>Here we assume the hypothetical program <code>generate</code> makes two
types of output, one if given ‘<samp>-big</samp>’ and one if given
‘<samp>-little</samp>’.
See <a href="Functions.html#Text-Functions">Functions for String Substitution and Analysis</a>,
for an explanation of the <code>subst</code> function.
</p></li></ul>
<p>Suppose you would like to vary the prerequisites according to the
target, much as the variable ‘<samp>$@</samp>’ allows you to vary the recipe.
You cannot do this with multiple targets in an ordinary rule, but you
can do it with a <em>static pattern rule</em>. See <a href="#Static-Pattern">Static Pattern Rules</a>.
</p>
<hr>
<a name="Multiple-Rules"></a>
<div class="header">
<p>
Next: <a href="#Static-Pattern" accesskey="n" rel="next">Static Pattern</a>, Previous: <a href="#Multiple-Targets" accesskey="p" rel="prev">Multiple Targets</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Multiple-Rules-for-One-Target"></a>
<h3 class="section">4.11 Multiple Rules for One Target</h3>
<a name="index-multiple-rules-for-one-target"></a>
<a name="index-several-rules-for-one-target"></a>
<a name="index-rule_002c-multiple-for-one-target"></a>
<a name="index-target_002c-multiple-rules-for-one"></a>
<p>One file can be the target of several rules. All the prerequisites
mentioned in all the rules are merged into one list of prerequisites for
the target. If the target is older than any prerequisite from any rule,
the recipe is executed.
</p>
<p>There can only be one recipe to be executed for a file. If more than
one rule gives a recipe for the same file, <code>make</code> uses the last
one given and prints an error message. (As a special case, if the
file’s name begins with a dot, no error message is printed. This odd
behavior is only for compatibility with other implementations of
<code>make</code>… you should avoid using it). Occasionally it is
useful to have the same target invoke multiple recipes which are
defined in different parts of your makefile; you can use
<em>double-colon rules</em> (see <a href="#Double_002dColon">Double-Colon</a>) for this.
</p>
<p>An extra rule with just prerequisites can be used to give a few extra
prerequisites to many files at once. For example, makefiles often
have a variable, such as <code>objects</code>, containing a list of all the
compiler output files in the system being made. An easy way to say
that all of them must be recompiled if <samp>config.h</samp> changes is to
write the following:
</p>
<div class="example">
<pre class="example">objects = foo.o bar.o
foo.o : defs.h
bar.o : defs.h test.h
$(objects) : config.h
</pre></div>
<p>This could be inserted or taken out without changing the rules that really
specify how to make the object files, making it a convenient form to use if
you wish to add the additional prerequisite intermittently.
</p>
<p>Another wrinkle is that the additional prerequisites could be
specified with a variable that you set with a command line argument to
<code>make</code> (see <a href="Running.html#Overriding">Overriding Variables</a>). For example,
</p>
<div class="example">
<pre class="example">extradeps=
$(objects) : $(extradeps)
</pre></div>
<p>means that the command ‘<samp>make extradeps=foo.h</samp>’ will consider
<samp>foo.h</samp> as a prerequisite of each object file, but plain ‘<samp>make</samp>’
will not.
</p>
<p>If none of the explicit rules for a target has a recipe, then <code>make</code>
searches for an applicable implicit rule to find one
see <a href="Implicit-Rules.html#Implicit-Rules">Using Implicit Rules</a>).
</p>
<hr>
<a name="Static-Pattern"></a>
<div class="header">
<p>
Next: <a href="#Double_002dColon" accesskey="n" rel="next">Double-Colon</a>, Previous: <a href="#Multiple-Rules" accesskey="p" rel="prev">Multiple Rules</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Static-Pattern-Rules"></a>
<h3 class="section">4.12 Static Pattern Rules</h3>
<a name="index-static-pattern-rule"></a>
<a name="index-rule_002c-static-pattern"></a>
<a name="index-pattern-rules_002c-static-_0028not-implicit_0029"></a>
<a name="index-varying-prerequisites"></a>
<a name="index-prerequisites_002c-varying-_0028static-pattern_0029"></a>
<p><em>Static pattern rules</em> are rules which specify multiple targets and
construct the prerequisite names for each target based on the target name.
They are more general than ordinary rules with multiple targets because the
targets do not have to have identical prerequisites. Their prerequisites must
be <em>analogous</em>, but not necessarily <em>identical</em>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Static-Usage" accesskey="1">Static Usage</a>:</td><td> </td><td align="left" valign="top">The syntax of static pattern rules.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Static-versus-Implicit" accesskey="2">Static versus Implicit</a>:</td><td> </td><td align="left" valign="top">When are they better than implicit rules?
</td></tr>
</table>
<hr>
<a name="Static-Usage"></a>
<div class="header">
<p>
Next: <a href="#Static-versus-Implicit" accesskey="n" rel="next">Static versus Implicit</a>, Previous: <a href="#Static-Pattern" accesskey="p" rel="prev">Static Pattern</a>, Up: <a href="#Static-Pattern" accesskey="u" rel="up">Static Pattern</a> [<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="Syntax-of-Static-Pattern-Rules"></a>
<h4 class="subsection">4.12.1 Syntax of Static Pattern Rules</h4>
<a name="index-static-pattern-rule_002c-syntax-of"></a>
<a name="index-pattern-rules_002c-static_002c-syntax-of"></a>
<p>Here is the syntax of a static pattern rule:
</p>
<div class="example">
<pre class="example"><var>targets</var> …: <var>target-pattern</var>: <var>prereq-patterns</var> …
<var>recipe</var>
…
</pre></div>
<p>The <var>targets</var> list specifies the targets that the rule applies to.
The targets can contain wildcard characters, just like the targets of
ordinary rules (see <a href="#Wildcards">Using Wildcard Characters in File
Names</a>).
</p>
<a name="index-target-pattern_002c-static-_0028not-implicit_0029"></a>
<a name="index-stem"></a>
<p>The <var>target-pattern</var> and <var>prereq-patterns</var> say how to compute the
prerequisites of each target. Each target is matched against the
<var>target-pattern</var> to extract a part of the target name, called the
<em>stem</em>. This stem is substituted into each of the <var>prereq-patterns</var>
to make the prerequisite names (one from each <var>prereq-pattern</var>).
</p>
<p>Each pattern normally contains the character ‘<samp>%</samp>’ just once. When the
<var>target-pattern</var> matches a target, the ‘<samp>%</samp>’ can match any part of
the target name; this part is called the <em>stem</em>. The rest of the
pattern must match exactly. For example, the target <samp>foo.o</samp> matches
the pattern ‘<samp>%.o</samp>’, with ‘<samp>foo</samp>’ as the stem. The targets
<samp>foo.c</samp> and <samp>foo.out</samp> do not match that pattern.
</p>
<a name="index-prerequisite-pattern_002c-static-_0028not-implicit_0029"></a>
<p>The prerequisite names for each target are made by substituting the stem
for the ‘<samp>%</samp>’ in each prerequisite pattern. For example, if one
prerequisite pattern is <samp>%.c</samp>, then substitution of the stem
‘<samp>foo</samp>’ gives the prerequisite name <samp>foo.c</samp>. It is legitimate
to write a prerequisite pattern that does not contain ‘<samp>%</samp>’; then this
prerequisite is the same for all targets.
</p>
<a name="index-_0025_002c-quoting-in-static-pattern"></a>
<a name="index-_0025_002c-quoting-with-_005c-_0028backslash_0029-1"></a>
<a name="index-_005c-_0028backslash_0029_002c-to-quote-_0025-1"></a>
<a name="index-backslash-_0028_005c_0029_002c-to-quote-_0025-1"></a>
<a name="index-quoting-_0025_002c-in-static-pattern"></a>
<p>‘<samp>%</samp>’ characters in pattern rules can be quoted with preceding
backslashes (‘<samp>\</samp>’). Backslashes that would otherwise quote ‘<samp>%</samp>’
characters can be quoted with more backslashes. Backslashes that quote
‘<samp>%</samp>’ characters or other backslashes are removed from the pattern
before it is compared to file names or has a stem substituted into it.
Backslashes that are not in danger of quoting ‘<samp>%</samp>’ characters go
unmolested. For example, the pattern <samp>the\%weird\\%pattern\\</samp> has
‘<samp>the%weird\</samp>’ preceding the operative ‘<samp>%</samp>’ character, and
‘<samp>pattern\\</samp>’ following it. The final two backslashes are left alone
because they cannot affect any ‘<samp>%</samp>’ character.
</p>
<p>Here is an example, which compiles each of <samp>foo.o</samp> and <samp>bar.o</samp>
from the corresponding <samp>.c</samp> file:
</p>
<div class="example">
<pre class="example">objects = foo.o bar.o
all: $(objects)
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
</pre></div>
<p>Here ‘<samp>$<</samp>’ is the automatic variable that holds the name of the
prerequisite and ‘<samp>$@</samp>’ is the automatic variable that holds the name
of the target; see <a href="Implicit-Rules.html#Automatic-Variables">Automatic Variables</a>.
</p>
<p>Each target specified must match the target pattern; a warning is issued
for each target that does not. If you have a list of files, only some of
which will match the pattern, you can use the <code>filter</code> function to
remove non-matching file names (see <a href="Functions.html#Text-Functions">Functions for String Substitution and Analysis</a>):
</p>
<div class="example">
<pre class="example">files = foo.elc bar.o lose.o
$(filter %.o,$(files)): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
$(filter %.elc,$(files)): %.elc: %.el
emacs -f batch-byte-compile $<
</pre></div>
<p>In this example the result of ‘<samp>$(filter %.o,$(files))</samp>’ is
<samp>bar.o lose.o</samp>, and the first static pattern rule causes each of
these object files to be updated by compiling the corresponding C source
file. The result of ‘<samp>$(filter %.elc,$(files))</samp>’<!-- /@w --> is
<samp>foo.elc</samp>, so that file is made from <samp>foo.el</samp>.
</p>
<p>Another example shows how to use <code>$*</code> in static pattern rules:
<a name="index-_0024_002a_002c-and-static-pattern"></a>
</p>
<div class="example">
<pre class="example">bigoutput littleoutput : %output : text.g
generate text.g -$* > $@
</pre></div>
<p>When the <code>generate</code> command is run, <code>$*</code> will expand to the
stem, either ‘<samp>big</samp>’ or ‘<samp>little</samp>’.
</p>
<hr>
<a name="Static-versus-Implicit"></a>
<div class="header">
<p>
Previous: <a href="#Static-Usage" accesskey="p" rel="prev">Static Usage</a>, Up: <a href="#Static-Pattern" accesskey="u" rel="up">Static Pattern</a> [<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="Static-Pattern-Rules-versus-Implicit-Rules"></a>
<h4 class="subsection">4.12.2 Static Pattern Rules versus Implicit Rules</h4>
<a name="index-rule_002c-static-pattern-versus-implicit"></a>
<a name="index-static-pattern-rule_002c-versus-implicit"></a>
<p>A static pattern rule has much in common with an implicit rule defined as a
pattern rule (see <a href="Implicit-Rules.html#Pattern-Rules">Defining and Redefining Pattern Rules</a>).
Both have a pattern for the target and patterns for constructing the
names of prerequisites. The difference is in how <code>make</code> decides
<em>when</em> the rule applies.
</p>
<p>An implicit rule <em>can</em> apply to any target that matches its pattern,
but it <em>does</em> apply only when the target has no recipe otherwise
specified, and only when the prerequisites can be found. If more than one
implicit rule appears applicable, only one applies; the choice depends on
the order of rules.
</p>
<p>By contrast, a static pattern rule applies to the precise list of targets
that you specify in the rule. It cannot apply to any other target and it
invariably does apply to each of the targets specified. If two conflicting
rules apply, and both have recipes, that’s an error.
</p>
<p>The static pattern rule can be better than an implicit rule for these
reasons:
</p>
<ul>
<li> You may wish to override the usual implicit rule for a few
files whose names cannot be categorized syntactically but
can be given in an explicit list.
</li><li> If you cannot be sure of the precise contents of the directories
you are using, you may not be sure which other irrelevant files
might lead <code>make</code> to use the wrong implicit rule. The choice
might depend on the order in which the implicit rule search is done.
With static pattern rules, there is no uncertainty: each rule applies
to precisely the targets specified.
</li></ul>
<hr>
<a name="Double_002dColon"></a>
<div class="header">
<p>
Next: <a href="#Automatic-Prerequisites" accesskey="n" rel="next">Automatic Prerequisites</a>, Previous: <a href="#Static-Pattern" accesskey="p" rel="prev">Static Pattern</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Double_002dColon-Rules"></a>
<h3 class="section">4.13 Double-Colon Rules</h3>
<a name="index-double_002dcolon-rules"></a>
<a name="index-rule_002c-double_002dcolon-_0028_003a_003a_0029"></a>
<a name="index-multiple-rules-for-one-target-_0028_003a_003a_0029"></a>
<a name="index-_003a_003a-rules-_0028double_002dcolon_0029"></a>
<p><em>Double-colon</em> rules are explicit rules written with ‘<samp>::</samp>’
instead of ‘<samp>:</samp>’ after the target names. They are handled
differently from ordinary rules when the same target appears in more
than one rule. Pattern rules with double-colons have an entirely
different meaning (see <a href="Implicit-Rules.html#Match_002dAnything-Rules">Match-Anything Rules</a>).
</p>
<p>When a target appears in multiple rules, all the rules must be the same
type: all ordinary, or all double-colon. If they are double-colon, each
of them is independent of the others. Each double-colon rule’s recipe
is executed if the target is older than any prerequisites of that rule.
If there are no prerequisites for that rule, its recipe is always
executed (even if the target already exists). This can result in
executing none, any, or all of the double-colon rules.
</p>
<p>Double-colon rules with the same target are in fact completely separate
from one another. Each double-colon rule is processed individually, just
as rules with different targets are processed.
</p>
<p>The double-colon rules for a target are executed in the order they appear
in the makefile. However, the cases where double-colon rules really make
sense are those where the order of executing the recipes would not matter.
</p>
<p>Double-colon rules are somewhat obscure and not often very useful; they
provide a mechanism for cases in which the method used to update a target
differs depending on which prerequisite files caused the update, and such
cases are rare.
</p>
<p>Each double-colon rule should specify a recipe; if it does not, an
implicit rule will be used if one applies.
See <a href="Implicit-Rules.html#Implicit-Rules">Using Implicit Rules</a>.
</p>
<hr>
<a name="Automatic-Prerequisites"></a>
<div class="header">
<p>
Previous: <a href="#Double_002dColon" accesskey="p" rel="prev">Double-Colon</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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="Generating-Prerequisites-Automatically"></a>
<h3 class="section">4.14 Generating Prerequisites Automatically</h3>
<a name="index-prerequisites_002c-automatic-generation-1"></a>
<a name="index-automatic-generation-of-prerequisites-1"></a>
<a name="index-generating-prerequisites-automatically-1"></a>
<p>In the makefile for a program, many of the rules you need to write often
say only that some object file depends on some header
file. For example, if <samp>main.c</samp> uses <samp>defs.h</samp> via an
<code>#include</code>, you would write:
</p>
<div class="example">
<pre class="example">main.o: defs.h
</pre></div>
<p>You need this rule so that <code>make</code> knows that it must remake
<samp>main.o</samp> whenever <samp>defs.h</samp> changes. You can see that for a
large program you would have to write dozens of such rules in your
makefile. And, you must always be very careful to update the makefile
every time you add or remove an <code>#include</code>.
<a name="index-_0023include"></a>
</p>
<a name="index-_002dM-_0028to-compiler_0029"></a>
<p>To avoid this hassle, most modern C compilers can write these rules for
you, by looking at the <code>#include</code> lines in the source files.
Usually this is done with the ‘<samp>-M</samp>’ option to the compiler.
For example, the command:
</p>
<div class="example">
<pre class="example">cc -M main.c
</pre></div>
<p>generates the output:
</p>
<div class="example">
<pre class="example">main.o : main.c defs.h
</pre></div>
<p>Thus you no longer have to write all those rules yourself.
The compiler will do it for you.
</p>
<p>Note that such a rule constitutes mentioning <samp>main.o</samp> in a
makefile, so it can never be considered an intermediate file by
implicit rule search. This means that <code>make</code> won’t ever remove
the file after using it; see <a href="Implicit-Rules.html#Chained-Rules">Chains of Implicit
Rules</a>.
</p>
<a name="index-make-depend"></a>
<p>With old <code>make</code> programs, it was traditional practice to use this
compiler feature to generate prerequisites on demand with a command like
‘<samp>make depend</samp>’. That command would create a file <samp>depend</samp>
containing all the automatically-generated prerequisites; then the
makefile could use <code>include</code> to read them in (see <a href="Makefiles.html#Include">Include</a>).
</p>
<p>In GNU <code>make</code>, the feature of remaking makefiles makes this
practice obsolete—you need never tell <code>make</code> explicitly to
regenerate the prerequisites, because it always regenerates any makefile
that is out of date. See <a href="Makefiles.html#Remaking-Makefiles">Remaking Makefiles</a>.
</p>
<p>The practice we recommend for automatic prerequisite generation is to have
one makefile corresponding to each source file. For each source file
<samp><var>name</var>.c</samp> there is a makefile <samp><var>name</var>.d</samp> which lists
what files the object file <samp><var>name</var>.o</samp> depends on. That way
only the source files that have changed need to be rescanned to produce
the new prerequisites.
</p>
<p>Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
called <samp><var>name</var>.d</samp> from a C source file called <samp><var>name</var>.c</samp>:
</p>
<div class="smallexample">
<pre class="smallexample">%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
</pre></div>
<p>See <a href="Implicit-Rules.html#Pattern-Rules">Pattern Rules</a>, for information on defining pattern rules. The
‘<samp>-e</samp>’ flag to the shell causes it to exit immediately if the
<code>$(CC)</code> command (or any other command) fails (exits with a
nonzero status).
<a name="index-_002de-_0028shell-flag_0029"></a>
</p>
<a name="index-_002dMM-_0028to-GNU-compiler_0029"></a>
<p>With the GNU C compiler, you may wish to use the ‘<samp>-MM</samp>’ flag instead
of ‘<samp>-M</samp>’. This omits prerequisites on system header files.
See <a href="http://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html#Preprocessor-Options">Options Controlling the Preprocessor</a> in <cite>Using GNU CC</cite>, for details.
</p>
<a name="index-sed-_0028shell-command_0029"></a>
<p>The purpose of the <code>sed</code> command is to translate (for example):
</p>
<div class="example">
<pre class="example">main.o : main.c defs.h
</pre></div>
<p>into:
</p>
<div class="example">
<pre class="example">main.o main.d : main.c defs.h
</pre></div>
<a name="index-_002ed"></a>
<p>This makes each ‘<samp>.d</samp>’ file depend on all the source and header files
that the corresponding ‘<samp>.o</samp>’ file depends on. <code>make</code> then
knows it must regenerate the prerequisites whenever any of the source or
header files changes.
</p>
<p>Once you’ve defined the rule to remake the ‘<samp>.d</samp>’ files,
you then use the <code>include</code> directive to read them all in.
See <a href="Makefiles.html#Include">Include</a>. For example:
</p>
<div class="example">
<pre class="example">sources = foo.c bar.c
include $(sources:.c=.d)
</pre></div>
<p>(This example uses a substitution variable reference to translate the
list of source files ‘<samp>foo.c bar.c</samp>’ into a list of prerequisite
makefiles, ‘<samp>foo.d bar.d</samp>’. See <a href="Using-Variables.html#Substitution-Refs">Substitution Refs</a>, for full
information on substitution references.) Since the ‘<samp>.d</samp>’ files are
makefiles like any others, <code>make</code> will remake them as necessary
with no further work from you. See <a href="Makefiles.html#Remaking-Makefiles">Remaking Makefiles</a>.
</p>
<p>Note that the ‘<samp>.d</samp>’ files contain target definitions; you should
be sure to place the <code>include</code> directive <em>after</em> the first,
default goal in your makefiles or run the risk of having a random
object file become the default goal.
See <a href="Introduction.html#How-Make-Works">How Make Works</a>.
</p>
<hr>
<div class="header">
<p>
Previous: <a href="#Double_002dColon" accesskey="p" rel="prev">Double-Colon</a>, Up: <a href="#Rules" accesskey="u" rel="up">Rules</a> [<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>
|