(GCC)STM32CubeMX生成的Makefile详解

本文所使用工程由STM32CubeMX生成,使用芯片:STM32F103ZET6,基本只开了时钟。看懂本篇解析需要有一些最基本的Makefile知识。
Makefile文件如下:

##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.0.0] date: [Fri Jul 10 15:04:01 CST 2020]
##########################################################################################################################

# ------------------------------------------------
# Generic Makefile (based on gcc)
#
# ChangeLog :
#	2017-02-10 - Several enhancements + project update mode
#   2015-07-22 - first version
# ------------------------------------------------

######################################
# target
######################################
TARGET = GCC_F103


######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og


#######################################
# paths
#######################################
# Build path
BUILD_DIR = build

######################################
# source
######################################
# C sources
C_SOURCES =  \
Src/main.c \
Src/stm32f1xx_it.c \
Src/stm32f1xx_hal_msp.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \
Src/system_stm32f1xx.c  

# ASM sources
ASM_SOURCES =  \
startup_stm32f103xe.s


#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
 
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m3

# fpu
# NONE for Cortex-M0/M0+/M3

# float-abi


# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# macros for gcc
# AS defines
AS_DEFS = 

# C defines
C_DEFS =  \
-DUSE_HAL_DRIVER \
-DSTM32F103xE


# AS includes
AS_INCLUDES = 

# C includes
C_INCLUDES =  \
-IInc \
-IDrivers/STM32F1xx_HAL_Driver/Inc \
-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \
-IDrivers/CMSIS/Include \
-IDrivers/CMSIS/Include


# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif


# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"


#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = STM32F103ZETx_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


#######################################
# build the application
#######################################
# list of objects
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)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@
	
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@	
	
$(BUILD_DIR):
	mkdir $@		

#######################################
# clean up
#######################################
clean:
	-rm -fR $(BUILD_DIR)
  
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

# *** EOF ***

这么长的文件,其实真正用来编译的指令只有几行:
在这里插入图片描述

而贯穿整篇Makefile的只有三行代码:

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

正向解析(从Makefile文件->输出结果)

如果要正向解析Makefile文件,需要理清整个文件输出流程,即依赖结构。
其实在这篇makefile里,make就等于make all。
在这里插入图片描述
可以看到,make执行的缺省动作就是all这个label,所以当你打下make时,makefile会想办法生成以下三个文件

$(BUILD_DIR)/$(TARGET).elf
$(BUILD_DIR)/$(TARGET).hex
$(BUILD_DIR)/$(TARGET).bin

而我们找到makefile最上面,定义的变量:
在这里插入图片描述
可以清楚的看到TARGET、BUILD_DIR这两个变量定义,所以我们所要生成的目标文件实际是:

build/GCC_F103.elf
build/GCC_F103.hex
build/GCC_F103.bin

也就是在目录build下生成这三个文件。而build目录和这三个文件的生成方法也已经给出:
在这里插入图片描述
其实把BUILD_DIR和TARGET替换掉,也就是:

build/GCC_F103.elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

build/%.hex: build/%.elf | build
	$(HEX) $< $@
	
build/%.bin: build/%.elf | build
	$(BIN) $< $@	
build:
	mkdir $@

由此也可以看出来,只要GCC_F103.elf有了,那GCC_F103.hex和GCC_F103.bin都是在GCC_F103.elf基础上生成的,所以肯定要先生成GCC_F103.elf。而build就是一行指令:mkdir build($@代表目标文件,这里目标文件就是build)。接着往下看,GCC_F103.elf这个文件又依赖于 $(OBJECTS)这个变量,也就是需要先生成这个,然后执行下面两行代码。 $(OBJECTS)具体是什么,我们再看上面变量定义:
在这里插入图片描述
可以看到这个变量又使用了其他变量,还运用了函数addprefix、notdir和sort。而其中新的两个变量也定义在最上面:
在这里插入图片描述

最外层addprefix用法是:

$(addprefix <prefix>, <name-1>, <name-2>...<name-n>)

把前缀prefix加到name前面。
而notdir是把字符串中的路径去掉,比如:

C_SOURCES = F103/Src/main.c
OBJECTS = $(notdir$(C_SOURCES))

最终OBJECT = main.c
还有一个地方$(C_SOURCES:.c=.o),这里的意思是把C_SOURCES里所有以.c结尾的文件替换为.o结尾的文件。
所以替换展开后OBJECTS其实为:

OBJECTS = \
build/main.o \
build/stm32f1xx_it.o \
build/stm32f1xx_hal_msp.o \
build/stm32f1xx_hal_gpio_ex.o \
build/stm32f1xx_hal_tim.o \
build/stm32f1xx_hal_tim_ex.o \
build/stm32f1xx_hal.o \
build/stm32f1xx_hal_rcc.o \
build/stm32f1xx_hal_rcc_ex.o \
build/stm32f1xx_hal_gpio.o \
build/stm32f1xx_hal_dma.o \
build/stm32f1xx_hal_cortex.o \
build/stm32f1xx_hal_pwr.o \
build/stm32f1xx_hal_flash.o \
build/stm32f1xx_hal_flash_ex.o \
build/system_stm32f1xx.o  

接下来看vpath这个关键字,它的用法是

vpath <pattern> <directories>

设置符合pattern的文件的搜索路径是directories
在这里插入图片描述
的意思就是,所有.c文件的搜索路径是$(sort $(dir $(C_SOURCES))) ,sort函数用来去掉重复的文件路径,dir函数即取路径和notdir函数刚好相反,最终结果就是所有.c文件的搜索路径是C_SOURCES这个变量去掉文件名只保留路径,去掉重复的并排好序列。
可以看出这两行代码是处理所有.c文件的,而下面的两行代码是处理.s文件的,处理方式都一样,都是把文件后缀换为.o:
在这里插入图片描述
注意这里OBJECT是用的 += 而不是 =,代表最后OBJECT里面包含了.c和.s的处理结果。至此我们得到了OBJECT这个变量的最终结果:

OBJECTS = \
build/main.o \
build/stm32f1xx_it.o \
build/stm32f1xx_hal_msp.o \
build/stm32f1xx_hal_gpio_ex.o \
build/stm32f1xx_hal_tim.o \
build/stm32f1xx_hal_tim_ex.o \
build/stm32f1xx_hal.o \
build/stm32f1xx_hal_rcc.o \
build/stm32f1xx_hal_rcc_ex.o \
build/stm32f1xx_hal_gpio.o \
build/stm32f1xx_hal_dma.o \
build/stm32f1xx_hal_cortex.o \
build/stm32f1xx_hal_pwr.o \
build/stm32f1xx_hal_flash.o \
build/stm32f1xx_hal_flash_ex.o \
build/system_stm32f1xx.o  
build/startup_stm32f103xe.o

回到我们刚才追溯到OBJECT的地方:
在这里插入图片描述
这里已经分析过build/GCC_F103.elf这个文件依赖OBJECT,其实就是生成它依赖那么多.o文件,那这些.o文件从哪里来?
在这里插入图片描述
可以看到,所有.o的生成规则Makefile也已经给出。这里的 | $(BUILD_DIR)不作过多解释,其实就是如果要生成.o就要先生成build这个目录,如果生成了build那以后都不需要再生成它了,如果你去掉这个或者只去掉管道符号 |,则每次make都会重新生成所有文件,这里贴出来GUN make的原文描述:
在这里插入图片描述
实际这些.o的生成规则其实就是:

$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

而里面用到的变量都在上面有定义,因为内容较多不再截图。
所以这一行展开就是:

arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb     -DUSE_HAL_DRIVER -DSTM32F103xE -IInc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"$(@:%.o=%.d)" -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

可以看到有一部分还未替换的则是根据生成的.o替换为.d和.lst的。其实就是替换变量,具体参数在gcc中的功能需要你自己去查找。(.s生成.o同理,参数并不完全相同)
在生成完所有.o文件后,我们终于可以继续刚才的代码:
在这里插入图片描述
同样的替换变量后:

arm-none-eabi-gcc $(OBJECTS) -mcpu=cortex-m3 -mthumb    -specs=nano.specs -TSTM32F103ZETx_FLASH.ld   -lc -lm -lnosys -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections -o $@
arm-none-eabi-size $@

如果变量为空,则实际展开时用空格代替。
最后生成.hex和.bin:

arm-none-eabi-objcopy -O ihex $< $@
arm-none-eabi-objcopy -O binary -S $< $@
$@可以简单理解为targets,而$<则是第一条prerequisites,而$^是所有prerequisites

我们查看输出结果和我们分析的是否一致:

$ make all
mkdir build
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/main.d" -Wa,-a,-ad,-alms=build/main.lst Src/main.c -o build/main.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_it.d" -Wa,-a,-ad,-alms=build/stm32f1xx_it.lst Src/stm32f1xx
_it.c -o build/stm32f1xx_it.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_msp.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_msp.lst Src
/stm32f1xx_hal_msp.c -o build/stm32f1xx_hal_msp.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_gpio_ex.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_gpio_ex
.lst Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c -o build/stm32f1xx
_hal_gpio_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_tim.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_tim.lst Dri
vers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c -o build/stm32f1xx_hal_tim.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_tim_ex.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_tim_ex.l
st Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c -o build/stm32f1xx_ha
l_tim_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal.lst Drivers/STM
32F1xx_HAL_Driver/Src/stm32f1xx_hal.c -o build/stm32f1xx_hal.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_rcc.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_rcc.lst Dri
vers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c -o build/stm32f1xx_hal_rcc.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_rcc_ex.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_rcc_ex.l
st Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c -o build/stm32f1xx_ha
l_rcc_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_gpio.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_gpio.lst D
rivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c -o build/stm32f1xx_hal_gpio
.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_dma.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_dma.lst Dri
vers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c -o build/stm32f1xx_hal_dma.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_cortex.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_cortex.l
st Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c -o build/stm32f1xx_ha
l_cortex.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_pwr.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_pwr.lst Dri
vers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c -o build/stm32f1xx_hal_pwr.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_flash.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_flash.lst
 Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c -o build/stm32f1xx_hal_f
lash.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/stm32f1xx_hal_flash_ex.d" -Wa,-a,-ad,-alms=build/stm32f1xx_hal_flash_
ex.lst Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c -o build/stm32f
1xx_hal_flash_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32F103xE -I
Inc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
 -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMSIS/Include -IDrivers/C
MSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP
 -MF"build/system_stm32f1xx.d" -Wa,-a,-ad,-alms=build/system_stm32f1xx.lst Src/s
ystem_stm32f1xx.c -o build/system_stm32f1xx.o
arm-none-eabi-gcc -x assembler-with-cpp -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_D
RIVER -DSTM32F103xE -IInc -IDrivers/STM32F1xx_HAL_Driver/Inc -IDrivers/STM32F1xx
_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F1xx/Include -IDrivers/CMS
IS/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections
 -g -gdwarf-2 -MMD -MP -MF"build/startup_stm32f103xe.d" startup_stm32f103xe.s -o
 build/startup_stm32f103xe.o
arm-none-eabi-gcc build/main.o build/stm32f1xx_it.o build/stm32f1xx_hal_msp.o bu
ild/stm32f1xx_hal_gpio_ex.o build/stm32f1xx_hal_tim.o build/stm32f1xx_hal_tim_ex
.o build/stm32f1xx_hal.o build/stm32f1xx_hal_rcc.o build/stm32f1xx_hal_rcc_ex.o
build/stm32f1xx_hal_gpio.o build/stm32f1xx_hal_dma.o build/stm32f1xx_hal_cortex.
o build/stm32f1xx_hal_pwr.o build/stm32f1xx_hal_flash.o build/stm32f1xx_hal_flas
h_ex.o build/system_stm32f1xx.o build/startup_stm32f103xe.o -mcpu=cortex-m3 -mth
umb   -specs=nano.specs -TSTM32F103ZETx_FLASH.ld  -lc -lm -lnosys  -Wl,-Map=buil
d/GCC_F103.map,--cref -Wl,--gc-sections -o build/GCC_F103.elf
arm-none-eabi-size build/GCC_F103.elf
   text    data     bss     dec     hex filename
   3712      20    1572    5304    14b8 build/GCC_F103.elf
arm-none-eabi-objcopy -O ihex build/GCC_F103.elf build/GCC_F103.hex
arm-none-eabi-objcopy -O binary -S build/GCC_F103.elf build/GCC_F103.bin
### 回答1: 在使用gcc编译stm32时,我们通常会使用stm32标准库。标准库是一系列的函数和头文件,可以方便地操作stm32的各种硬件资源。 在编写程序时,为了方便地管理代码和编译,我们常常会使用makefile来构建项目。makefile是一个包含了一系列规则的文本文件,规定了如何编译程序和生成可执行文件。 在使用gccstm32标准库时,我们需要在makefile中进行一些配置和设置。首先,我们需要指定编译器为gcc,可以使用`CC = gcc`来定义编译器变量。接下来,我们需要设置编译选项,例如指定目标芯片型号、编译的优化级别等。可以使用`CFLAGS = -mcpu=cortex-m3 -mthumb -Os`来设置。 在项目中,我们还需要指定头文件搜索路径和库文件路径。由于stm32标准库可能需要包含较多的头文件和库文件,我们可以使用`INCLUDES = -I./inc`来指定头文件搜索路径,`LIBS = -L./lib`来指定库文件路径。 在makefile中,我们还需要定义编译规则,包括编译和链接。例如,我们可以使用以下规则来编译源文件并生成可执行文件: ``` main.elf: main.o stm32_startup.o $(CC) $(CFLAGS) $(INCLUDES) $(LIBS) -o main.elf main.o stm32_startup.o ``` 这个规则表示,要生成main.elf可执行文件,需要先编译main.o和stm32_startup.o两个目标文件,然后使用gcc进行链接并生成可执行文件。$(CC)表示使用的编译器,$(CFLAGS)表示编译选项,$(INCLUDES)表示头文件搜索路径,$(LIBS)表示库文件路径。 最后,我们还可以定义一些其他的规则,例如清除中间文件、烧录目标等。 总之,通过编写适当的makefile,我们可以方便地使用gcc编译stm32的程序,并使用stm32标准库来操作硬件资源。 ### 回答2: gcc是一种常用的编译器,而STM32则是一系列基于ARM Cortex-M内核的单片机产品。当我们在开发STM32项目时,通常会使用到gcc编译器,同时也需要使用到STM32的标准库。那么如何在项目中正确地使用gcc编译器和STM32标准库呢?这就需要借助makefile来完成。 makefile是一种文本文件,其中定义了编译、链接和构建项目所需的规则。在使用gcc编译器和STM32标准库时,我们可以通过makefile来自动化地管理编译过程,提高效率。 首先,我们需要在makefile中定义编译器的路径及参数。可以使用gcc命令行选项来指定编译器的路径,并使用-D参数定义一些预处理宏,以支持不同的编译选项和功能。 接下来,我们需要定义源文件和目标文件的依赖关系,以及编译和链接的规则。通过在makefile中明确规定依赖关系,可以确保在进行编译和链接时,只对修改过的文件进行重新编译和链接操作,提高编译速度。 在构建STM32项目时,我们还需要包含STM32的标准库头文件,并链接对应的库文件。可以通过在makefile中设置INCLUDES和LIBS变量来指定相应的路径。 最后,在makefile中定义一个默认的目标(all),来指定编译和链接的规则。当我们执行make命令时,makefile会自动根据定义的规则来执行编译和链接操作,生成最终的可执行文件。 总的来说,通过使用gcc编译器和STM32标准库,并结合makefile的自动化管理功能,可以更方便地进行STM32项目的开发和构建,提高效率和代码质量。 ### 回答3: gcc是一种开源的C语言编译器,可用于编译嵌入式系统中的代码。stm32是一系列由STMicroelectronics公司生产的32位ARM Cortex-M微控制器。标准库是一组常用函数和宏定义的集合,可用于简化程序的开发。 在使用gcc编译stm32程序时,需要编写一个makefile文件来指示编译器如何编译和链接代码。makefile文件是一个文本文件,其中包含了一系列规则和命令,用于描述编译过程中的依赖关系和操作指令。 在makefile中,我们需要为编译器提供必要的编译选项,以指示编译器使用正确的指令集和连接器脚本。我们还需要指定源代码文件的路径和依赖关系,以确保所有依赖的文件都被正确编译和链接。 在编译stm32程序时,我们通常需要借助于STM32Cube软件包提供的HAL(硬件抽象层)库和CMSIS(Cortex Microcontroller Software Interface Standard)库。在makefile中,我们需要将这些库的路径添加到编译器的搜索路径中,以确保编译器能够找到并正确链接这些库。 除了库的路径配置外,我们还可以在makefile中定义一些宏,用于指示编译器启用或禁用某些功能。例如,我们可以定义宏来启用调试输出、优化代码或配置硬件引脚。 总之,通过编写一个适当的makefile文件,我们可以使用gcc编译stm32程序,并包含所需的库和宏定义。这样可以大大简化程序的开发过程,提高代码的可维护性和可重用性。
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值