# File should be named Makefile and then can be run as `make <target>`.
# Otherwise we use `make -f "filename" <target>`.
file0.txt:
echo "foo" > file0.txt
# Even comments in these 'recipe' sections get passed to the shell.
# Try `make file0.txt` or simply `make` - first rule is the default.
file1.txt: file0.txt
cat file0.txt > file1.txt
# use the same quoting rules as in the shell.
@cat file0.txt >> file1.txt
# @ stops the command from being echoed to stdout.
-@echo 'hello'
# - means that make will keep going in the case of an error.
# Try `make file1.txt` on the commandline.file2.txt file3.txt: file0.txt file1.txt
touch file2.txt
touch file3.txt
all: maker process
maker:
touch ex0.txt ex1.txt
.PHONY: all maker process
ex0.txt ex1.txt: maker
@echo $^ # $^ is a variable containing the list of prerequisites
@echo $@ # prints the target name
#(for multiple target rules, $@ is whichever caused the rule to run)
@echo $< # the first prerequisite listed
@echo $? # only the dependencies that are out of date
@echo $+ # all dependencies including duplicates (unlike normal)
#@echo $| # all of the 'order only' prerequisites
process: ex1.txt file0.txt
%.png: %.svg
inkscape --export-png $^
small/%.png: %.svg inkscape --export-png --export-dpi 30 $^
@echo this rule is chosen
%.png: %.ps
@echo this rule is not chosen if *.svg and *.ps are both present
.png.ps:
@echo this rule is similar to a pattern rule.
.SUFFIXES: .png
name = Ted
name2="Sarah"
echo:
@echo $(name)
@echo ${name2}
@echo $name # This won't work, treated as $(n)ame.
@echo $(name3) # Unknown variables are treated as empty strings.
name4 ?= Jean
override name5 = David
name4 +=grey
echo: name2 = Sara # True within the matching rule # and also within its remade recursive dependencies # (except it can break when your graph gets too complicated!)
echo_inbuilt:
echo $(CC)
echo ${CXX}
echo $(FC)
echo ${CFLAGS}
echo $(CPPFLAGS)
echo ${CXXFLAGS}
echo $(LDFLAGS)
echo ${LDLIBS}
var := hello
var2 ::= $(var) hello
#:= and ::= are equivalent.
var3 ::= $(var4) and good luck
var4 ::= good night
sourcefiles = $(wildcard *.c */*.c)
objectfiles = $(patsubst %.c,%.o,$(sourcefiles))
ls: * src/*
@echo $(filter %.txt, $^)
@echo $(notdir $^)
@echo $(join $(dir $^),$(notdir $^))
include foo.mk
sport = tennis
report:
ifeq ($(sport),tennis)
@echo 'game, set, match'
else
@echo "They think it's all over; it is now"
endif
foo = true
ifdef $(foo)
bar = 'hello'
endif