<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Makefile Tutorial | Chemeketa CS</title><link>https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/</link><atom:link href="https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/index.xml" rel="self" type="application/rss+xml"/><description>Makefile Tutorial</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><image><url>https://computerscience.chemeketa.edu/images/icon_hu_9ce158bca5f7cacc.png</url><title>Makefile Tutorial</title><link>https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/</link></image><item><title>Building Multiple Programs</title><link>https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/multiple-programs/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/multiple-programs/</guid><description>&lt;p&gt;This guide shows how to use one makefile to build multiple programs from one code base.
For example: you have code that is for a unit test project and code that is for
your main program. You can't compile it into one program as the tests and &amp;quot;main&amp;quot;
program each define a &lt;code&gt;main&lt;/code&gt; function. So you need to build two separate programs.&lt;/p&gt;
&lt;p&gt;These instructions are based on the &lt;strong&gt;MakeComplex&lt;/strong&gt; project from Week01 in the
CS260 code repository. Use it if you want to be able to follow along exactly.
The instructions assume you have a terminal open in that directory.&lt;/p&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Examine the code in &lt;strong&gt;MakeComplex&lt;/strong&gt;. There are two separate files with main functions - programA.cpp and programB.cpp.
Those represent two separate programs that both happen to use code from Answer.cpp. We need to build the two files into
two separate programs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a file called &lt;strong&gt;Makefile&lt;/strong&gt; and start with a basic set of targets for programA.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-make"&gt;programA.exe: programA.cpp Answer.cpp Answer.h Templated.h
g++ -g -Wall -o programA.exe programA.cpp Answer.cpp
.PHONY: clean
clean:
rm -f programA.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As before, the target is named after the program to build, and it
depends on all of the source files, both &lt;code&gt;.cpp&lt;/code&gt; and &lt;code&gt;.h&lt;/code&gt;, that I
might want to edit and would require the program to be rebuilt if
I did. The commands use &lt;code&gt;g++&lt;/code&gt; and list all of the &lt;code&gt;.cpp&lt;/code&gt; files (but not .h files) to
be compiled.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add a rule for programB.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-make"&gt;.PHONY: programA.exe
programA.exe: programA.cpp Answer.cpp Answer.h Templated.h
g++ -g -Wall -o programA.exe programA.cpp Answer.cpp
.PHONY: programB.exe
programB.exe: programB.cpp Answer.cpp Answer.h Templated.h
g++ -g -Wall -o programB.exe programB.cpp Answer.cpp
.PHONY: clean
clean:
rm -f programA.exe programB.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two &lt;code&gt;.exe&lt;/code&gt; rules are similar, but note that each program only
uses its own &lt;code&gt;.cpp&lt;/code&gt; file that defines &lt;code&gt;main&lt;/code&gt;. They both use the
shared files such as &lt;code&gt;Answer.cpp&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Normally &lt;strong&gt;make&lt;/strong&gt; just runs the first rule it finds. So if you type
&lt;code&gt;make&lt;/code&gt;, only programA.exe will be built. To build programB.exe, you will
have to do:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;make programB.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you want to easily be able to make both program at once, you can add a
phone all rule as the first one. It lists programA.exe and programB.exe
as prerequisites - so those rules will be invoked automatically as part
of building &lt;code&gt;all&lt;/code&gt;. The &lt;code&gt;all&lt;/code&gt; rules itself does not have any commands - all
it does is trigger both program rules to run.&lt;/p&gt;
&lt;p&gt;Since &lt;code&gt;all&lt;/code&gt; is the first rule, it is the one that runs if you run &lt;code&gt;make&lt;/code&gt;
with no arguments. You can always run just &lt;code&gt;make programA.exe&lt;/code&gt; or &lt;code&gt;make programB.exe&lt;/code&gt; if you want to specify just one.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-make"&gt;.PHONY: all
all: programA.exe programB.exe
.PHONY: programA.exe
programA.exe: programA.cpp Answer.cpp Answer.h Templated.h
g++ -g -Wall -o programA.exe programA.cpp Answer.cpp
.PHONY: programB.exe
programB.exe: programB.cpp Answer.cpp Answer.h Templated.h
g++ -g -Wall -o programB.exe programB.cpp Answer.cpp
.PHONY: clean
clean:
rm -f programA.exe programB.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Once any significant amount of information is shared, it becomes
good practice to use variables to move the repeated information to
just one place. For example, we are going to want to pass the same
flags to the compiler each time, so let's put that information into
a variable.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-make"&gt;CXXFLAGS = -g -Wall
.PHONY: all
all: programA.exe programB.exe
.PHONY: programA.exe
programA.exe: programA.cpp Answer.cpp Answer.h Templated.h
g++ $(CXXFLAGS) -o programA.exe programA.cpp Answer.cpp
.PHONY: programB.exe
programB.exe: programB.cpp Answer.cpp Answer.h Templated.h
g++ $(CXXFLAGS) -o programB.exe programB.cpp Answer.cpp
.PHONY: clean
clean:
rm -f programA.exe programB.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The new first line defines the variable, &lt;code&gt;CXXFLAGS&lt;/code&gt;, and the
commands can simply use its value as &lt;code&gt;$(CXXFLAGS)&lt;/code&gt; instead of
duplicating the list of flags. This is particularly useful once
the list of flags you want to configure gets long.&lt;/p&gt;
&lt;p&gt;We can also use variables to avoid repeating the shared file names.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-make"&gt;CXXFLAGS = -g -Wall
PROGRAM_A_FILES = programA.exe
PROGRAM_B_FILES = programB.exe
SHARED_HEADERS = Answer.h Templated.h
SHARED_FILES = Answer.cpp
.PHONY: all
all: programA.exe programB.exe
.PHONY: programA.exe
programA.exe: $(PROGRAM_A_FILES) $(SHARED_FILES) $(SHARED_HEADERS)
g++ $(CXXFLAGS) -o programA.exe $(PROGRAM_A_FILES) $(SHARED_FILES)
.PHONY: programB.exe
programB.exe: $(PROGRAM_B_FILES) $(SHARED_FILES) $(SHARED_HEADERS)
g++ $(CXXFLAGS) -o programB.exe $(PROGRAM_B_FILES) $(SHARED_FILES)
.PHONY: clean
clean:
rm -f programA.exe programB.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this sort of style, you can often control most things about
the Makefile by editing variable definitions near the top, while the
commands can sometimes get quite cryptic-looking but rarely require
editing anyway.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Making A Simple Program</title><link>https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/simple-program/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/simple-program/</guid><description>&lt;p&gt;Instructions to build a simple makefile for a single program.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;A makefile consists of a set of &lt;strong&gt;rules&lt;/strong&gt;, each with 3 parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;target&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;a list of &lt;strong&gt;prerequisites&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;a list of &lt;strong&gt;commands&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The general syntax for a rule is:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-make"&gt;target: prerequisite-1 prerequisite-2 ...
command1
command2
...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;target&lt;/strong&gt; and &lt;strong&gt;prerequisites&lt;/strong&gt; are separated by a colon (:). The &lt;strong&gt;commands&lt;/strong&gt; must be
preceded by a tab (NOT spaces).&lt;/p&gt;
&lt;div class="alert alert-warning"&gt;
&lt;div&gt;
If a command is not preceded by a tab, you get an error message like this:&lt;br&gt;
&lt;code&gt;makefile:4: *** missing separator. Stop.&lt;/code&gt;&lt;br&gt;
If you see this message it usually means you have spaces instead of tabs.
&lt;/div&gt;
&lt;/div&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;From the terminal, navigate to the folder that has the code you
wish to make a Makefile for. The sample shown below uses the &lt;strong&gt;MakeBasics&lt;/strong&gt; project from
the CS260 code (should be &lt;strong&gt;cs260Code/Week01/MakeBasics&lt;/strong&gt;/).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using your text editor, make a file called &lt;code&gt;Makefile&lt;/code&gt; in that directory.
Make sure it does not have an extension (no .txt at the end).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To edit the Makefile, you must use a text editor
editor that will NOT put spaces in when you type a tab. By default,
many programming tools will convert a press of the tab key into spaces.
VSCode should correctly handle tabs (unless you have changed the settings).
To see if you have tabs or spaces in VSCode, use &lt;strong&gt;View &amp;gt; Command Pallette&lt;/strong&gt;
and then &lt;strong&gt;View: Toggle Render Whitespace&lt;/strong&gt;. Spaces will show as individual &lt;code&gt;.&lt;/code&gt;s while
tabs show as &lt;code&gt;→&lt;/code&gt; followed by blank space.&lt;/p&gt;
&lt;p&gt;Alternatively, you can use a simple text editor:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Windows: &lt;strong&gt;Notepad&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Mac: &lt;strong&gt;Textedit&lt;/strong&gt; (see these instructions for switching to
&lt;a href="https://www.tekrevue.com/tip/textedit-plain-text-mode/" target="_blank" rel="noopener"&gt;plain text mode&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the Makefile put the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-make"&gt;.PHONY: program.exe
program.exe: main.cpp Answer.cpp Answer.h
g++ -g -Wall -o program.exe main.cpp Answer.cpp
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;.PHONY: program.exe&lt;/code&gt; is a way to tell make that we always want &lt;strong&gt;make&lt;/strong&gt; to
run the commands when this target is invoked. Normally, make would try to figure out
if &lt;code&gt;program.exe&lt;/code&gt; actually needs to be rebuilt by comparing its date to that of its prerequisites.
Normally, you might not declare &lt;code&gt;program.exe&lt;/code&gt; as PHONY to prevent unnecessary rebuilds -
especially if working on a large project where builds could take many minutes.
However, always rebuilding is a nice way to make sure that your code ALWAYS gets
rebuilt when you run make (which is essential if you are doing something like
switching back and forth from Windows to Linux while developing.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The next line - the first required one - defines the &lt;strong&gt;target&lt;/strong&gt; &lt;code&gt;program.exe&lt;/code&gt;.
A target that builds a file is typically given a name that matches the file it builds.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;After the &lt;code&gt;:&lt;/code&gt; on that line, it lists three prerequisite files.
These files must exist to run the rule. (If they are missing, &lt;strong&gt;make&lt;/strong&gt; will try to
build them using rules that match their names or, if those are not available, fail the build).
If the rule is not declared &lt;code&gt;.PHONY&lt;/code&gt;, the prerequisites also will be used to determine
if the target is up to date and thus the build can be skipped.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The next line is the &lt;strong&gt;command&lt;/strong&gt; to run. This command builds the file &lt;code&gt;program.exe&lt;/code&gt; using &lt;em&gt;g++&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-g&lt;/code&gt; says to include debugging info. This helps if you use Valgrind on this project&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-Wall&lt;/code&gt; says to mention ‘all’ warnings&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-o program.exe&lt;/code&gt; gives the output file the correct name&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main.cpp Answer.cpp&lt;/code&gt; are the list of files that need to be compiled&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that you should &lt;em&gt;not&lt;/em&gt; list the &lt;code&gt;.h&lt;/code&gt; files in the list of files to be compiled. They are not
compiled, they are included into the &lt;code&gt;.cpp&lt;/code&gt; files as appropriate. Any &lt;code&gt;.h&lt;/code&gt; files you might edit
&lt;em&gt;should&lt;/em&gt; be listed in the prerequisites, but only there.&lt;/p&gt;
&lt;p&gt;Save the Makefile.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Continuing in the terminal, type:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;make
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The default behavior for &lt;strong&gt;make&lt;/strong&gt;
is to run the first target in the file. In this, case that is the target &lt;code&gt;program.exe&lt;/code&gt;.
You can also run a particular rule by calling it explicitly.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;make program.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now type:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;./program.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To run the program &lt;code&gt;program.exe&lt;/code&gt; which is in the current directory
(&lt;code&gt;.&lt;/code&gt;). In general, this is how you will want to run your programs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Usually, people add some additional rules to every makefile with
some common behavior. These are called ‘phony’ rules, because they
are not named after a file they produce but just given convenient names.
You always should use &lt;code&gt;.PHONY&lt;/code&gt; for these rules.&lt;/p&gt;
&lt;p&gt;Add a rule to clean up, leaving the Makefile&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-make"&gt;.PHONY: program.exe
program.exe: main.cpp Answer.cpp Answer.h
g++ -g -Wall -o program.exe main.cpp Answer.cpp
.PHONY: clean
clean:
rm -f program.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;all&lt;/code&gt; target should come first, so that running make with no arguments
builds everything by default. The all target simply depends on all of the
files you want to make sure are always up-to-date, and does not need a recipe.
The &lt;code&gt;clean&lt;/code&gt; target doesn't have any prerequisites, so it always runs, and it
deletes files created by the build process. It uses &lt;code&gt;rm&lt;/code&gt; to remove files, and
the &lt;code&gt;-f&lt;/code&gt; option means that it won't ask for confirmation or complain if the
file(s) already don't exist.&lt;/p&gt;
&lt;p&gt;Note that &lt;code&gt;rm&lt;/code&gt; is a Linux/MacOS command. The clean rule will not work if run
in Windows. (To work in Windows it would have to execute the command &lt;code&gt;del program.exe&lt;/code&gt;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now if you run &lt;code&gt;make clean&lt;/code&gt;, you should end up with only your source files and
not the compiled program.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can rebuild the program with &lt;code&gt;make&lt;/code&gt; or &lt;code&gt;make all&lt;/code&gt;. If you
try to rebuild it after the program has already been built and there
are no changes to the source files, make will detect that nothing
needs to be done.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Setup</title><link>https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/setup/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://computerscience.chemeketa.edu/guides/command-line-development/makefiles/setup/</guid><description>&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;You will need some code to test out these instructions with. If you grab the CS260 examples code,
it has some projects in Week01 you can use to do so with. From a command prompt you can do this
to grab a copy of the CS260 code:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;git clone https://github.com/ChemeketaCS/cs260Code.git
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you are using Linux or Mac, you should be good to go. If you are using Windows you have two options:&lt;/p&gt;
&lt;p&gt;A. Run everything inside of Windows Subsystem for Linux. This is preferable, especially if you
also will be using Valgrind in WSL.&lt;/p&gt;
&lt;p&gt;B. Use Windows PowerShell or Command Prompt, but type &lt;code&gt;mingw32-make&lt;/code&gt; instead of &lt;code&gt;make&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item></channel></rss>