# An "omnipotent" makefile for g++ that builds the # dependecy tree "automagically". # I assume that you have a subfolder ./src that contains # all your .cpp and .hpp files, and that you made a subfolder # ./obj that will contain the .o files. # Hint: the compiler option -MMD does a lot of magic. # ----------------------------------------------------------- # the compiler, in my case the C++ compiler CC = g++ # flags for compiling .cpp files CC_FLAGS = -O3 -Wall -c # flags for linking the .o files LD_FLAGS = -O3 -Wall # all my .cpp files are saved in a folder ./src CPP_FILES := $(wildcard src/*.cpp) # and I want my .o files to be stored in a ./obj folder OBJ_FILES := $(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o))) # the target "all" is my main executable called # "hawaiitoaster" (make, make all) all: hawaiitoaster # a rule for cleaning up (make clean) clean: rm -f obj/*.o obj/*.d hawaiitoaster # the rule for the main executable hawaiitoaster: $(OBJ_FILES) $(CC) $(LD_FLAGS) $^ -o $@ # a pattern for compiling .cpp files # (and hence, making the .o files) obj/%.o: src/%.cpp $^ $(CC) $(CC_FLAGS) $< -o $@ # now include dependencies. "gcc -c -MMD src/foo.cpp -o obj/foo.o" # will make a file foo.d (and foo.o). The foo.d file contains the # line "obj/foo.o: src/foo.cpp src/foo.hpp src/bar.hpp", depending # on whatever has been "#include"d in the header "foo.hpp". CC_FLAGS += -MMD -include $(OBJ_FILES:.o=.d)