STM32 Makefile的设置和工程管理
Makefile
直接附上Makefile的内容:
######################################
# target
######################################
TARGET = demo_project
# debug build?
DEBUG = 1
# optimization
OPT = -O1
# Build path
BUILD_DIR = Output
######################################
# source
######################################
# C sources
# wildcard 获取当前目录下全部文件
# 设置全部需要编译的文件
C_SOURCES =
C_SOURCES += $(wildcard *.c ./Src/*.c)
C_SOURCES += $(wildcard *.c ./ProjectDrivers/Src/*.c)
# ASM sources
ASM_SOURCES =
ASM_SOURCES += $(wildcard *.s ./Src/*.s)
#######################################
# Tools
#######################################
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# FLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m4
# fpu
FPU = -mfpu=fpv4-sp-d16
# float-abi
FLOAT-ABI = -mfloat-abi=hard
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
# macros for gcc
# AS defines
AS_DEFS =
# C defines
C_DEFS = \
-DUSE_HAL_DRIVER \
-DSTM32F407xx
# AS includes
AS_INCLUDES =
# C includes
PROJECTDRIVERPATH = ProjectDrivers/Inc
CMSISPATH = Drivers/CMSIS
# addprefix的功能增加前缀,例如$(addprefix -I,./Inc)执行后为 -I ./Inc
C_INCLUDES =
C_INCLUDES += $(addprefix -I,./Inc)
C_INCLUDES += $(addprefix -I,$(PROJECTDRIVERPATH))
C_INCLUDES += $(addprefix -I,$(PROJECTDRIVERPATH)/Legacy)
C_INCLUDES += $(addprefix -I,$(CMSISPATH)/Device/ST/STM32F4xx/Include)
C_INCLUDES += $(addprefix -I,$(CMSISPATH)/Include)
# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -s -fdata-sections -ffunction-sections
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
# link script
LDSCRIPT = ./Src/STM32F407ZGTx_FLASH.ld
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
#######################################
# default action: build all
#######################################
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
# list of objects
# notdir去掉文件名中的路径部分
# :.c = .o的意思是C文件对应相应的.o文件
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
OBJ_DIR = obj
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
@$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
@echo $(notdir $(<:.c=.o))
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
@$(AS) -c $(CFLAGS) $< -o $@
@echo $(notdir $(<:.s=.o))
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
@echo linking...
$(SZ) $@
rm -fR $(BUILD_DIR)/$(OBJ_DIR)
mkdir $(BUILD_DIR)/$(OBJ_DIR)
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(HEX) $< $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) $< $@
mv -f $(BUILD_DIR)/*.o $(BUILD_DIR)/$(OBJ_DIR)/
mv -f $(BUILD_DIR)/*.d $(BUILD_DIR)/$(OBJ_DIR)/
mv -f $(BUILD_DIR)/*.lst $(BUILD_DIR)/$(OBJ_DIR)/
$(BUILD_DIR):
mkdir $@
#######################################
# clean up
#######################################
clean:
@-rm -rf $(BUILD_DIR)/*
# dependencies
-include $(wildcard $(BUILD_DIR)/*.d)
# *** EOF ***
说明
这份Makefile是由CubeMX生成的Makefile,原始版本中有些路径和目录设置并不十分合理,这里做了一些修改
源文件列表设置
原本的Makefile中的源文件列表是通过换行逐一添加的,这里简化配置的过程,用wildcard函数代替实现
C_SOURCES =
C_SOURCES += $(wildcard *.c ./Src/*.c)
C_SOURCES += $(wildcard *.c ./ProjectDrivers/Src/*.c)
更换文件路径
stm32中常用的HAL库和CMSIS放在同一路径下,但是一个项目中并不需要全部的HAL库源文件,因此,因此将用到的HAL库和CMSIS文件摘出至ProjectDrivers
文件夹下,只编译需要的HAL库文件,同时将编译过程中产生的中间文件(.o .d .lst)搬移至其他目录,不与hex文件混合
调整make过程中的打印信息
由于Makefile的包含路径较多,将编译指令全部打出的话会影响查看编译过程中的错误信息,这里修改如下:
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
@$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
@echo $(notdir $(<:.c=.o))
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
@$(AS) -c $(CFLAGS) $< -o $@
@echo $(notdir $(<:.s=.o))
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
@echo linking...
$(SZ) $@
rm -fR $(BUILD_DIR)/$(OBJ_DIR)
mkdir $(BUILD_DIR)/$(OBJ_DIR)
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(HEX) $< $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) $< $@
mv -f $(BUILD_DIR)/*.o $(BUILD_DIR)/$(OBJ_DIR)/
mv -f $(BUILD_DIR)/*.d $(BUILD_DIR)/$(OBJ_DIR)/
mv -f $(BUILD_DIR)/*.lst $(BUILD_DIR)/$(OBJ_DIR)/
$(BUILD_DIR):
mkdir $@
.s文件和.c文件生成.o文件的过程中只打印当前正在编译的文件名,链接的过程中只打印link...语句表示正在链接,不显示链接全部过程,修改后的shell的编译结果如下:
$ make
main.o
stm32f4xx_hal_msp.o
stm32f4xx_it.o
system_stm32f4xx.o
stm32f4xx_hal.o
stm32f4xx_hal_cortex.o
stm32f4xx_hal_dma.o
stm32f4xx_hal_dma_ex.o
stm32f4xx_hal_exti.o
stm32f4xx_hal_flash.o
stm32f4xx_hal_flash_ex.o
stm32f4xx_hal_flash_ramfunc.o
stm32f4xx_hal_gpio.o
stm32f4xx_hal_pwr.o
stm32f4xx_hal_pwr_ex.o
stm32f4xx_hal_rcc.o
stm32f4xx_hal_rcc_ex.o
stm32f4xx_hal_tim.o
stm32f4xx_hal_tim_ex.o
startup_stm32f407xx.o
linking...
arm-none-eabi-size Output/demo_project.elf
text data bss dec hex filename
4348 20 1572 5940 1734 Output/demo_project.elf
rm -fR Output/obj
mkdir Output/obj
arm-none-eabi-objcopy -O ihex Output/demo_project.elf Output/demo_project.hex
arm-none-eabi-objcopy -O binary -S Output/demo_project.elf Output/demo_project.bin
mv -f Output/*.o Output/obj/
mv -f Output/*.d Output/obj/
mv -f Output/*.lst Output/obj/
可以看出编译过程已经很简洁,同时warning和error信息可以很快定位,工程也更易于扩展。