plesiv
12/22/2014 - 8:11 PM

Minimal Makefile - compiles all files in specified directories

Minimal Makefile - compiles all files in specified directories

# Configuration
BIN_NAME   := app_bin.elf
SRC_PATH   := src
INC_PATH   := include
LIB_PATH   := lib
BUILD_PATH := build

CC 	 := gcc
CFLAGS 	 := -Wall -O0 -g3
LDFLAGS  := 
LIBS  	 := m

# --------------------------------------------------------------

.PHONY: all clean

# Inferred variables
SRC_FILES =  $(foreach srcdir,$(SRC_PATH),$(wildcard $(srcdir)/*.c)) 
OBJ_FILES =  $(addprefix $(BUILD_PATH)/,$(notdir $(SRC_FILES:.c=.o)))
CFLAGS__  += $(foreach includedir,$(INC_PATH),-I$(includedir))
CFLAGS__  += $(CFLAGS)
LDFLAGS__ += $(foreach librarydir,$(LIB_PATH),-L$(librarydir))
LDFLAGS__ += $(foreach library,$(LIBS),-l$(library))
LDFLAGS__ += $(LDFLAGS)

# Targets

#-default
all: $(BUILD_PATH)/$(BIN_NAME)

#-build .elf binary
$(BUILD_PATH)/$(BIN_NAME): $(OBJ_FILES)
	$(CC) -o $@ $^ $(LDFLAGS__)

#-build .o files
$(BUILD_PATH)/%.o: $(SRC_PATH)/%.c
	$(CC) -c -o $@ $< $(CFLAGS__)

#-clean
clean:
	rm -f $(OBJ_FILES)
	rm -f $(BUILD_PATH)/$(BIN_NAME)