diff --git a/Makefile b/Makefile index 766f77c..48e6595 100644 --- a/Makefile +++ b/Makefile @@ -3,35 +3,40 @@ CROSS_COMPILE ?= TARGET_FILE ?= test CC = $(CROSS_COMPILE)gcc +CPP = $(CROSS_COMPILE)cpp SRC_DIR = src OBJ_DIR = obj BIN_DIR = bin -C_FLAGS := -O0 -g -Iinc +INCLUDES := inc + +C_FLAGS := -O0 -g +CPP_FLAGS := $(addprefix -I, $(INCLUDES)) C_SRCS = $(wildcard $(SRC_DIR)/*.c) -C_OBJS = $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(C_SRCS))) +C_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.c,%.o,$(C_SRCS))) OBJS = $(C_OBJS) TARGET = $(BIN_DIR)/$(TARGET_FILE) +THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST)) .PHONY: all all: $(TARGET) -$(TARGET): $(OBJS) +$(TARGET): $(OBJS) $(THIS_MAKEFILE) @mkdir -p $(BIN_DIR) $(CC) $(LD_FLAGS) $(OBJS) $(LIBS) -o $@ .PRECIOUS: $(OBJ_DIR)/%.d -$(OBJ_DIR)/%.d: +$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c @mkdir -p $(OBJ_DIR) - $(CC) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(C_FLAGS) $(filter %$(patsubst %.d,%.c,$(notdir $@)),$(C_SRCS)) + $(CPP) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(CPP_FLAGS) $(patsubst $(OBJ_DIR)/%.d,$(SRC_DIR)/%.c,$@) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d @mkdir -p $(OBJ_DIR) - $(CC) -c $(C_FLAGS) $< -o $@ + $(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@ .PHONY: clean clean: