cmake构建stm32工程
用习惯了cmake,cmake的编译输出比makefile好看许多。对于stm32cubemx生成的makefile工程,我是否可以转换成cmake的工程呢?
生成makefile
我首先使用stm32cubemx生成了一个点亮led的工程。该工程的makefile如下
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [2.29.2] date: [Mon Aug 13 11:26:30 CST 2018]
##########################################################################################################################
# ------------------------------------------------
# Generic Makefile (based on gcc)
#
# ChangeLog :
# 2017-02-10 - Several enhancements + project update mode
# 2015-07-22 - first version
# ------------------------------------------------
######################################
# target
######################################
TARGET = 431test
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og
#######################################
# paths
#######################################
# Build path
BUILD_DIR = build
######################################
# source
######################################
# C sources
C_SOURCES = \
Src/main.c \
Src/gpio.c \
Src/stm32l4xx_it.c \
Src/stm32l4xx_hal_msp.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c_ex.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ex.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_gpio.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma_ex.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c \
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c \
Src/system_stm32l4xx.c
# ASM sources
ASM_SOURCES = \
startup_stm32l431xx.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-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 \
-DSTM32L431xx
# AS includes
AS_INCLUDES =
# C includes
C_INCLUDES = \
-IInc \
-IDrivers/STM32L4xx_HAL_Driver/Inc \
-IDrivers/STM32L4xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32L4xx/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 = STM32L431RBTx_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 ***
移植cmake
我首先安装他的makefile来移植出来的是这样: # CMAKE最小版本
cmake_minimum_required (VERSION 2.6)
# 设置工程名称
project (431test)
# 设置可执行文件名称
set(MY_TARGET 431test)
######################################
# 构建变量
######################################
# debug build?
set(DEBUG "1")
# optimization
set(OPT "-Og")
set(CMAKE_VERBOSE_MAKEFILE ON)
#######################################
# 设置编译器
#######################################
# 开启汇编
ENABLE_LANGUAGE(ASM C CXX)
# 设置目标平台系统
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
# 设置交叉编译器
set(CMAKE_C_COMPILER "/opt/gccStm32/bin/arm-none-eabi-gcc")
set(CMAKE_ASM_COMPILER "/opt/gccStm32/bin/arm-none-eabi-gcc")
set(CMAKE_CXX_COMPILER "/opt/gccStm32/bin/arm-none-eabi-g++")
SET(CMAKE_FIND_ROOT_PATH "/opt/gccStm32/")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
#######################################
# 设置文件目录
#######################################
#设置执行文件输出目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#设置库输出路径
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
message("++++++++++++++Start Build+++++++++++++++++")
# 添加头文件目录
include_directories(${PROJECT_SOURCE_DIR}/Inc)
include_directories(${PROJECT_SOURCE_DIR}/Drivers/STM32L4xx_HAL_Driver/Inc)
include_directories(${PROJECT_SOURCE_DIR}/Drivers/STM32L4xx_HAL_Driver/Inc/Legacy)
include_directories(${PROJECT_SOURCE_DIR}/Drivers/CMSIS/Device/ST/STM32L4xx/Include)
include_directories(${PROJECT_SOURCE_DIR}/Drivers/CMSIS/Include)
# 添加源文件目录
aux_source_directory(${PROJECT_SOURCE_DIR}/Src USRSRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/Drivers/STM32L4xx_HAL_Driver/Src HALSRC)
aux_source_directory(${PROJECT_SOURCE_DIR} ASMSRC)
#######################################
# 设置CFLAGS
#######################################
# cpu
set(CPU "-mcpu=cortex-m4")
# fpu
set(FPU "-mfpu=fpv4-sp-d16")
# float-abi
set(FLOAT-ABI "-mfloat-abi=hard")
# mcu
set(MCU "$(CPU) -mthumb $(FPU) $(FLOAT-ABI)")
ADD_DEFINITIONS(-DUSE_HAL_DRIVER -DSTM32L431xx)
set(CMAKE_C_EXTENSIONS "$(MCU) -fno-builtin $(OPT) -Wall -std=gnu99 -fdata-sections -ffunction-sections")
set(CMAKE_CXX_EXTENSIONS "$(MCU) -fno-builtin $(OPT) -Wall -std=c++11 -fdata-sections -ffunction-sections")
set(CMAKE_ASM_FLAGS "$(MCU) -x assembler-with-cpp")
#######################################
# 设置编译链接
#######################################
# 添加动态库
link_directories("/opt/gccStm32/")
set(LIBS "-lc -lm -lnosys" )
# 添加link script
set(LDSCRIPT "STM32L431RBTx_FLASH.ld")
# 这是LDFLAGS
set(CMAKE_EXE_LINKER_FLAGS "$(MCU) -specs=nano.specs -T$(LDSCRIPT) -Wl,--gc-sections")
set(CMAKE_MODULE_LINKER_FLAGS "$(MCU) " )
set(CMAKE_SHARED_LINKER_FLAGS "$(MCU) " )
# 添加可执行文件(可执行文件名 [配置] 源文件)
add_executable(${MY_TARGET} ${USRSRC} ${HALSRC} ${ASMSRC} )
# 执行文件链接属性
set_target_properties(${MY_TARGET} PROPERTIES LINK_DEPENDS ${LDSCRIPT})
target_link_libraries(${MY_TARGET} ${LIBS})
问题 1
我第一次尝试构建: ➜ 431test cd build
➜ build cmake ..
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The ASM compiler identification is GNU
-- Found assembler: /opt/gccStm32/bin/arm-none-eabi-gcc
++++++++++++++Start Build+++++++++++++++++
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zqh/Program/Stm32/431test/build
➜ build make
Scanning dependencies of target 431test
[ 4%] Building C object CMakeFiles/431test.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c.o
/tmp/ccqCkW1b.s: Assembler messages:
/tmp/ccqCkW1b.s:482: Error: selected processor does not support `dsb 0xF' in ARM mode
/tmp/ccqCkW1b.s:495: Error: selected processor does not support `dsb 0xF' in ARM mode
/tmp/ccqCkW1b.s:898: Error: selected processor does not support `dmb 0xF' in ARM mode
/tmp/ccqCkW1b.s:944: Error: selected processor does not support `dsb 0xF' in ARM mode
/tmp/ccqCkW1b.s:947: Error: selected processor does not support `isb 0xF' in ARM mode
CMakeFiles/431test.dir/build.make:206: recipe for target 'CMakeFiles/431test.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c.o' failed
make[2]: *** [CMakeFiles/431test.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/431test.dir/all' failed
make[1]: *** [CMakeFiles/431test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
可以看到编译出错在: Error: selected processor does not support `dsb 0xF' in ARM mode
-- The ASM compiler identification is GNU
-- Found assembler: /opt/gccStm32/bin/arm-none-eabi-gcc
通过观察原makefile,发现其汇编器的定义是:AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
说明这里的汇编器不是另一个可执行文件,而是arm-none-eabi-gcc
添加命令得到。所以需要对CMakeLists.txt
做一些修改。
问题1解决
发现在CMakeLists.txt
第90和91行 set(CMAKE_C_EXTENSIONS "$(MCU) -fno-builtin $(OPT) -Wall -std=gnu99 -fdata-sections -ffunction-sections")
set(CMAKE_CXX_EXTENSIONS "$(MCU) -fno-builtin $(OPT) -Wall -std=c++11 -fdata-sections -ffunction-sections")CMAKE_C_EXTENSIONS
改成CMAKE_C_FLAGS
。。。。。这个错误让我非常蛋疼啊
问题2
修改后运行编译还是出现问题: /opt/gccStm32/bin/arm-none-eabi-gcc -DSTM32L431xx -DUSE_HAL_DRIVER -I/home/zqh/Program/Stm32/431test/Inc -I/home/zqh/Program/Stm32/431test/Drivers/STM32L4xx_HAL_Driver/Inc -I/home/zqh/Program/Stm32/431test/Drivers/STM32L4xx_HAL_Driver/Inc/Legacy -I/home/zqh/Program/Stm32/431test/Drivers/CMSIS/Device/ST/STM32L4xx/Include -I/home/zqh/Program/Stm32/431test/Drivers/CMSIS/Include -fno-builtin -Wall -std=gnu99 -fdata-sections -ffunction-sections -o CMakeFiles/431test.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c.o -c /home/zqh/Program/Stm32/431test/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c
/tmp/ccxPxBMi.s: Assembler messages:
/tmp/ccxPxBMi.s:494: Error: selected processor does not support `dsb 0xF' in ARM mode
观察后发现是$(MCU)
这个变量没有起作用。原来是因为cmake
的变量和makefile
是不同的!要使用${MCU}
才可以!
问题3
终于编译到最后了,但是还是有一个问题 /opt/gccStm32/bin/arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fno-builtin -Og -Wall -std=gnu99 -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -specs=nano.specs -TSTM32L431RBTx_FLASH.ld -Wl,--gc-sections -rdynamic CMakeFiles/431test.elf.dir/Src/gpio.c.o CMakeFiles/431test.elf.dir/Src/main.c.o CMakeFiles/431test.elf.dir/Src/stm32l4xx_hal_msp.c.o CMakeFiles/431test.elf.dir/Src/stm32l4xx_it.c.o CMakeFiles/431test.elf.dir/Src/system_stm32l4xx.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma_ex.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ex.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_gpio.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c_ex.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c.o CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c.o -o ../bin/431test.elf
arm-none-eabi-gcc: error: unrecognized command line option '-rdynamic'
CMakeFiles/431test.elf.dir/build.make:617: recipe for target '../bin/431test.elf' failed
make[2]: *** [../bin/431test.elf] Error 1
make[2]: Leaving directory '/home/zqh/Program/Stm32/431test/build'
CMakeFiles/Makefile2:70: recipe for target 'CMakeFiles/431test.elf.dir/all' failed
make[1]: *** [CMakeFiles/431test.elf.dir/all] Error 2
make[1]: Leaving directory '/home/zqh/Program/Stm32/431test/build'
Makefile:86: recipe for target 'all' failed
make: *** [all] Error 2
他说没有这个命令-rdynamic
,但是我发现我没有输入这个命令啊.经过一番寻找,终于找到解决方案:
在CMakeLists.txt
之前加上: # 取消-rdynamic的错误
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
文件格式转换
这个时候我们就可以成功生成431test.elf
文件,但是我们要烧录到板子里面还是需要bin
文件或者hex
文件,所以需要格式转换
在CMakeLists.txt
最后添加如下 # 将elf文件转hex和bin
add_custom_command(
TARGET ${MY_TARGET}.elf #当 ${MY_TARGET}.elf被重新生成是执行以下命令
COMMAND ${CMAKE_OBJCOPY} -O ihex ${MY_TARGET}.elf ${MY_TARGET}.hex)
add_custom_command(
TARGET ${MY_TARGET}.elf
POST_BUILD COMMAND ${CMAKE_OBJCOPY} -O binary ${MY_TARGET}.elf ${MY_TARGET}.bin)
# 显示代码段大小
add_custom_command(
TARGET ${MY_TARGET}.elf #当 ${MY_TARGET}.elf被重新生成是执行以下命令
POST_BUILD COMMAND ${CMAKE_SIZE} ${MY_TARGET}.elf )
效果
进入我的工程中 ➜ 431test cd build
➜ build cmake ..
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/cc
++++++++++++++Start Build+++++++++++++++++
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zqh/Program/Stm32/431test/build
➜ build make
Scanning dependencies of target 431test.elf
[ 4%] Building C object CMakeFiles/431test.elf.dir/Src/gpio.c.o
[ 8%] Building C object CMakeFiles/431test.elf.dir/Src/main.c.o
[ 13%] Building C object CMakeFiles/431test.elf.dir/Src/stm32l4xx_hal_msp.c.o
[ 17%] Building C object CMakeFiles/431test.elf.dir/Src/stm32l4xx_it.c.o
[ 21%] Building C object CMakeFiles/431test.elf.dir/Src/system_stm32l4xx.c.o
[ 26%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c.o
[ 30%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c.o
[ 34%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c.o
[ 39%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma_ex.c.o
[ 43%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c.o
[ 47%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ex.c.o
[ 52%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c.o
[ 56%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_gpio.c.o
[ 60%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c.c.o
[ 65%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_i2c_ex.c.o
[ 69%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr.c.o
[ 73%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pwr_ex.c.o
[ 78%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc.c.o
[ 82%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c.o
[ 86%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c.o
[ 91%] Building C object CMakeFiles/431test.elf.dir/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim_ex.c.o
[ 95%] Building ASM object CMakeFiles/431test.elf.dir/startup_stm32l431xx.s.o
[100%] Linking C executable 431test.elf
text data bss dec hex filename
4584 24 1568 6176 1820 431test.elf
[100%] Built target 431test.elf
CMakeLists.txt
放出完整文档: # CMAKE最小版本
cmake_minimum_required (VERSION 2.6)
# 设置工程名称
project (431test)
# 设置可执行文件名称
set(MY_TARGET 431test)
######################################
# 构建变量
######################################
# debug build?
set(DEBUG "1")
# optimization
set(OPT "-Og")
# 开启详细输出
# set(CMAKE_VERBOSE_MAKEFILE ON)
#######################################
# 设置编译器
#######################################
# 开启汇编
ENABLE_LANGUAGE(ASM)
# 设置目标平台系统
set(CMAKE_SYSTEM Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
# 设置交叉编译器
# set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_C_COMPILER "/opt/gccStm32/bin/arm-none-eabi-gcc")
set(CMAKE_ASM_COMPILER "/opt/gccStm32/bin/arm-none-eabi-gcc")
set(CMAKE_CXX_COMPILER "/opt/gccStm32/bin/arm-none-eabi-g++")
set(CMAKE_OBJCOPY "/opt/gccStm32/bin/arm-none-eabi-objcopy")
set(CMAKE_OBJDUMP "/opt/gccStm32/bin/arm-none-eabi-objdump" )
set(CMAKE_SIZE "/opt/gccStm32/bin/arm-none-eabi-size" )
set(CMAKE_DEBUGER "/opt/gccStm32/bin/arm-none-eabi-gdb")
set(CMAKE_CPPFILT "/opt/gccStm32/bin/arm-none-eabi-c++filt" )
set(CMAKE_FIND_ROOT_PATH "/opt/gccStm32/")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
#######################################
# 设置文件目录
#######################################
# #设置执行文件输出目录
# set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# #设置库输出路径
# set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
message("++++++++++++++Start Build+++++++++++++++++")
# 添加头文件目录
include_directories(${PROJECT_SOURCE_DIR}/Inc)
include_directories(${PROJECT_SOURCE_DIR}/Drivers/STM32L4xx_HAL_Driver/Inc)
include_directories(${PROJECT_SOURCE_DIR}/Drivers/STM32L4xx_HAL_Driver/Inc/Legacy)
include_directories(${PROJECT_SOURCE_DIR}/Drivers/CMSIS/Device/ST/STM32L4xx/Include)
include_directories(${PROJECT_SOURCE_DIR}/Drivers/CMSIS/Include)
# 添加源文件目录
aux_source_directory(${PROJECT_SOURCE_DIR}/Src USRSRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/Drivers/STM32L4xx_HAL_Driver/Src HALSRC)
#######################################
# 设置CFLAGS
#######################################
# cpu
set(CPU "-mcpu=cortex-m4")
# fpu
set(FPU "-mfpu=fpv4-sp-d16")
# float-abi
set(FLOAT-ABI "-mfloat-abi=hard")
# mcu
set(MCU "${CPU} -mthumb ${FPU} ${FLOAT-ABI}")
ADD_DEFINITIONS(-DUSE_HAL_DRIVER -DSTM32L431xx)
set(CMAKE_C_FLAGS "${MCU} -fno-builtin ${OPT} -Wall -std=gnu99 -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS "${MCU} -fno-builtin ${OPT} -Wall -std=c++11 -fdata-sections -ffunction-sections")
set(CMAKE_ASM_FLAGS "${MCU} -x assembler-with-cpp")
#######################################
# 设置编译链接
#######################################
# 取消-rdynamic的错误
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# 添加动态库
link_directories("/opt/gccStm32/")
set(LIBS "-lc -lm -lnosys" )
# 添加link script
set(LDSCRIPT "${PROJECT_SOURCE_DIR}/STM32L431RBTx_FLASH.ld")
# 这是LDFLAGS
set(CMAKE_EXE_LINKER_FLAGS "-specs=nano.specs -T${LDSCRIPT} -Wl,-Map=${PROJECT_BINARY_DIR}/${MY_TARGET}.map,--cref -Wl,--gc-sections")
# 添加可执行文件(可执行文件名 [配置] 源文件)
add_executable(${MY_TARGET}.elf ${USRSRC} ${HALSRC} ${PROJECT_SOURCE_DIR}/startup_stm32l431xx.s)
# 执行文件链接属性
target_link_libraries(${MY_TARGET}.elf ${LIBS})
set_target_properties(${MY_TARGET}.elf PROPERTIES LINK_DEPENDS ${LDSCRIPT})
# 将elf文件转hex和bin
add_custom_command(
TARGET ${MY_TARGET}.elf
COMMAND ${CMAKE_OBJCOPY} -O ihex ${MY_TARGET}.elf ${MY_TARGET}.hex)
add_custom_command(
TARGET ${MY_TARGET}.elf
POST_BUILD COMMAND ${CMAKE_OBJCOPY} -O binary ${MY_TARGET}.elf ${MY_TARGET}.bin)
# 显示代码段大小
add_custom_command(
TARGET ${MY_TARGET}.elf
POST_BUILD COMMAND ${CMAKE_SIZE} ${MY_TARGET}.elf )