Makefile now puts object files and elf into build subdirectory

This commit is contained in:
Imbus 2024-03-23 21:27:48 +01:00
parent d5001183d6
commit 2ed0c0a4ac

View file

@ -14,27 +14,30 @@ CFLAGS = -std=c2x -Wall -Wno-array-bounds -mmcu=$(MCU) -DF_CPU=16000000UL -O3
# Source files
SRCS = $(wildcard *.c)
# Object files directory
OBJDIR = build
# Object files
OBJS = $(SRCS:.c=.o)
OBJS = $(addprefix $(OBJDIR)/,$(SRCS:.c=.o))
# Target file
TARGET = main
# Default target
all: $(TARGET).hex
all: $(OBJDIR) $(TARGET).hex
# Compile C files
%.o: %.c
$(OBJDIR)/%.o: %.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
# Link object files
$(TARGET).elf: $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(TARGET).elf
avr-strip $(TARGET).elf
$(OBJDIR)/$(TARGET).elf: $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(OBJDIR)/$(TARGET).elf
avr-strip $(OBJDIR)/$(TARGET).elf
# Convert ELF to HEX
$(TARGET).hex: $(TARGET).elf
avr-objcopy -O ihex -R .eeprom $(TARGET).elf $(TARGET).hex
$(TARGET).hex: $(OBJDIR)/$(TARGET).elf
avr-objcopy -O ihex -R .eeprom $(OBJDIR)/$(TARGET).elf $(TARGET).hex
# Flash the program
flash: $(TARGET).hex
@ -42,18 +45,22 @@ flash: $(TARGET).hex
# Run the program in QEMU
qemu: $(TARGET).elf
qemu-system-avr -machine $(QEMU_MACHINE_NAME) -bios $(TARGET).elf
qemu-system-avr -machine $(QEMU_MACHINE_NAME) -bios $(OBJDIR)/$(TARGET).elf
# View the generated assembly
asm: $(TARGET).hex
avr-objdump -S $(TARGET).elf
avr-objdump -S $(OBJDIR)/$(TARGET).elf
serial:
picocom -b 9600 /dev/ttyUSB0
# Create build directory
$(OBJDIR):
mkdir -p $(OBJDIR)
# Clean
clean:
rm -f $(OBJS) $(TARGET).elf $(TARGET).hex
rm -rf $(OBJDIR) $(TARGET).hex
size: main.hex
avr-size --mcu=atmega328p main.hex
size: $(TARGET).hex
avr-size --mcu=atmega328p $(TARGET).hex