Skip to main content
brucegong
Associate III
December 30, 2025
Question

vscode + STM32CubeIDE, debug, but 'optimized out'

  • December 30, 2025
  • 1 reply
  • 837 views

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

# Define the build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
endif()
 

截图 2025-12-30 08-27-35.png截图 2025-12-30 08-40-08.png

1 reply

KnarfB
Super User
December 30, 2025

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

 

brucegong
brucegongAuthor
Associate III
December 30, 2025
Thanks ,change starm-clang.cmake as following would be OK
 
#set(CMAKE_C_FLAGS_DEBUG "-Og -g3")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
set(CMAKE_C_FLAGS_RELEASE "-Oz -g0")
#set(CMAKE_CXX_FLAGS_DEBUG "-Og -g3")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3")
set(CMAKE_CXX_FLAGS_RELEASE "-Oz -g0")