简介
在使用STM32CubeIDE开发时,如果需要对最终编译出来的elf文件做二次处理,比如删除部分section,根据配置文件(假设为config.h),对最终的hex文件命名等,则需要添加一个post-build脚本来最终生成的处理elf文件(或hex文件)。
post-build脚本
设置post-build脚本路径
工程文件–> 右键 --> Properties --> C/C++ Build --> Settings --> 选择 Configuration(Debug or Release) --> Build Steps --> Post-build steps --> Command:
运行post-build脚本的指令
假设post-build脚本的名字为post-buils.bat, 位于工程文件的根目录中,即 project_file/post-buils.bat。
则上一步的Command: 应该输入的指令为:
$(abspath $(dir $(dir $(dir $(CURDIR)))))/post_build.bat
即:切换到工程的根目录,运行post_build.bat。
编写post-build.bat
假设当前的stm32CubeIDE 工程包含 TouchGFX工程,现在需要从elf文件中移除TouchGFX相关的section, 生成新的hex文件。同时新的hex文件的名称根据 config.h文件里的宏定义来定义,包括项目名称,版本号信息。
在编写post-buils.bat之前,先确定需要脚本完成的工作:
- 裁剪CubeIDE生成的elf文件,即删除TouchGFX section
- 读取 config.h里的设置
- 生成最终的hex文件
下面是config.h文件的内容:
/* defines of all variants */
#define PROJECT_1 1
#define PROJECT_2 0
#if PROJECT_1
#define PRG_VERSION "02"
#define PRG_REVISION "02"
#define PRG_BETA "00&#