2025-12-29 4:50 PM - last edited on 2025-12-30 7:19 AM by Andrew Neil
When creating a project in STM32CubeMX, ST ARM Clang was selected. Compilation and debugging in VSCode all work, but some variables are optimized out, which seems like the compiler optimization is set too high. However, it's unclear where to set the compile flag -O0.
CMakeLists.txt like this, does not work
2025-12-30 12:09 AM
The if condition is not true if you build inside VS Code.
The _CXX_ defines are for C++ sources, for C sources, use _C_ instead or both for mixed projects.
Try the following block instead:
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0")
message(STATUS "CMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}")
endif()If everything else fails, search in all files (Ctrl+Shift+F) for the original definitions and change them to your needs.
hth
KnarfB
2025-12-30 7:16 AM