Skip to content

Instantly share code, notes, and snippets.

@rynr
Last active February 11, 2024 19:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rynr/72734da4b8c7b962aa65 to your computer and use it in GitHub Desktop.
Save rynr/72734da4b8c7b962aa65 to your computer and use it in GitHub Desktop.
Makefile for avr-g++
# Makefile for AVR C++ projects
# From: https://gist.github.com/rynr/72734da4b8c7b962aa65
# ----- Update the settings of your project here -----
# Hardware
MCU = atmega32u4 # see `make show-mcu`
OSC = 16000000UL
PROJECT = example
# ----- These configurations are quite likely not to be changed -----
# Binaries
GCC = avr-gcc
G++ = avr-g++
RM = rm -f
AVRDUDE = avrdude
# Files
EXT_C = c
EXT_C++ = cc
EXT_ASM = asm
# ----- No changes should be necessary below this line -----
OBJECTS = \
$(patsubst %.$(EXT_C),%.o,$(wildcard *.$(EXT_C))) \
$(patsubst %.$(EXT_C++),%.o,$(wildcard *.$(EXT_C++))) \
$(patsubst %.$(EXT_ASM),%.o,$(wildcard *.$(EXT_ASM)))
# TODO explain these flags, make them configurable
CFLAGS = $(INC)
CFLAGS += -Os
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -Wall -Wstrict-prototypes
CFLAGS += -DF_OSC=$(OSC)
CFLAGS += -mmcu=$(MCU)
C++FLAGS = $(INC)
C++FLAGS += -Os
C++FLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
C++FLAGS += -Wall
C++FLAGS += -DF_OSC=$(OSC)
C++FLAGS += -mmcu=$(MCU)
ASMFLAGS = $(INC)
ASMFLAGS += -Os
ASMFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
ASMFLAGS += -Wall -Wstrict-prototypes
ASMFLAGS += -DF_OSC=$(OSC)
ASMFLAGS += -x assembler-with-cpp
ASMFLAGS += -mmcu=$(MCU)
default: $(PROJECT).elf
echo $(OBJECTS)
%.elf: $(OBJECTS)
$(GCC) $(CFLAGS) $(OBJECTS) --output $@ $(LDFLAGS)
%.o : %.$(EXT_C)
$(GCC) $< $(CFLAGS) -c -o $@
%.o : %.$(EXT_C++)
$(G++) $< $(C++FLAGS) -c -o $@
%.o : %.$(EXT_ASM)
$(G++) $< $(ASMFLAGS) -c -o $@
clean:
$(RM) $(PROJECT).elf $(OBJECTS)
help:
@echo "usage:"
@echo " make <target>"
@echo ""
@echo "targets:"
@echo " clean Remove any non-source files"
@echo " config Shows the current configuration"
@echo " help Shows this help"
@echo " show-mcu Show list of all possible MCUs"
config:
@echo "configuration:"
@echo ""
@echo "Binaries for:"
@echo " C compiler: $(GCC)"
@echo " C++ compiler: $(G++)"
@echo " Programmer: $(AVRDUDE)"
@echo " remove file: $(RM)"
@echo ""
@echo "Hardware settings:"
@echo " MCU: $(MCU)"
@echo " OSC: $(OSC)"
@echo ""
@echo "Defaults:"
@echo " C-files: *.$(EXT_C)"
@echo " C++-files: *.$(EXT_C++)"
@echo " ASM-files: *.$(EXT_ASM)"
show-mcu:
$(G++) --help=target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment