Search code examples
makefileavr-gcc

I don't have this line: "cc drone.c hex1 clean -o drone"


I make a simple makefile for compile the code for a project. when I use the command:

$ make drone

The output files are correct, but the make utility gives me a result that I don't understand (from line 10 to line 14):

1: avr-gcc -g -Os -mmcu=atmega328p -DF_CPU=16000000UL -c drone.c
2: drone.c:1:0: warning: "F_CPU" redefined
3: #define F_CPU 16000000
4: ^
5: <command-line>:0:0: note: this is the location of the previous definition
6: avr-gcc -g -Os -mmcu=atmega328p -DF_CPU=16000000UL -c ./lib/lib_uart.c
7: avr-gcc -g -mmcu=atmega328p -o drone.elf drone.o lib_uart.o
8: avr-objcopy -j .text -j .data -O ihex drone.elf drone.hex
9: rm *.o *.elf
10: cc     drone.c hex1 clean   -o drone
11: cc: error: hex1: No such file or directory
12: cc: error: clean: No such file or directory
13: <builtin>: recipe for target 'drone' failed
14: make: *** [drone] Error 1

The makefile text is:

CPU=atmega328p
CLK=16000000UL
PRG=avrisp
BRATE=-b 19200
PORT=ttyACM0
libs_comando=lib_uart.o
libs_drone=lib_uart.o

drone: hex1 clean
comando: hex2 clean

# drone

hex1: elf1
    avr-objcopy -j .text -j .data -O ihex drone.elf drone.hex

elf1: obj1 $(libs_drone)
    avr-gcc -g -mmcu=$(CPU) -o drone.elf drone.o $(libs_drone)

obj1:
    avr-gcc -g -Os -mmcu=$(CPU) -DF_CPU=$(CLK) -c drone.c

#--------------------------------------------------------------------------------

# comando

hex2: elf2
    avr-objcopy -j .text -j .data -O ihex comando.elf comando.hex

elf2: obj2 $(libs_comando)
    avr-gcc -g -mmcu=$(CPU) -o comando.elf comando.o $(libs_comando)

obj2:
    avr-gcc -g -Os -mmcu=$(CPU) -DF_CPU=$(CLK) -c comando.c

#--------------------------------------------------------------------------------

# library

lib_uart.o:
    avr-gcc -g -Os -mmcu=$(CPU) -DF_CPU=$(CLK) -c ./lib/lib_uart.c

#--------------------------------------------------------------------------------

clean:
    rm *.o *.elf

flash_drone: drone.hex
    avrdude -c $(PRG) -p $(CPU) $(BRATE) -P /dev/$(PORT) -U flash:w:drone.hex:i

flash_comando: comando.hex
    avrdude -c $(PRG) -p $(CPU) $(BRATE) -P /dev/$(PORT) -U flash:w:comando.hex:i

Solution

  • The problem is drone: hex1 clean rule. Since you've got drone.c file in your current directory, implicit rule %: %.c is matched. The easiest ways to fix this are to disble implicit rules, or rename drone to something else, or move drone.c file in another directory (say src).

    A better solution is not to fight implicit rules but to build on them. Here's a snippet.

    # Programs
    CC      := avr-gcc
    OBJCOPY := avr-objcopy
    
    # Flags
    CFLAGS      := -g -Os
    TARGET_ARCH := -mmcu=atmega328p -DF_CPU=16000000UL
    OBJFLAGS    := -j .text -j .data -O ihex 
    
    # Libraray file location
    vpath lib_%.c lib
    
    all: drone.hex
    
    drone: lib_uart.o
    
    %.hex: %
        $(OBJCOPY) $(OBJFLAGS) $< $@
    

    Will produce:

    avr-gcc -g -Os  -mmcu=atmega328p -DF_CPU=16000000UL -c -o lib_uart.o lib/lib_uart.c
    avr-gcc -g -Os   -mmcu=atmega328p -DF_CPU=16000000UL drone.c lib_uart.o   -o drone
    avr-objcopy -j .text -j .data -O ihex  drone drone.hex