|
172 | 172 |
|
173 | 173 |
|
174 | 174 | <ol>
|
175 |
| - <li><a class="toc-item" href="#static-pattern-rules">Static Pattern Rules</a> |
| 175 | + <li><a class="toc-item" href="#implicit-rules">Implicit Rules</a> |
176 | 176 | </li>
|
177 | 177 |
|
178 | 178 | </ol>
|
179 | 179 |
|
180 | 180 | <ol>
|
181 |
| - <li><a class="toc-item" href="#static-pattern-rules-and-filter">Static Pattern Rules and Filter</a> |
| 181 | + <li><a class="toc-item" href="#static-pattern-rules">Static Pattern Rules</a> |
182 | 182 | </li>
|
183 | 183 |
|
184 | 184 | </ol>
|
185 | 185 |
|
186 | 186 | <ol>
|
187 |
| - <li><a class="toc-item" href="#implicit-rules">Implicit Rules</a> |
| 187 | + <li><a class="toc-item" href="#static-pattern-rules-and-filter">Static Pattern Rules and Filter</a> |
188 | 188 | </li>
|
189 | 189 |
|
190 | 190 | </ol>
|
@@ -653,13 +653,44 @@ <h2 id="automatic-variables">Automatic Variables</h2>
|
653 | 653 | rm -f hey one two</code></pre>
|
654 | 654 |
|
655 | 655 | <h1 id="fancy-rules">Fancy Rules</h1>
|
| 656 | +<h2 id="implicit-rules">Implicit Rules</h2> |
| 657 | +<!-- (Section 10) --> |
| 658 | +<p>Make loves c compilation. And every time it expresses its love, things get confusing. Perhaps the most confusing part of Make is the magic/automatic rules that are made. Make calls these "implicit" rules. I don't personally agree with this design decision, and I don't recommend using them, but they're often used and are thus useful to know. Here's a list of implicit rules:</p> |
| 659 | +<ul> |
| 660 | +<li>Compiling a C program: <code>n.o</code> is made automatically from <code>n.c</code> with a command of the form <code>$(CC) -c $(CPPFLAGS) $(CFLAGS)</code></li> |
| 661 | +<li>Compiling a C++ program: <code>n.o</code> is made automatically from <code>n.cc</code> or <code>n.cpp</code> with a command of the form <code>$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)</code></li> |
| 662 | +<li>Linking a single object file: <code>n</code> is made automatically from <code>n.o</code> by running the command <code>$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)</code></li> |
| 663 | +</ul> |
| 664 | +<p>The important variables used by implicit rules are:</p> |
| 665 | +<ul> |
| 666 | +<li><code>CC</code>: Program for compiling C programs; default <code>cc</code></li> |
| 667 | +<li><code>CXX</code>: Program for compiling C++ programs; default <code>g++</code></li> |
| 668 | +<li><code>CFLAGS</code>: Extra flags to give to the C compiler</li> |
| 669 | +<li><code>CXXFLAGS</code>: Extra flags to give to the C++ compiler</li> |
| 670 | +<li><code>CPPFLAGS</code>: Extra flags to give to the C preprocessor</li> |
| 671 | +<li><code>LDFLAGS</code>: Extra flags to give to compilers when they are supposed to invoke the linker</li> |
| 672 | +</ul> |
| 673 | +<p>Let's see how we can now build a C program without ever explicitly telling Make how to do the compililation:</p> |
| 674 | +<pre><code class="hljs makefile">CC = gcc <span class="hljs-comment"># Flag for implicit rules</span> |
| 675 | +CFLAGS = -g <span class="hljs-comment"># Flag for implicit rules. Turn on debug info</span> |
| 676 | + |
| 677 | +<span class="hljs-comment"># Implicit rule #1: blah is built via the C linker implicit rule</span> |
| 678 | +<span class="hljs-comment"># Implicit rule #2: blah.o is built via the C compilation implicit rule, because blah.c exists</span> |
| 679 | +<span class="hljs-section">blah: blah.o</span> |
| 680 | + |
| 681 | +<span class="hljs-section">blah.c:</span> |
| 682 | + echo <span class="hljs-string">"int main() { return 0; }"</span> > blah.c |
| 683 | + |
| 684 | +<span class="hljs-section">clean:</span> |
| 685 | + rm -f blah*</code></pre> |
| 686 | + |
656 | 687 | <h2 id="static-pattern-rules">Static Pattern Rules</h2>
|
657 | 688 | <!-- (Section 4.10) -->
|
658 |
| -<p>Make loves c compilation. And every time it expresses its love, things get confusing. Here's the syntax for a new type of rule called a static pattern:</p> |
| 689 | +<p>Static pattern rules are another way to write less in a Makefile, but I'd say are more useful and a bit less "magic". Here's their syntax:</p> |
659 | 690 | <pre><code class="hljs makefile">targets ...: target-pattern: prereq-patterns ...
|
660 | 691 | commands</code></pre>
|
661 | 692 |
|
662 |
| -<p>The essence is that the given target is matched by the target-pattern (via a <code>%</code> wildcard). Whatever was matched is called the <em>stem</em>. The stem is then substituted into the prereq-pattern, to generate the target's prereqs.</p> |
| 693 | +<p>The essence is that the given <code>target</code> is matched by the <code>target-pattern</code> (via a <code>%</code> wildcard). Whatever was matched is called the <em>stem</em>. The stem is then substituted into the <code>prereq-pattern</code>, to generate the target's prereqs.</p> |
663 | 694 | <p>A typical use case is to compile <code>.c</code> files into <code>.o</code> files. Here's the <em>manual way</em>:</p>
|
664 | 695 | <pre><code class="hljs makefile">objects = foo.o bar.o all.o
|
665 | 696 | <span class="hljs-section">all: <span class="hljs-variable">$(objects)</span></span>
|
@@ -718,36 +749,6 @@ <h2 id="static-pattern-rules-and-filter">Static Pattern Rules and Filter</h2>
|
718 | 749 | rm -f <span class="hljs-variable">$(src_files)</span></code></pre>
|
719 | 750 |
|
720 | 751 |
|
721 |
| -<h2 id="implicit-rules">Implicit Rules</h2> |
722 |
| -<!-- (Section 10) --> |
723 |
| -<p>Perhaps the most confusing part of make is the magic rules and variables that are made. Here's a list of implicit rules:</p> |
724 |
| -<ul> |
725 |
| -<li>Compiling a C program: <code>n.o</code> is made automatically from <code>n.c</code> with a command of the form <code>$(CC) -c $(CPPFLAGS) $(CFLAGS)</code></li> |
726 |
| -<li>Compiling a C++ program: <code>n.o</code> is made automatically from <code>n.cc</code> or <code>n.cpp</code> with a command of the form <code>$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)</code></li> |
727 |
| -<li>Linking a single object file: <code>n</code> is made automatically from <code>n.o</code> by running the command <code>$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)</code></li> |
728 |
| -</ul> |
729 |
| -<p>As such, the important variables used by implicit rules are:</p> |
730 |
| -<ul> |
731 |
| -<li><code>CC</code>: Program for compiling C programs; default cc</li> |
732 |
| -<li><code>CXX</code>: Program for compiling C++ programs; default G++</li> |
733 |
| -<li><code>CFLAGS</code>: Extra flags to give to the C compiler</li> |
734 |
| -<li><code>CXXFLAGS</code>: Extra flags to give to the C++ compiler</li> |
735 |
| -<li><code>CPPFLAGS</code>: Extra flags to give to the C preprocessor</li> |
736 |
| -<li><code>LDFLAGS</code>: Extra flags to give to compilers when they are supposed to invoke the linker</li> |
737 |
| -</ul> |
738 |
| -<pre><code class="hljs makefile">CC = gcc <span class="hljs-comment"># Flag for implicit rules</span> |
739 |
| -CFLAGS = -g <span class="hljs-comment"># Flag for implicit rules. Turn on debug info</span> |
740 |
| - |
741 |
| -<span class="hljs-comment"># Implicit rule #1: blah is built via the C linker implicit rule</span> |
742 |
| -<span class="hljs-comment"># Implicit rule #2: blah.o is built via the C compilation implicit rule, because blah.c exists</span> |
743 |
| -<span class="hljs-section">blah: blah.o</span> |
744 |
| - |
745 |
| -<span class="hljs-section">blah.c:</span> |
746 |
| - echo <span class="hljs-string">"int main() { return 0; }"</span> > blah.c |
747 |
| - |
748 |
| -<span class="hljs-section">clean:</span> |
749 |
| - rm -f blah*</code></pre> |
750 |
| - |
751 | 752 | <h2 id="pattern-rules">Pattern Rules</h2>
|
752 | 753 | <p>Pattern rules are often used but quite confusing. You can look at them as two ways:</p>
|
753 | 754 | <ul>
|
|
0 commit comments