将Makefile调整为仅在文件更改后重新编译

时间:2019-01-19 18:14:45

标签: c makefile

我对我的Makefile有疑问。 Makefile打算使用Cosmic编译器编译包含STM8 µC代码的C文件。问题在于,每次我调用构建目标时,所有可用的源文件都会重新编译而没有任何更改。我真的是Makefiles领域的新手,我也不知道如何解决它。

第二个问题与两个目标“%.o:src /%。c”和%.o:src / stm8 /%。c有关。它们的作用完全相同,我更喜欢一个通用的,能够处理src文件夹中所有子目录的文件。使用此解决方案时,需要为src文件夹的每个子文件夹添加一个附加规则

#***************PROJECT INFORMATIONS****************
PROJECT_NAME = stm8template
MODULES = stm8

#****************SET BUILD MODE*********************
ifeq ($(MODE), )
  MODE=Debug
endif

#***************DIRECTORY INFORMATION***************
SRCDIR = src
INCLUDES = includes
OUTPUT_DIR = bin/$(MODE)

#**************HELPER FUNCTIONS*********************
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

#***************FILE SPECIFICATIONS***************
SOURCE_FILES = $(foreach d, $(call rwildcard,$(SRCDIR),*.c), $(notdir $d))
OBJECT_FILES = $(patsubst %.c, %.o, $(SOURCE_FILES))
HEADER_FILES = $(wildcard $(INCLUDES)/*.h) $(wildcard $(INCLUDES)/**/*.h)
INCLUDE_DIRS_PARAM=$(foreach d, $(MODULES), -iincludes/$d) -iincludes -iC:\Hstm8

#***************COMPILER INFORMATIONS***************
CC = cxstm8
LIBS = -l +mods0
ifeq ("$(MODE)","Debug")
CFLAGS = $(INCLUDE_DIRS_PARAM) -cl$(OUTPUT_DIR) -co$(OUTPUT_DIR) -pxp -no -pp +debug 
else
CFLAGS = $(INCLUDE_DIRS_PARAM) -cl$(OUTPUT_DIR) -co$(OUTPUT_DIR) -pxp -no -pp
endif

#***************LINKER INFORMATIONS***************
LINKFILE=$(OUTPUT_DIR)\$(PROJECT_NAME).lkf
OUTFILE=$(PROJECT_NAME)
LFLAGS = -lC:\Lib

#*************FLASHER CONFIGURATIONS***************
FLASHER_PATH="C:\Program Files (x86)\STMicroelectronics\st_toolset\stvp\STVP_CmdLine.exe"
DEVICE=STM8S105x6
PORT=USB
PROG_MODE=SWIM
BOARD_NAME=ST-LINK
FLASHER_PARAM = -no_loop

#***************BUILT TARGETS***************
all: build run

%.o: src/%.c
  $(info ********** Compile $< ***********)
  $(CC) $(CFLAGS) $(LIBS) $<

%.o: src/stm8/%.c
  $(info ********** Compile $< ***********)
  $(CC) $(CFLAGS) $(LIBS) $<

build: $(OBJECT_FILES)
  $(info ********** Build the Application ***********)
  clnk -m $(OUTPUT_DIR)\$(OUTFILE).map -o $(OUTPUT_DIR)\$(OUTFILE).sm8 $(LINKFILE)
  cvdwarf $(OUTPUT_DIR)\$(OUTFILE).sm8 
  chex -o $(OUTPUT_DIR)\$(OUTFILE).s19 $(OUTPUT_DIR)\$(OUTFILE).sm8

run:
  $(info ********** Flashing the Application ***********)
  $(FLASHER_PATH) -BoardName=$(BOARD_NAME) -Device=$(DEVICE) -Port=$(PORT) -ProgMode=$(PROG_MODE) -FileProg="$(OUTPUT_DIR)\$(OUTFILE).s19" $(FLASHER_PARAM) 

2 个答案:

答案 0 :(得分:2)

this.cast = allCast[1].map(el => el.name).slice(0, 4); 目标永远不会被创建,因此每次您运行build(或makemake all)时都会执行目标,因此该程序已链接每次。

更改您的make build目标,使其为假:

build

,因此它取决于程序,而不是目标文件:

.PHONY: build clean

,然后有一个规则(配方)在目标文件较新的情况下构建程序:

build: $(OUTPUT_DIR)\$(OUTFILE).sm8

我不确定我为该程序选择了正确的后缀是100%。我还将创建一系列宏,以避免出现重复:

$(OUTPUT_DIR)\$(OUTFILE).sm8: $(OBJECT_FILES)
    $(info ********** Build the Application ***********)
    clnk -m $(OUTPUT_DIR)\$(OUTFILE).map -o $(OUTPUT_DIR)\$(OUTFILE).sm8 $(LINKFILE)
    cvdwarf $(OUTPUT_DIR)\$(OUTFILE).sm8 
    chex -o $(OUTPUT_DIR)\$(OUTFILE).s19 $(OUTPUT_DIR)\$(OUTFILE).sm8

此外,由于我主要在Unix上工作,因此我将使用OUTFILE.sm8 = $(OUTPUT_DIR)\$(OUTFILE).sm8 OUTFILE.s19 = $(OUTPUT_DIR)\$(OUTFILE).s19 OUTFILE.map = $(OUTPUT_DIR)\$(OUTFILE).map build: $(OUTFILE.sm8) $(OUTFILE.sm8): $(OBJECT_FILES) $(info ********** Build the Application ***********) clnk -m $(OUTFILE.map) -o $(OUTFILE.sm8) $(LINKFILE) cvdwarf $(OUTFILE.sm8) chex -o $(OUTFILE.s19) $(OUTFILE.sm8) 而不是/,但这只是次要的细节。

答案 1 :(得分:1)

更新: 谢谢大家的帮助。我以如下所示的方式更改了Makefile。第二个问题现在已解决,但第一个问题仍然存在。

每次调用构建规则时,都会重新编译所有.c文件。我认为,仅编译更改的文件是使用make的主要目的/好处。所以出了点问题,但不幸的是我没有找到错误。

#***************PROJECT INFORMATIONS****************
PROJECT_NAME = stm8template
MODULES = stm8

#****************SET BUILD MODE*********************
ifeq ($(MODE), )
    MODE=Debug
endif

#***************DIRECTORY INFORMATION***************
SRCDIR = src
INCLUDES = includes

#**************HELPER FUNCTIONS*********************
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

#***************FILE SPECIFICATIONS***************
SOURCE_FILES = $(foreach d, $(call rwildcard,$(SRCDIR),*.c), $(notdir $d))
OBJECT_FILES = $(patsubst %.c, %.o, $(call rwildcard,$(SRCDIR),*.c))
HEADER_FILES = $(wildcard $(INCLUDES)/*.h) $(wildcard $(INCLUDES)/**/*.h)
INCLUDE_DIRS_PARAM=$(foreach d, $(MODULES), -iincludes/$d) -iincludes -iC:\Hstm8

#***************COMPILER INFORMATIONS***************
CC = cxstm8
LIBS = -l +mods0
ifeq ("$(MODE)","Debug")
  CFLAGS = $(INCLUDE_DIRS_PARAM) -cl$(OUTPUT_DIR) -co$(OUTPUT_DIR) -pxp -no -pp +debug 
else
  CFLAGS = $(INCLUDE_DIRS_PARAM) -cl$(OUTPUT_DIR) -co$(OUTPUT_DIR) -pxp -no -pp
endif

#***************LINKER INFORMATIONS***************
LINKFILE=$(OUTPUT_DIR)\$(PROJECT_NAME).lkf
LFLAGS = -LC:\Lib

#*******************OUTPUT FILES********************
OUTPUT_DIR = bin/$(MODE)
OUTFILE=$(PROJECT_NAME)
OUTFILE.sm8 = $(OUTPUT_DIR)\$(OUTFILE).sm8
OUTFILE.s19 = $(OUTPUT_DIR)\$(OUTFILE).s19
OUTFILE.map = $(OUTPUT_DIR)\$(OUTFILE).map
TARGET_FILE=$(OUTPUT_DIR)\$(PROJECT_NAME).elf

#*************FLASHER CONFIGURATIONS***************
FLASHER_PATH="C:\Program Files (x86)\STMicroelectronics\st_toolset\stvp\STVP_CmdLine.exe"
DEVICE := STM8S105x6
PORT=USB
PROG_MODE=SWIM
BOARD_NAME=ST-LINK
FLASHER_PARAM = -no_loop

#***************BUILT TARGETS***************
.PHONY: all run build clean
all: build run

%.o: %.c
  $(info ********** Compile $< ***********)
  $(CC) $(CFLAGS) $(LIBS) $<

build: $(OUTPUT_DIR)\$(PROJECT_NAME).elf

$(TARGET_FILE): $(OBJECT_FILES)
  $(info ********** Build the Application ***********)
  clnk -m $(OUTFILE.map) -o $(OUTFILE.sm8) $(LINKFILE)
  cvdwarf $(OUTFILE.sm8) 
  chex -o $(OUTFILE.s19) $(OUTFILE.sm8)

run:
  $(info ********** Flashing the Application ***********)
  $(FLASHER_PATH) -BoardName=$(BOARD_NAME) -Device=$(DEVICE) -Port=$(PORT) -ProgMode=$(PROG_MODE) -FileProg="$(OUTPUT_DIR)\$(OUTFILE).s19" $(FLASHER_PARAM)