如何故意创建.dep文件?

时间:2018-10-17 21:35:47

标签: c makefile embedded gnu-make preprocessor

我有5个源文件(main.c,memory.c等),我想为所有这些文件生成依赖文件。

当我在线搜索时,我发现大多数人只遭受由编译器创建的那些文件的困扰,有些人通过将其强行添加到目录中来解决此问题。但是,我想有意地将它们创建为(main.dep,memory.dep等)

我如何让make通过我的命令为我自动创建它们:

$ make build

我想这将是我要放入CPPFLAGS的某种顺序,然后将CPPFLAGS包含到我的$(TARGET).out配方中,但是我不知道到底是什么。有什么想法吗?

这是我的Makefile:

include sources.mk

# Platform Overrides

ifeq ($(PLATFORM),MSP432)

# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs
COMPILE = -DMSP432
LDFLAGS_ARCH = -T $(LINKER_FILE)
CFLAGS_ARCH = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -march=armv7e-m 
-mfloat-abi=hard -mfpu=fpv4-sp-d16
OBJS = $(SOURCES:.c=.o)
DUMP = arm-none-eabi-objdump

# Compiler Flags and Defines
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld

endif

ifeq ($(PLATFORM),HOST)
OBJS = $(SOURCES_BOTH:.c=.o)
COMPILE = -DHOST
CC = gcc
DUMP = objdump
endif


TARGET = c1m2
LDFLAGS = -Wl,-Map=$(TARGET).map $(LDFLAGS_ARCH)
CFLAGS = $(CFLAGS_ARCH) $(INCLUDES)  -Wall -Werror -g -O0  -std=c99 
$(COMPILE)
CPPFLAGS =


.PHONY: build
build: $(TARGET).out

.PHONY: compile-all
compile-all: $(OBJS)

CLEANO = main.o memory.o interrupts_msp432p401r_gcc.o 
startup_msp432p401r_gcc.o system_msp432p401r.o
CLEANI = main.i memory.i interrupts_msp432p401r_gcc.i 
startup_msp432p401r_gcc.i system_msp432p401r.i
CLEANASM = main.asm memory.asm interrupts_msp432p401r_gcc.asm 
startup_msp432p401r_gcc.asm system_msp432p401r.asm

.PHONY: clean
clean:
    $(RM) $(TARGET).out $(TARGET).map $(CLEANO) $(CLEANI) $(CLEANASM)

%.o : %.c
    $(CC) -c $< $(CFLAGS) -o $@

%.i : %.c
    $(CC) -E $< $(CFLAGS) -o $@

%.asm :  %.o
$(DUMP) -S --disassemble $< > $@

$(TARGET).out: $(OBJS)
    $(CC) $(OBJS) $(CFLAGS) $(LDFLAGS) -o $@

1 个答案:

答案 0 :(得分:1)

您只需在编译步骤中添加一行即可:

%.o : %.c
    $(CC) -c $< $(CFLAGS) -o $@

将成为:

%.o : %.c
    # dep generation
    $(CC) -MM $< > $<.dep
    # compilation 
    $(CC) -c $< $(CFLAGS) -o $@

如果您希望依赖性文件扩展名是.dep而不是.c.dep,则可以调整以下规则:

%.o : %.c
    # dep generation
    $(CC) -MM $< > $(patsubst %.c,%.dep,%<)
    # compilation 
    $(CC) -c $< $(CFLAGS) -o $@