<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="default.xsl"?>
<fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="true"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>345</fr:anchor><fr:addr
type="user">ocs-000D</fr:addr><fr:route>ocs-000D.xml</fr:route><fr:title
text="Makefiles › Makefile syntax"><fr:link
type="local"
href="ocs-000B.xml"
addr="ocs-000B"
title="Makefiles">Makefiles</fr:link> › Makefile syntax</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>It is common practice to copy and modify old makefules rather than generating new ones from scratch.</fr:p><fr:pre><![CDATA[#
# A simple makefile for managing build of project composed of C source files.
#


# It is likely that default C compiler is already gcc, but explicitly
# set, just to be sure
CC = gcc

# The CFLAGS variable sets compile flags for gcc:
#  -g        compile with debug information
#  -Wall     give verbose compiler warnings
#  -O0       do not optimize generated code
#  -std=c99  use the C99 standard language definition
CFLAGS = -g -Wall -O0 -std=c99

# The LDFLAGS variable sets flags for linker
#  -lm   says to link in libm (the math library)
LDFLAGS = -lm

# In this section, you list the files that are part of the project.
# If you add/change names of source files, here is where you
# edit the Makefile.
SOURCES = demo.c vector.c map.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = demo


# The first target defined in the makefile is the one
# used when make is invoked with no argument. Given the definitions
# above, this Makefile file will build the one named TARGET and
# assume that it depends on all the named OBJECTS files.

$(TARGET) : $(OBJECTS)
    $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

# Phony means not a "real" target, it doesn't build anything
# The phony target "clean" is used to remove all compiled object files.

.PHONY: clean

clean:
    @rm -f $(TARGET) $(OBJECTS) core]]></fr:pre></fr:mainmatter><fr:backmatter><fr:tree
toc="false"
numbered="false"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:title
text="Context">Context</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:tree
toc="true"
numbered="false"
show-heading="true"
show-metadata="true"
expanded="false"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>346</fr:anchor><fr:addr
type="user">ocs-000B</fr:addr><fr:route>ocs-000B.xml</fr:route><fr:title
text="Makefiles">Makefiles</fr:title><fr:taxon>Note</fr:taxon><fr:date><fr:year>2024</fr:year><fr:month>9</fr:month><fr:day>13</fr:day></fr:date><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>A well written makefile describes all the files and settings used to compile a project and link it with the appropriate libraries</fr:p><fr:p>Managing a build with a makefile is less error-prone and far simpler after a _one-time setup cost_. A makefile with a proper dependency graph will only rebuild a target when one or more of the components it depends on has changed. This allows seperating the components of a project whcih can be built independently.</fr:p><fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>257</fr:anchor><fr:addr
type="user">ocs-000D</fr:addr><fr:route>ocs-000D.xml</fr:route><fr:title
text="Makefile syntax">Makefile syntax</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>It is common practice to copy and modify old makefules rather than generating new ones from scratch.</fr:p><fr:pre><![CDATA[#
# A simple makefile for managing build of project composed of C source files.
#


# It is likely that default C compiler is already gcc, but explicitly
# set, just to be sure
CC = gcc

# The CFLAGS variable sets compile flags for gcc:
#  -g        compile with debug information
#  -Wall     give verbose compiler warnings
#  -O0       do not optimize generated code
#  -std=c99  use the C99 standard language definition
CFLAGS = -g -Wall -O0 -std=c99

# The LDFLAGS variable sets flags for linker
#  -lm   says to link in libm (the math library)
LDFLAGS = -lm

# In this section, you list the files that are part of the project.
# If you add/change names of source files, here is where you
# edit the Makefile.
SOURCES = demo.c vector.c map.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = demo


# The first target defined in the makefile is the one
# used when make is invoked with no argument. Given the definitions
# above, this Makefile file will build the one named TARGET and
# assume that it depends on all the named OBJECTS files.

$(TARGET) : $(OBJECTS)
    $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

# Phony means not a "real" target, it doesn't build anything
# The phony target "clean" is used to remove all compiled object files.

.PHONY: clean

clean:
    @rm -f $(TARGET) $(OBJECTS) core]]></fr:pre></fr:mainmatter><fr:backmatter /></fr:tree><fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>258</fr:anchor><fr:addr
type="user">ocs-000E</fr:addr><fr:route>ocs-000E.xml</fr:route><fr:title
text="Makefile macros">Makefile macros</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>Subsitutions defined toward top of the file, e.g. (<fr:code>CFLAGS = -g -Wall</fr:code>), and are similar to <fr:code>#define</fr:code> in <fr:link
type="external"
href="%7B%7B%3C%20relref%20%2220240401132118-c.md%22%20%3E%7D%7D">C</fr:link> . It can be referenced later with <fr:code>$(CFLAGS)</fr:code>.</fr:p><fr:p>Example macros: - <fr:code>OBJECTS = $(SOURCES:.c=o)</fr:code> - Defines the <fr:code>OBJECTS</fr:code> macro to be the same as the <fr:code>SOURCES</fr:code> macro except that every instance of <fr:code>.c</fr:code> is replaced with <fr:code>.o</fr:code>. - <fr:code>$@</fr:code>: - built-in, name of the current target - <fr:code>$^</fr:code>: - built-in, current targets list of dependencies</fr:p></fr:mainmatter><fr:backmatter /></fr:tree><fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>259</fr:anchor><fr:addr
type="user">ocs-000F</fr:addr><fr:route>ocs-000F.xml</fr:route><fr:title
text="Targets">Targets</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>Written in the following form</fr:p><fr:pre><![CDATA[target-name : dependencies
    action]]></fr:pre><fr:p>where the target name is the name of the file that will be produced when this target is built. The first target listed in a make file is the default target, meaning it is invoked by <fr:code>make</fr:code> with no args.</fr:p></fr:mainmatter><fr:backmatter /></fr:tree><fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>260</fr:anchor><fr:addr
type="user">ocs-000C</fr:addr><fr:route>ocs-000C.xml</fr:route><fr:title
text="Phony targets">Phony targets</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>The clean target in the example does not fit the pattern which is     previously described of targets. It does not create a file named     <fr:code>clean</fr:code>, rather it is a shortcut for running a command whcih cclears out     the projects build files. These commands are listed as dependencies of     <fr:code>.PHONY</fr:code> which allows Make to run the target without any dependency     checks.</fr:p></fr:mainmatter><fr:backmatter /></fr:tree><fr:p>links</fr:p><fr:ul><fr:li><fr:link
type="external"
href="https://web.stanford.edu/class/archive/cs/cs107/cs107.1174/guide_make.html">cs107 makefile guide</fr:link></fr:li>
<fr:li><fr:link
type="external"
href="https://gist.github.com/isaacs/62a2d1825d04437c6f08">Makefile basics - gist</fr:link></fr:li></fr:ul></fr:mainmatter><fr:backmatter /></fr:tree></fr:mainmatter><fr:backmatter /></fr:tree></fr:backmatter></fr:tree>