Q := @
CC = gcc
PRJ_DIR=$(shell pwd)
OUT_DIR := _build
OUT_FILE_NAME:= $(shell basename $$PWD)
SRCS = \
main.c\
comps/comp.c\
# inc folders common to all targets
INC_FOLDERS = \
-I $(PRJ_DIR)/mqtt\
-I $(PRJ_DIR)/comps\
# Libraries common to all targets
LIB_FILES += \
# Optimization flags
OPT := $(CFLAGS) -Wall -Werror
OPT += -Os
# C flags common to all targets
CFLAGS += $(OPT)
# C++ flags common to all targets
CXXFLAGS += $(OPT)
# Assembler flags common to all targets
ASMFLAGS += -g3
# Linker flags
LDFLAGS += $(OPT)
# Add standard libraries at the very end of the linker input, after all objects
# that may need symbols provided by these libraries.
LIB_FILES += -lstdc++
all: prepare $(OUT_FILE_NAME)
prepare:
$(Q)mkdir -p $(OUT_DIR)
OBJS = $(patsubst %.c, $(OUT_DIR)/%.o, $(SRCS))
$(OUT_FILE_NAME):$(OBJS)
$(Q)echo "+ Linking $(OUT_DIR)/$(notdir $@) ..."
$(Q)mkdir -p $(dir $@)
$(Q)$(CC) -o $@ $^ $(LIB_FILES) $(LDFLAGS)
$(Q)mv $@ $(OUT_DIR)
$(OUT_DIR)/%.o : %.c
$(Q)echo ": Compiling $< ..."
$(Q)mkdir -p $(OUT_DIR)/$(dir $<)
$(Q)$(CC) -c $(INC_FOLDERS) $(CFLAGS) $< -o $@
.PHONY : clean
clean :
rm -rf $(OUT_DIR)