I'm writing my first Makefile for a AVR-project and ran into some problems. It says "undefined reference to `main'" the first time that I execute "make" but spits out all object files and a main.map file. The next time I execute "make" it completes the compilation and also spits out the main.elf and main.hex files.
As I mentioned, I'm new to this but something tells me that it's a linking problem. I have searched for similar posts but haven't found any solution so any help is appreciated.
I have a project folder where the Makefile is and in the project folder a src folder and a header folder exists where the .c files and .h are located.
Here follows the Makefile:
SOURCE_FILES=$(wildcard src/*)
OBJECT_FILES=$(patsubst %.c,%.o,$(SOURCE_FILES))
INCLUDE_PATH=headers/
MCU=atmega328p
CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues
hex : elf
avr-size main.elf
avr-objcopy -R .eeprom -O ihex main.elf main.hex
elf : object
avr-gcc $(CFLAGS) -o main.elf -Wl,-Map,main.map $(wildcard *.o)
object : $(SOURCE_FILES)
avr-gcc $(CFLAGS) -Os -c $(SOURCE_FILES) -I$(INCLUDE_PATH)
load: main.hex
avrdude -c arduino -p m328p -P /dev/ttyACM0 -D -U flash:w:main.hex
clean:
rm *.o *.hex *.map *.elf
EDIT:
The output after the first make:
avr-gcc -g -mmcu=atmega328p -Wall -Wstrict-prototypes -Os -mcall-prologues -o main.elf -Wl,-Map,main.map
/usr/lib/gcc/avr/4.8.2/../../../avr/lib/avr5/crtm328p.o: In function `__bad_interrupt':
../../../../crt1/gcrt1.S:195: undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [elf] Error 1
The output after the second make:
avr-gcc -g -mmcu=atmega328p -Wall -Wstrict-prototypes -Os -mcall-prologues -o main.elf -Wl,-Map,main.map Car.o main.o TinyTimber.o
avr-size main.elf
text data bss dec hex filename
7046 8 855 7909 1ee5 main.elf
avr-objcopy -R .eeprom -O ihex main.elf main.hex