我有一个makefile,我对其进行了编辑,以便将目标文件转到单独的目录。
我编辑了makefile,它现在正在执行我想要的操作,但是在链接器阶段它无法按预期工作,因为我做错了。
我有一个sources
文件和makefile
,如下所示:
源文件
TARGET = demo
SRC_DIR = modbus/ascii/ \
modbus/functions/ \
modbus/port/ \
modbus/rtu/ \
modbus/ \
./
INCLUDE_DIR = modbus/include/ \
modbus/port/ \
modbus/rtu \
modbus/ascii \
./
SOURCE = modbus/ascii/mbascii.c \
modbus/functions/mbfunccoils.c \
modbus/functions/mbfuncdiag.c \
modbus/functions/mbfuncdisc.c \
modbus/functions/mbfuncholding.c \
modbus/functions/mbfuncinput.c \
modbus/functions/mbfuncother.c \
modbus/functions/mbutils.c \
modbus/port/port.c \
modbus/port/portevent.c \
modbus/port/portserial.c \
modbus/port/porttimer.c \
modbus/rtu/mbcrc.c \
modbus/rtu/mbrtu.c \
modbus/mb.c \
demo.c \
startup_LPC17xx.c \
system_LPC17xx.c
Makefile
include ./sources
ARCH = arm-none-eabi
BUILD_DIR := build/objs
# Tool definitions
CC = $(ARCH)-gcc
LD = $(ARCH)-gcc
AR = $(ARCH)-ar
AS = $(ARCH)-as
CP = $(ARCH)-objcopy
OD = $(ARCH)-objdump
SIZE = $(ARCH)-size
RM = rm
Q = # @./quiet "$@"
# Flags
CFLAGS = -W -Wall -O0 --std=gnu99 -fgnu89-inline -mcpu=cortex-m3 -mthumb
CFLAGS += -ffunction-sections -fdata-sections
ASFLAGS =
LDFLAGS = -nostartfiles -specs=nosys.specs
CPFLAGS =
ODFLAGS = -x --syms
PRFLAGS ?=
# Source files
LINKER_SCRIPT = LPC17xx.ld
OBJS = $(SOURCE:.c=.o)
BUILD_OBJS := $(patsubst %,$(BUILD_DIR)/%,$(SOURCE:.c=.o))
OBJ_DIR := $(dir $(BUILD_OBJS))
INC_PARAMS = $(foreach d, $(INCLUDE_DIR), -I$d)
print-% : ; @echo $* = $($*)
.PHONY: all size clean nuke
all: $(TARGET).bin $(TARGET).hex
isp: $(TARGET).bin
# @./quiet $< cp $^ $(MBED_VOLUME)/
size: $(TARGET).elf
@$(SIZE) $<
%.hex: %.elf
$Q $(CP) $(CPFLAGS) -O ihex $< $*.hex
%.bin: %.elf
$Q $(CP) $(CPFLAGS) -O binary $< $*.bin
$(TARGET).elf: $(OBJS)
@touch $(@:.elf=.map)
$Q $(LD) -Xlinker -Map $(@:.elf=.map) $(LDFLAGS) -T $(LINKER_SCRIPT) $(BUILD_DIR)/$^ -o $@
$Q $(OD) $(ODFLAGS) $@ > $(@:.elf=.dump)
@$(SIZE) $@
$(OBJS): %.o: %.c
mkdir -p $(dir $(BUILD_OBJS))
$Q $(CC) $(CFLAGS) $(INC_PARAMS) -c $< -o $(BUILD_DIR)/$@
.PHONY: clean
clean: CRUFT=$(shell find . -name '*.o' -o -name '*.d')
clean: ; rm -f $(CRUFT); rm -f *.elf *.hex *.bin *.dump *.map; rm -rf $(BUILD_DIR)/*
nuke: clean
-rm -f *.hex *.bin *.dump *.map
问题所在了:
$Q $(LD) -Xlinker -Map $(@:.elf=.map) $(LDFLAGS) -T $(LINKER_SCRIPT) $(BUILD_DIR)/$^ -o $@
其输出为:
arm-none-eabi-gcc -Xlinker -Map demo.map -nostartfiles -specs=nosys.specs -T LPC17xx.ld build/objs/modbus/ascii/mbascii.o modbus/functions/mbfunccoils.o modbus/functions/mbfuncdiag.o modbus/functions/mbfuncdisc.o modbus/functions/mbfuncholding.o modbus/functions/mbfuncinput.o modbus/functions/mbfuncother.o modbus/functions/mbutils.o modbus/port/port.o modbus/port/portevent.o modbus/port/portserial.o modbus/port/porttimer.o modbus/rtu/mbcrc.o modbus/rtu/mbrtu.o modbus/mb.o demo.o startup_LPC17xx.o system_LPC17xx.o -o demo.elf
输出命令仅从build/objs
下正确目录中选择第一个目标文件。
其余对象文件路径没有build/objs/
。
请有人帮助我解决此问题吗?
答案 0 :(得分:1)
您违反了Mad Scientist's second rule of makefiles,并引发了超出您所知的更多问题。
看看这些规则(simplified):
$(TARGET).elf: $(OBJS)
$(LD) $(BUILD_DIR)/$^ -o $@
$(OBJS): %.o: %.c
$(CC) -c $< -o $(BUILD_DIR)/$@
假设构建目录为build/
,而目标文件为build/foo.o
。第二个规则的目标是foo.o
,但实际上建立的是build/foo.o
。同样,第一个规则以foo.o
为前提,但实际上并没有使用foo.o
,而是使用build/foo.o
这两个错误在某种意义上互相抵消了;使成功建立主要目标。但是,正如您所发现的,如果存在多个目标文件,则会遇到麻烦,因为如果先决条件列表为foo.o bar.o
,那么
`$(BUILD_DIR)/$^`
扩展到
build/foo.o bar.o
此外,即使build/foo.o
存在并且是最新的,Make也会运行第二条规则,如果foo.o
存在,Make将无法运行第二条规则。非PHONY规则的目标应该是它生成的文件的名称,非PHONY前提条件应该是存在相关的文件的名称:
$(TARGET).elf: $(BUILD_OBJS)
$(LD) $^ -o $@
$(BUILD_OBJS): $(BUILD_DIR)/%.o: %.c
$(CC) -c $< -o $@