STM32G4CETx generated CMAKE misuses CMAKE_C_FLAGS for optimization
- November 1, 2024
- 1 reply
- 1428 views
Hello,
I recently noticed an issue with the CMake project generated by CubeMX targeting the STM32G4CETx MCU.
The generated gcc-arm-none-eabi.cmake sets the optimization flags using `CMAKE_C_FLAGS`, which are then overwritten by the default optimization flags used by CMake.
I've attached the compile_commands.json (though I've had to rename it to `.json.txt`) from a Release build, which is generated by CMake by using `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`. As you can see, the optimization flag `-Os` is immediately overwritten by `-O3`, which is the default for CMake.
It appears that projects for the STM32H7 do not have this issue, as they correctly use CMAKE_<LANG>_FLAGS_<CONFIG> (https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_CONFIG.html#variable:CMAKE_%3CLANG%3E_FLAGS_%3CCONFIG%3E)
Also, as a side note, it appears that -Og should be used for debug builds, as that is preferred for the typical debug experience (per https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)
