Update Makefile to produce dependencies & objects

This commit is contained in:
2022-04-05 17:56:11 -05:00
parent 478778841e
commit ef916835e8
3 changed files with 40 additions and 98 deletions

View File

@@ -1,19 +1,48 @@
# Executable
TARGET := main.out
# Paths to source, dependency, object files
SPATH = src
IPATH = inc
DPATH = dep
OPATH = obj
VPATH = $(SPATH) $(OPATH) $(IPATH) $(DPATH)
# compiler and compiler flags
CC = g++
CFLAGS = -Iinc -pthread -lrt
VPATH = src
CFLAGS = -MMD -I$(IPATH) -pthread -lrt
OBJECTS := %.o list.o producer.o transformer.o consumer.o
EXECUTE := main.out #test.out
# list of object files
SOURCES = $(wildcard $(SPATH)/*.cpp)
OBJECTS = $(addprefix $(OPATH)/,$(notdir $(SOURCES:.cpp=.o)))
.PHONY: all clean
.PHONY: all clean dump
all: $(EXECUTE)
all: $(DPATH) $(OPATH) $(TARGET)
dump:
@echo SOURCES: $(SOURCES)
@echo OBJECTS: $(OBJECTS)
@echo TARGET: $(TARGET)
@echo VPATH: $(VPATH)
clean:
rm $(EXECUTE)
-rm $(TARGET)
-rm -r dep obj
$(DPATH) $(OPATH):
mkdir -p $@
# Make the executable(s)
%.out: $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^
%.o: %.cpp
$(CC) $(CFLAGS) -c $<
# Make the object and dependency files
$(OPATH)/%.o $(DPATH)/%.d: $(SPATH)/%.cpp
$(CC) $(CFLAGS) -MF $(DPATH)/$(@F:.o=.d) -o $@ -c $<
# Don't delete object files:
.PRECIOUS: $(OPATH)/%.o $(DPATH)/%.d
# use dependencies when rebuilding
-include $(addprefix $(DPATH)/,$(wildcard *.d))