0

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
flott
  • 231
  • 1
  • 11
  • 1
    Makefile targets should generally be the output file themselves and not a `name` for the target. It helps things work better. – Etan Reisner Mar 15 '16 at 22:17
  • Can you post the full output from running `make` those two times? – Etan Reisner Mar 15 '16 at 22:17
  • Thanks for the reply and that piece of information @EtanReisner I updated my post with the outputs after each make, well the relevant parts, I removed a ton of compiler warnings caused by a (so far) non-compatible kernel. – flott Mar 15 '16 at 22:38
  • What's above that first snippet? I want to see the actual commands make runs. – Etan Reisner Mar 15 '16 at 22:40
  • I noticed now that the differences seems to be that it doesn't substitute the $(wildcard *.o) with the object files the first time but it does the second time make is executed. – flott Mar 15 '16 at 22:43
  • 1
    Yup. That's the problem. The first time there *aren't* any. Don't use that. Use `OBJECT_FILES`. That's why you created it. – Etan Reisner Mar 15 '16 at 22:44
  • Yes of course it is! Thank you Etan, I feel kinda silly. The reason why i didn't use it as I first intended to do is because it searches for the object files in /src which is wrong, so I guess I need to remove the /src prefix from the OBJECT_FILES somehow. – flott Mar 15 '16 at 22:49
  • 1
    Look into the `patsubst` and/or `notdir` make functions. – Etan Reisner Mar 15 '16 at 22:56
  • encapsulating the patsubst in OBJECT_FILES with a notdir did the trick, thank you for the help! – flott Mar 15 '16 at 23:24

0 Answers0