Makefile: simplification and improvment

Default target is the main program.
Use implicit rule for main program.
Generate .d file at compilation time instead of using specific rules
Fix CFLAGS CPPFLAGS usage
This commit is contained in:
Mathieu Maret 2017-06-12 14:29:01 +02:00
parent 5f29edd6cc
commit b0541348aa

View File

@ -1,19 +1,23 @@
CPPFLAGS ?= -Werror -Wall # Preproc options
# -MMD is used to generate .d files for header dependencies
CPPFLAGS = -MMD
# main compilation
CFLAGS ?= -Werror -Wall
#Linker flags
LDFLAGS =
#Linker path
LDLIBS =
program = test program = test
sources = $(wildcard *.c) sources = $(wildcard *.c)
objects = $(sources:%.c=%.o) objects = $(sources:%.c=%.o)
depends = $(sources:%.c=%.d) depends = $(sources:%.c=%.d)
%.d: %.c
@$(CPP) $(CPPFLAGS) -c -MP -MM -MT "$@ $*.o" $< >$@
$(program): $(objects) $(program): $(objects)
$(CXX) -o $@ $^ $(LDFLAGS) $(LDLIBS)
.PHONY: clean .PHONY: clean
clean: clean:
rm -f $(program) $(objects) $(depends) $(RM) $(program) $(objects) $(depends)
ifneq ($(MAKECMDGOALS),clean) ifneq ($(MAKECMDGOALS),clean)
-include $(depends) -include $(depends)