'Atmega8 - Compile and link C and Assembly-files with one Makefile
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...
Solution 1:[1]
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
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | David Grayson |