0

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

Thats 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 dont have any *.S-files in the folder it works like a charm.

This line was hardcoded just for debuging.

 %.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...

Nico
  • 320
  • 1
  • 4
  • 12
  • 1
    That command doesn't even mention the asm file, if it fails, it should fail the same without the asm rule too. Check why it doesn't. – Jester Jan 23 '17 at 16:22
  • googling this, i found "https://bugs.eclipse.org/bugs/show_bug.cgi?id=330249#c11" – Tommylee2k Jan 23 '17 at 16:24
  • @Jester , yep it fails without the asm rule too. But when I use the Makefile without an asm-file in the dir i try to compile, it will work. – Nico Jan 23 '17 at 16:58
  • Make sure you don't have a name collision. The reason for the different behavior is that you use wildcards to collect your `SOURCES` and `OBJECTS`. – Jester Jan 23 '17 at 17:07
  • No name collision. There are 4 files is the dir: "lcd.c", "lcd.h", "main.c" and "timing.S" – Nico Jan 23 '17 at 17:49
  • Ok have to correct myself. When i remove the asm file from the folder, the same error occours. I think the reason is a missing flag because in the main.c im using variables wich are declared in the asm file. – Nico Jan 23 '17 at 18:16

2 Answers2

0

The error message makes it pretty clear:

cc1: error: to generate dependencies you must specify either -M or -MM

The compiler wants you to give it one of those flags. You are currently not giving it either of those flags, as we can see from the command that got printed right before the error message.

To solve this problem, you would add one of those flags to your Makefile. There are lots of places you could add a flag, but for this one it makes the most the sense to add it right after any place where you have -MMD.

Alternatively, you could disable GCC dependency generation entirely by removing the -MMD option.

You can also consult the GCC documentation for these preprocessor options.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

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

Nico
  • 320
  • 1
  • 4
  • 12