STM32 Makefile的一次bug解决过程
背景
迁移华为的liteOS到STM32F4的开发板上,按照官方的步骤修改makefile后报错:
arm-none-eabi-gcc.exe: warning: '-x assembler-with-cpp' after last input file has no effect
arm-none-eabi-gcc.exe: fatal error: no input files
compilation terminated.
解决过程
根据Makefile的报错地点可以看出是在对.S文件的编译过程中找不到文件所致,将结果打印出来:
arm-none-eabi-gcc -x assembler-with-cpp -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_STDPERIPH_DRIVER -DSTM32F407xx -DSTM32F40_41xxx -I./Inc -IProjectDrivers/Inc -IProjectDrivers/Inc/Inc -IProjectDrivers/Inc/CORE -IDrivers/CMSIS/include -I./Src/SYSTEM -I./Src/MALLOC -I./ProjectDrivers/HARDWARE -IThirdParties/LWIP -I./ThirdParties/LWIP -I./ThirdParties/LWIP/arch -I./ThirdParties/LWIP/lwip_app -I./ThirdParties/LWIP/lwip_app/lwip_comm -I./ThirdParties/LWIP/lwip_app/tcp_server_demo -I./ThirdParties/LWIP/lwip_app/udp_demo -I./ThirdParties/LWIP/lwip-1.4.1 -I./ThirdParties/LWIP/lwip-1.4.1/src -I./ThirdParties/LWIP/lwip-1.4.1/src/api -I./ThirdParties/LWIP/lwip-1.4.1/src/core -I./ThirdParties/LWIP/lwip-1.4.1/src/core/ipv4 -I./ThirdParties/LWIP/lwip-1.4.1/src/include -I./ThirdParties/LWIP/lwip-1.4.1/src/include/ipv4 -I./ThirdParties/LWIP/lwip-1.4.1/src/include/ipv4/lwip -I./ThirdParties/LWIP/lwip-1.4.1/src/include/lwip -I./ThirdParties/LWIP/lwip-1.4.1/src/include/netif -I./ThirdParties/LWIP/lwip-1.4.1/src/netif -I./utils/datastructure -I./utils/tools -I ./Middlewares/LiteOS/arch/arm/arm-m/include -I ./Middlewares/LiteOS/arch/arm/common/cmsis -I./OS_CONFIG -I ./Middlewares/LiteOS/kernel/base/include -I ./Middlewares/LiteOS/kernel/extended/include -I ./Middlewares/LiteOS/kernel/include -O1 -Wall -Wno-pointer-sign -Wno-missing-braces -Wno-format -Wno-address -Wno-unused-but-set-variable -s -fdata-sections -ffunction-sections -g -gdwarf-2 --specs=nano.specs --specs=nosys.specs -MMD -MP -MF"Output/obj/los_dispatch_gcc.d"./Middlewares/LiteOS/arch/arm/arm-m/cortex-m4/gcc/los_dispatch_gcc.S -o Output/obj/los_dispatch_gcc.o
注意这里-MF"Output/obj/los_dispatch_gcc.d"./Middlewares/LiteOS/arch/arm/arm-m/cortex-m4/gcc/los_dispatch_gcc.S
,".d"后面紧跟着./Middlewares,这说明本该分开的两个参数被合并到一起了,查看Makefile:
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@
@echo $(notdir $(<:.s=.o))
$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS)$< -o $@
@echo $(notdir $(<:.s=.o))
第二个CFLAGS后面少了个空格,修改后如下:
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@
@echo $(notdir $(<:.s=.o))
$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@
@echo $(notdir $(<:.s=.o))
重新编译:
$ make
main.o
linking...
arm-none-eabi-size Output/obj/out.elf
text data bss dec hex filename
31876 308 3456 35640 8b38 Output/obj/out.elf
# rm -fR Output/obj/Output/obj
# mkdir Output/obj/Output/obj
arm-none-eabi-objcopy -O ihex Output/obj/out.elf Output/obj/out.hex
arm-none-eabi-objcopy -O binary -S Output/obj/out.elf Output/obj/out.bin
cp Output/obj/*.bin Output/bin/
cp Output/obj/*.elf Output/bin/
cp Output/obj/*.hex Output/bin/
cp Output/obj/*.map Output/bin/
bug解决
总结
工程移植一定要小心,不要随意调整Makefile的内容,做好记录和对比,这样有助于解决问题,这次尽管只是一个空格,但是引起的错误却很难直接发现,而如果移植的时候小心些则完全可以避免。