Skip to content

Instantly share code, notes, and snippets.

@nibral
Created May 2, 2016 15:19
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 nibral/cf1b4a4002d0dab7ea7256f54abfebb4 to your computer and use it in GitHub Desktop.
Save nibral/cf1b4a4002d0dab7ea7256f54abfebb4 to your computer and use it in GitHub Desktop.
# Make directory tree as below
#--------------------
#.
#├─ Makefile
#└─ src
# └─ main.c
#--------------------
# Project settings
PROG = hello
#PROG = $(shell basename `readlink -f .`)
MCU = atmega328p
# Compiler settings
COMPILER = avr-gcc
OBJCOPY = avr-objcopy
CFLAGS = -g -O2 -mmcu=$(MCU)
LDFLAGS =
LIBS =
INCLUDE = -I./include
# filename and directories
HEX = $(PROG).hex
SRCDIR = ./src
SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJDIR = ./obj
OBJECTS = $(addprefix $(OBJDIR)/, $(notdir $(SOURCES:.c=.o)))
DEPENDS = $(OBJECTS:.o=.d)
# Generate ihex format file
$(HEX): $(PROG)
$(OBJCOPY) -j .text -j .data -O ihex $< $@
# Link objects
$(PROG): $(OBJECTS) $(LIBS)
$(COMPILER) $(LDFLAGS) $^ -o $@
# Compile SOURCES
$(OBJDIR)/%.o: $(SRCDIR)/%.c
-mkdir -p $(OBJDIR)
$(COMPILER) $(CFLAGS) $(INCLUDE) -c $< -o $@
all: clean $(HEX)
.PHONY: clean
clean:
-$(RM) $(OBJECTS) $(DEPENDS) $(PROG) $(HEX)
-include $(DEPENDS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment