assemblymakefileembeddedavravr-gcc

Atmega8 - Compile and link C and Assembly-files with one Makefile


I'm playing around with the Atmega8 and Assembly. So I came to the point where it would be nice to have a Makefile, which looks through the folder, grabs every *.S, *.c and *.cpp file, link and compile them to a flashable binary. I'm using the avr-toolchain and BootHID to flash the binary, but I think the last one doesn't matter.

That's what I got so far:

CROSS   ?=/home/nico/Dokumente/avr8-gnu-toolchain/bin/avr-
CC      :=$(CROSS)gcc
CXX     :=$(CROSS)g++
LD      :=$(CROSS)g++
SIZE    :=$(CROSS)size
OBJCOPY :=$(CROSS)objcopy
OBJDUMP :=$(CROSS)objdump

BOOTHID = /home/nico/Dokumente/bootloadHID/commandline/bootloadHID

RM=rm -f

TARGET=a
MMCU?=atmega8
AVRDUDE_FLAGS?=

SOURCES=$(wildcard *.cpp) $(wildcard *.c) $(wildcard *.S)
INCLUDES=

#SETTINGS=settings.h

OBJECTS = $(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(patsubst %.S,%.o,$(SOURCES))))

CSTD?=c99
COPT=-O2 -fdata-sections -ffunction-sections
CFLAGS=-mmcu=$(MMCU) -std=$(CSTD) $(COPT) -Wall
CFLAGS+=$(addprefix -I,$(INCLUDES))
CFLAGS+=-include

CXXSTD?=c++98
CXXOPT=$(COPT) -fno-exceptions -fno-rtti
CXXFLAGS=-mmcu=$(MMCU) -std=$(CXXSTD) $(CXXOPT) -Wall
CXXFLAGS+=$(addprefix -I,$(INCLUDES))
CXXFLAGS+=-include

LDFLAGS=-mmcu=$(MMCU) -Wl,--gc-sections -Wl,-Map=$(TARGET).map,--cref

.PHONY: all avrdude flash
all: $(TARGET).hex $(TARGET).lst

$(TARGET).elf: $(OBJECTS)
    $
    $(LD) $(LDFLAGS) $^ -lm -o $@

$(TARGET).hex: $(TARGET).elf
    $(OBJCOPY) -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@

$(TARGET).bin: $(TARGET).elf
    $(OBJCOPY) -O binary -R .eeprom -R .fuse -R .lock -R .signature $< $@

%.o: %.cpp
    $(CXX) -o $@ $(CXXFLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -c

%.o: %.c
    $(CC) -o $@ $(CFLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -c
    
%.o: %.S
    $(CC) -mmcu=$(MMCU) -c -o timing.o timing.S

$(TARGET).lst: $(TARGET).elf
    $(OBJDUMP) -h -S $< > $@

avrdude: $(TARGET).hex
    avrdude $(AVRDUDE_FLAGS) -e -m flash -i $<

flash: $(TARGET).hex
    $(BOOTHID) $<

clean:
    $(RM) $(OBJECTS) *.elf *.hex *.lst *.map *.d

If I don't have any *.S-files in the folder it works like a charm.

This line was hardcoded just for debugging.

 %.o: %.S
        $(CC) -mmcu=$(MMCU) -c -o timing.o timing.S

But as soon as I put my Assemblyfile in the dir together with the c-files I get this error:

$ make
/home/nico/Dokumente/avr8-gnu-toolchain/bin/avr-gcc -o lcd.o -mmcu=atmega8 -std=c99 -O2 -fdata-sections -ffunction-sections -Wall  -include -MMD -MP -MF lcd.d lcd.c -c
cc1: error: to generate dependencies you must specify either -M or -MM

It's a kind of group project I'm working on. When my group partner compiles the code through Visual Studio everything is fine, so I think the code should be fine. But I'm using Linux, so here I am...


Solution

  • Had to delete/comment out these two lines

    ...
    #CFLAGS+=-include
    ...
    #CXXFLAGS+=-include
    ...
    

    I forgot those because of way earlier editing.

    And I've edited the asm rule a bit:

    %.o: %.S
        $(CC) -mmcu=$(MMCU) -c -o $@ $<
    

    Here is the full and corrected Makefile if someone needs it one day:

    CROSS   ?=/home/nico/Dokumente/avr8-gnu-toolchain/bin/avr-
    CC      :=$(CROSS)gcc
    CXX     :=$(CROSS)g++
    LD      :=$(CROSS)g++
    SIZE    :=$(CROSS)size
    OBJCOPY :=$(CROSS)objcopy
    OBJDUMP :=$(CROSS)objdump
    
    BOOTHID = /home/nico/Dokumente/bootloadHID/commandline/bootloadHID
    
    RM=rm -f
    
    TARGET=a
    MMCU?=atmega8
    AVRDUDE_FLAGS?=
    
    SOURCES=$(wildcard *.cpp) $(wildcard *.c) $(wildcard *.S)
    INCLUDES=
    
    OBJECTS = $(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(patsubst %.S,%.o,$(SOURCES))))
    
    CSTD?=c99
    COPT=-O2 -fdata-sections -ffunction-sections
    CFLAGS=-mmcu=$(MMCU) -std=$(CSTD) $(COPT) -Wall
    CFLAGS+=$(addprefix -I,$(INCLUDES))
    
    CXXSTD?=c++98
    CXXOPT=$(COPT) -fno-exceptions -fno-rtti
    CXXFLAGS=-mmcu=$(MMCU) -std=$(CXXSTD) $(CXXOPT) -Wall
    CXXFLAGS+=$(addprefix -I,$(INCLUDES))
    
    LDFLAGS=-mmcu=$(MMCU) -Wl,--gc-sections -Wl,-Map=$(TARGET).map,--cref
    
    
    .PHONY: all avrdude flash
    all: $(TARGET).hex $(TARGET).lst
    
    $(TARGET).elf: $(OBJECTS)
        
        $(LD) $(LDFLAGS) $^ -lm -o $@
    
    $(TARGET).hex: $(TARGET).elf
        $(OBJCOPY) -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
    
    $(TARGET).bin: $(TARGET).elf
        $(OBJCOPY) -O binary -R .eeprom -R .fuse -R .lock -R .signature $< $@
    
    %.o: %.cpp
        $(CXX) -o $@ $(CXXFLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -c
    
    %.o: %.c
        $(CC) -o $@ $(CFLAGS) -MMD -MP -MF $(@:%.o=%.d) $< -c
    
    %.o: %.S
        $(CC) -mmcu=$(MMCU) -c -o $@ $<
    
    $(TARGET).lst: $(TARGET).elf
        $(OBJDUMP) -h -S $< > $@
    
    avrdude: $(TARGET).hex
        avrdude $(AVRDUDE_FLAGS) -e -m flash -i $<
    
    flash: $(TARGET).hex
        $(BOOTHID) $<
    
    clean:
        $(RM) $(OBJECTS) *.elf *.hex *.lst *.map *.d
    

    Thanks for your help