From 2ed0c0a4acad69717d08457f3f3341f796df03d4 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 23 Mar 2024 21:27:48 +0100 Subject: [PATCH] Makefile now puts object files and elf into build subdirectory --- makefile | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/makefile b/makefile index 3e28edb..efd39fb 100644 --- a/makefile +++ b/makefile @@ -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 \ No newline at end of file +size: $(TARGET).hex + avr-size --mcu=atmega328p $(TARGET).hex