我没有这句话:“cc drone.c hex1 clean -o drone”

时间:2016-07-22 11:36:18

标签: makefile avr-gcc

我创建了一个简单的makefile来编译项目的代码。当我使用命令时:

$ make drone

输出文件是正确的,但是make实用程序给了我一个我不理解的结果(从第10行到第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

makefile文本是:

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

1 个答案:

答案 0 :(得分:1)

问题是drone: hex1 clean规则。由于您当前目录中有drone.c个文件,因此隐式规则%: %.c匹配。解决此问题的最简单方法是隐藏隐式规则,或将drone重命名为其他内容,或将drone.c文件移到另一个目录中(例如src)。

更好的解决方案不是打击隐式规则,而是建立在它们之上。这是一个片段。

# 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) $< $@

将产生:

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