# This is a "universal" makefile for compiling and linking C++ applications
# define a name for the application's name
target := log

# define build options - adjust the lists according to your needs:

# include directories for custom header files:
INCL_DIRS :=

# link options:
LDFLAGS :=

# link libraries - preceeded with 'l':
LDLIBS := 

#CXX
CXX := g++

# compile options:
CXXFLAGS := -g -Wall -fmessage-length=0 

# -O3
#-w //inhibit all warnings
#-time // dump building time

# Below follows the actual makefile section where commands are performed.
# There will be rarely need to change something there.

# construct a list of .cpp (source) files
sources := $(wildcard *.cpp)

# construct a list of the corresponding .o (object) files
objects := $(sources:.cpp=.o)

# name of the dependency file - no need to touch it
dependency := Makefile.dep

# main goal for 'make' is the first target, here - all
# all is always assumed to be a target, and not a file
# file disambiguity is achieved via the .PHONY directive
# usage: make all or simply make, since this is the first target
.PHONY : all clean
all : $(target)

# the automatic variable $^ expands to all prerequisites (objects)
# the automatic variable $@ expands to the targets name
# the actual action done is the linking of all .o files into the executable
# a preceeding @ symbol before a non-make command doesn't print the command
$(target) : $(objects)
	@echo linking...
	@$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
	@echo done.

# rule for creating an .o file out of a corresponding .cpp file
# the automatic variable $< expands to the first prerequisite (.cpp)
.cpp.o :
	@echo compiling $(CXX) $(CXXFLAGS) $(INCL_DIRS) $<... 
	@$(CXX) $(CXXFLAGS) $(INCL_DIRS) -c $< -o $@

# even if there exists a file clean, make clean will execute its commands
# and will not ever assume clean is an up-to-date file
# the clean target will remove all intermediate files -
# .o, .dep respectively
# usage: make clean
clean :
	@$(RM) $(objects)
	@$(RM) $(target)
	@$(RM) $(dependency)
	@echo target, dependencies and objects were removed

$(dependency):
	@echo generating dependencies...
	@$(CXX) -E -MM $(sources) -o $@

ifeq (,$(findstring clean,$(MAKECMDGOALS)))
-include $(dependency)
endif

