I'm still learning makefile and I created a generic makefile to build a simple AVR project. I use Proteus to simulate the application, but I'm having an issue debugging the application when I change the executable's directory. Debugging works fine with this makefile:
# ===============================================
# Toolchain
# ===============================================
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRSIZE = avr-size
# ===============================================
# Directories
# ===============================================
BUILDDIR = build
OBJDIR = $(BUILDDIR)/obj
BINDIR = $(BUILDDIR)/bin
# ===============================================
# Files
# ===============================================
TARGET = atmega32
SOURCES = $(shell find src/ -name "*.c")
OBJS = $(SOURCES:%.c=$(OBJDIR)/%.o)
HEADERS = $(shell find . -name "*.h")
# ===============================================
# Flags
# ===============================================
DEVICE = atmega32
F_CPU = 1000000
BAUD = 9600
CFLAGS = -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)UL -DBAUD=$(BAUD) -Os
CFLAGS += -Wall -Wextra -Werror -Wshadow -Wstrict-prototypes
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax
CFLAGS += -g -ggdb
CFLAGS += -std=gnu99 -Wl,-Map,"main.map"
# ===============================================
# Compiling & Linking
# ===============================================
all: $(TARGET).hex
%.hex: %.elf
$(OBJCOPY) -R .eeprom -O ihex $< $@
%.elf: $(SOURCES) $(HEADERS)
@$(CC) $(CFLAGS) $(SOURCES) --output $@
size : $(TARGET).elf
@$(AVRSIZE) -C --mcu=$(DEVICE) $^
# ===============================================
# Phonies
# ===============================================
.PHONY: all flash clean size
clean:
@rm -f *.elf *.hex *.map
but when I change the generated files directory to ex: build/bin debugging info are gone:
Note: changes are in Compiling & Linking section
# ===============================================
# Toolchain
# ===============================================
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRSIZE = avr-size
# ===============================================
# Directories
# ===============================================
BUILDDIR = build
OBJDIR = $(BUILDDIR)/obj
BINDIR = $(BUILDDIR)/bin
# ===============================================
# Files
# ===============================================
TARGET = atmega32
SOURCES = $(shell find src/ -name "*.c")
OBJS = $(SOURCES:%.c=$(OBJDIR)/%.o)
HEADERS = $(shell find . -name "*.h")
# ===============================================
# Flags
# ===============================================
DEVICE = atmega32
F_CPU = 1000000
BAUD = 9600
CFLAGS = -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)UL -DBAUD=$(BAUD) -Os
CFLAGS += -Wall -Wextra -Werror -Wshadow -Wstrict-prototypes
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax
CFLAGS += -g -ggdb
CFLAGS += -std=gnu99 -Wl,-Map,"$(BUILDDIR)/main.map"
# ===============================================
# Compiling & Linking
# ===============================================
all: $(BUILDDIR)/$(TARGET).hex
%.hex: %.elf
$(OBJCOPY) -R .eeprom -O ihex $< $@
%.elf: $(SOURCES) $(HEADERS)
@$(CC) $(CFLAGS) $(SOURCES) --output $@
size : $(BUILDDIR)/$(TARGET).elf
@$(AVRSIZE) -C --mcu=$(DEVICE) $^
# ===============================================
# Phonies
# ===============================================
.PHONY: all flash clean size
clean:
@rm -f $(BUILDDIR)/*.elf $(BUILDDIR)/*.hex $(BUILDDIR)/*.map
which is weird to me. Actually, I don't know what happens here. I already included all the flags required for debugging information -g -ggdb.

