makefile - Could someone explain this make file? -
i found makefile on site. don't explain example, wondering if new going on.
cc=g++ cflags=-c -wall ldflags= sources=main.cpp hello.cpp factorial.cpp objects=$(sources:.cpp=.o) executable=hello all: $(sources) $(executable) $(executable): $(objects) $(cc) $(ldflags) $(objects) -o $@ .cpp.o: $(cc) $(cflags) $< -o $@
cc=g++ cflags=-c -wall ldflags= sources=main.cpp hello.cpp factorial.cpp
sets 4 variables constant strings. rest of makefile, wherever $(cc)
appears (for example), replaced g++
objects=$(sources:.cpp=.o)
sets variable objects same sources, except wherever pattern .cpp
appears in words of sources, replaced .o
executable=hello
sets constant string var
all: $(sources) $(executable)
the first actual rule in makefile, tells make build all
must first build in $(sources)
, $(executable)
, , nothing. since first, becomes default target, running make
equivalent make all
$(executable): $(objects) $(cc) $(ldflags) $(objects) -o $@
another rule: create $(executable)
(which expands hello
) must first build in $(objects)
(equivalent main.o hello.o factorial.o
) , run command $(cc) $(ldflags) $(objects) -o $@
.cpp.o: $(cc) $(cflags) -o $@ $<
a pattern rule: in order build file ending in .o
, first rebuild/create/find corresponding file ending in .cpp, , run command $(cc) $(cflags) -o $@ $<
.
these last 2 rules contain special variables $@
, $<
valid in rule actions , expand target , first dependency respectively
so when run make
, reads , tries build default target (all). since doesn't exist, tries build files main.cpp, hello.cpp, factorial.cpp, , hello. since first 3 (presumably) exist, looks rules/dependencies them, doesn't find any, decides there's nothing them. if didn't exist, make give error saying "no rule make target 'main.cpp'"
in case of "hello" depends on main.o, hello.o , factorial.o, looks them. main.o, pattern rule says depends on main.cpp, if main.o doesn't exist or if main.cpp newer, run command g++ -c -wall -o main.o main.cpp
. same happens hello.o , factorial.o.
once done, if hello
doesn't exist or older of .o files (which may have changed, possibly pretty new), run command relink it. finally, run empty command (doing nothing) 'rebuild' all.
Comments
Post a Comment