# # Example Makefile (C) Giovanni Organtini 2007 - G.Organtini@roma1.infn.it # # Lines starting with # are comments # # The following line defines new suffixes (some suffixes are already defined) # You can check existing rules using make -p (note that newly defined rules # will be appended to the output). This line is needed to inform make that we # are going to define implicit rules for files ending in .e and .d .SUFFIXES: .e .d # The following line declares all, clean and mrproper to be phony rules, i.e. # rules whose target is not the name of a file. If missing rules works fine, # but if a file exists with the name of a rule, then it stops working .PHONY: all clean mrproper # Variables definition. Variable content can be updated using += operator CC=c++ CFLAGS=-O1 CFLAGS+=-I. # This rule describes how to generate .d files from .cpp files. .d files # contains dependencies. The -MM flag on c++ command generates dependencies # (ignoring system wide files). The source file is processed and the output # passed to sed to generate a line of type: # file.o file.d : # so make knows that file.o depends on the content of file.d and that file.d # must be regenerated if any of the dependencies changes. .cpp.d: $(SHELL) -ec '$(CC) -MM $(CFLAGS) $< | sed -e "s/$*.o/& $@/g" > $@' # This rule describe how to pass from .o to .e (executable) files .o.e: $(CC) -o $@ $< # Implicit rule for compilation (overrides existing rule) .cpp.o: $(CC) -c $(CFLAGS) $< # Define the variable sources using wildcard expansion. Note that it is not # the same of *.cpp. The latter works, but if *.cpp files are missing an error # is generated. sources = $(wildcard *.cpp) # Phony rule to generate all. Since this is the first rule it is executed # automatically. It asks to generate all .e files from .cpp files. all: $(patsubst %.cpp, %.e, $(sources)) # include dependencies list. If .d files are missing they are generated # using rule above. include $(sources:.cpp=.d) # clean phony rule clean: rm -f *.o rm -f *.e # more cleaning mrproper: clean rm -f *.d rm -f *~