Skip to content

Instantly share code, notes, and snippets.

@plesiv
Created December 22, 2014 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plesiv/d6fe9214308a59d252ee to your computer and use it in GitHub Desktop.
Save plesiv/d6fe9214308a59d252ee to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment