You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>If you mostly understand Make, consider checking out the Makefile Cookbook, which has a template for medium sized projects with ample comments about what each part of the Makefile is doing.</p>
440
440
<p>Good luck, and I hope you are able to slay the confusing world of Makefiles!</p>
441
441
<h1id="getting-started">Getting Started</h1>
442
-
<h2id="why-do-makefiles-exist-">Why do Makefiles exist?</h2>
442
+
<h2id="why-do-makefiles-exist">Why do Makefiles exist?</h2>
443
443
<p>Makefiles are used to help decide which parts of a large program need to be recompiled. In the vast majority of cases, C or C++ files are compiled. Other languages typically have their own tools that serve a similar purpose as Make. It can be used beyond programs too, when you need a series of instructions to run depending on what files have changed. This tutorial will focus on the C/C++ compilation use case.</p>
444
444
<p>Here's an example dependency graph that you might build with Make. If any file's dependencies changes, then the file will get recompiled:</p>
445
445
<divclass="center">
446
446
<imgsrc="/assets/dependency_graph.png">
447
447
</div>
448
448
449
-
<h2id="what-alternatives-are-there-to-make-">What alternatives are there to Make?</h2>
449
+
<h2id="what-alternatives-are-there-to-make">What alternatives are there to Make?</h2>
450
450
<p>Popular C/C++ alternative build systems are <ahref="https://scons.org/">SCons</a>, <ahref="https://cmake.org/">CMake</a>, <ahref="https://bazel.build/">Bazel</a>, and <ahref="https://ninja-build.org/">Ninja</a>. Some code editors like <ahref="https://visualstudio.microsoft.com/">Microsoft Visual Studio</a> have their own built in build tools. For Java, there's <ahref="https://ant.apache.org/">Ant</a>, <ahref="https://maven.apache.org/what-is-maven.html">Maven</a>, and <ahref="https://gradle.org/">Gradle</a>. Other languages like Go and Rust have their own build tools.</p>
451
451
<p>Interpreted languages like Python, Ruby, and Javascript don't require an analogue to Makefiles. The goal of Makefiles is to compile whatever files need to be compiled, based on what files have changed. But when files in interpreted languages change, nothing needs to get recompiled. When the program runs, the most recent version of the file is used.</p>
452
452
<h2id="the-versions-and-types-of-make">The versions and types of Make</h2>
<p>Add an <code>@</code> before a command to stop it from being printed<br>You can also run make with <code>-s</code> to add an <code>@</code> before each line </p>
echo <spanclass="hljs-string">"Hello from bash"</span></code></pre>
812
812
813
-
<h2id="error-handling-with-k-i-and-">Error handling with <code>-k</code>, <code>-i</code>, and <code>-</code></h2>
813
+
<h2id="error-handling-with--k--i-and--">Error handling with <code>-k</code>, <code>-i</code>, and <code>-</code></h2>
814
814
<!-- (Section 5.4) -->
815
815
<p>Add <code>-k</code> when running make to continue running even in the face of errors. Helpful if you want to see all the errors of Make at once.<br>Add a <code>-</code> before a command to suppress the error<br>Add <code>-i</code> to make to have this happen for every command.</p>
816
816
<!-- (Section 5.4) -->
@@ -888,6 +888,7 @@ <h2 id="use-export-for-recursive-make">Use export for recursive make</h2>
888
888
889
889
<h2id="arguments-to-make">Arguments to make</h2>
890
890
<!-- (Section 9) -->
891
+
891
892
<p>There's a nice <ahref="http://www.gnu.org/software/make/manual/make.html#Options-Summary">list of options</a> that can be run from make. Check out <code>--dry-run</code>, <code>--touch</code>, <code>--old-file</code>. </p>
892
893
<p>You can have multiple targets to make, i.e. <code>make clean run test</code> runs the <code>clean</code> goal, then <code>run</code>, and then <code>test</code>.</p>
<p>This example shows you how to test make flags with <code>findstring</code> and <code>MAKEFLAGS</code>. Run this example with <code>make -i</code> to see it print out the echo statement.</p>
<p><code>$(patsubst pattern,replacement,text)</code> does the following:</p>
1092
1094
<p>"Finds whitespace-separated words in text that match pattern and replaces them with replacement. Here pattern may contain a ‘%’ which acts as a wildcard, matching any number of any characters within a word. If replacement also contains a ‘%’, the ‘%’ is replaced by the text that matched the ‘%’ in pattern. Only the first ‘%’ in the pattern and replacement is treated this way; any subsequent ‘%’ is unchanged." (<ahref="https://www.gnu.org/software/make/manual/html_node/Text-Functions.html#Text-Functions">GNU docs</a>)</p>
<p>Adding <code>.PHONY</code> to a target will prevent make from confusing the phony target with a file name. In this example, if the file <code>clean</code> is created, make clean will still be run. <code>.PHONY</code> is great to use, but I'll skip it in the rest of the examples for simplicity.</p>
<p>The make tool will stop running a rule (and will propogate back to prerequisites) if a command returns a nonzero exit status.<br><code>DELETE_ON_ERROR</code> will delete the target of a rule if the rule fails in this manner. This will happen for all targets, not just the one it is before like PHONY. It's a good idea to always use this, even though make does not for historical reasons. </p>
0 commit comments