2025-11-21 11:07 AM - edited 2025-11-21 11:12 AM
Generate code from attached .ioc
Build with cmake:
cmake --preset Debug && cmake --build --preset Debug
Inspect NonSecure/build/build.ninja and we find
build CMakeFiles/myproject_NS.dir/AZURE_RTOS/App/app_azure_rtos.c.obj: C_COMPILE
R__myproject_NS_Debug ... || c
make_object_order_depends_target_myproject_NS
...
FLAGS = -mcpu=cortex-m33 -mfpu=fpv5-sp-d16 -mfloat-abi=hard -Wall -fdata-sect
ions -ffunction-sections -mcpu=cortex-m33 -mfpu=fpv5-sp-d16 -mfloat-abi=hard -Wall -fdata-sections -ffunction-sections -O0 -g3 -std=gnu11
Note that the flags set in gcc-arm-none-eabi.cmake
# MCU specific flags
set(TARGET_FLAGS "${STM32_MCU_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fdata-sections -ffunction-sections")
occurs twice!
The reason for this is that the toolchain file gcc-arm-none-eabi.cmake is evaluated multiple times. It is exactly the problem described at [3.31.6] Toolchain file gets parsed twice - Development - CMake Discourse but for an older CMake. And due to that the global variable is set multiple times each time amending to the existing value.
PROPOSED SOLUTION
Change the generated code to read
# MCU specific flags
set(TARGET_FLAGS "${STM32_MCU_FLAGS}")
set(CMAKE_C_FLAGS_INIT ${TARGET_FLAGS}")
set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} -Wall -fdata-sections -ffunction-sections")CMAKE_C_FLAGS_INIT is the proper variable to use here and will be combined with any CMAKE_C_FLAGS passed on the command line and CFLAGS from the environment to initialize CMAKE_C_FLAGS. By setting it instead of appending it, multiple evaluations of the toolchain files will still work. I believe this is how CMake toolchain files are intended to be setup.
Related problems:
Hopefully this can be fixed in a future version of STM32CubeMX. Please let me know if an internal ticket is created or if you want me to provide additional details.
ENVIRONMENT
STM32CubeMX 6.16.0 running on debian bookworm.
STM32CubeIDE 1.18.0 with /opt/st/stm32cubeide_1.18.0/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.13.3.rel1.linux64_1.0.0.202410170706/tools/bin in the PATH
cmake version 3.25.1
ninja 1.11.1
Best regards, Jesper