使用 wsl+makefile+clangd编辑stm32代码环境的搭建
安装wsl环境
可以看看下面的文章安装与换源都提及,相信大家可以安装成功的
https://www.cnblogs.com/banmei-brandy/p/16218660.html
安装make、bear、clangd、arm-none-eabi-gcc、最新的构建库
sudo apt install make bear clangd gcc-arm-none-eabi libstdc++-arm-none-eabi-newlib
下载工程模板
作者是在github上浏览到的一个对std库做的一个makefile工程模板,稍加修改使用的,链接覆下
https://github.com/ertuil/stm32_template
修改makefile文档,以及使用bear生产compile_commands.json文件使用clangd进行补全以及错误分析
个人修改后的makefile模板如下
Author = ertuil
######################################
# target
######################################
TARGET = target
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og
#######################################
# paths
#######################################
# source path
# firmware library path
PERIFLIB_PATH =
# Build path
BUILD_DIR = build
######################################
# source
######################################
# C sources
C_SOURCES = \
$(shell find ./ -name '*.c')
# ASM sources
ASM_SOURCES = \
startup_stm32f103xb.s
######################################
# firmware library
######################################
PERIFLIB_SOURCES =
#######################################
# 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
AR = $(GCC_PATH)/$(PREFIX)ar
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
# float-abi
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
# macros for gcc
# AS defines
AS_DEFS =
# C defines
C_DEFS = \
-DSTM32F103X_LD \
-DSTM32F10X_LD \
-DUSE_STDPERIPH_DRIVER\
# AS includes
AS_INCLUDES =
# C includes
C_INCLUDES = -I CORE \
-I FWLIB/inc \
-I HARDWARE \
-I SYSTEM \
-I USER \
-I /usr/lib/arm-none-eabi/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)" -MT"$(@:%.o=%.d)"
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = STM32F103R8Tx_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 .dep $(BUILD_DIR)
flash:
st-flash write $(BUILD_DIR)/$(TARGET).bin 0x8000000
erase:
st-flash erase
#######################################
# dependencies
#######################################
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
# *** EOF ***
在makefile目录下使用bear生成compile_commandsr.json文件
bear -- make -j8
这时会发现在文件目录下生成了一个compile_commandsr.json文件,之后可以通过配置好的nvim进行编辑,或者通过vscode clangd插件进行编辑