STM32CubeMX2 Generated Code Throws Error When Treating Warnings As Errors
After generating a CMAKE project using STM32CubeMX2 v1.1.1 followed by setting compiler options to “treat warnings as errors” (-Werror) and enable strict standards-conformance (-Wpedantic) in the root CMakeLists.txt file, I am errors in, mx_def.h.
Though this post shows the workaround:
- What is the recommended C Standard that STM32C5 project should follow?
- If the generated code stated “gnu11” but yet uses “gnu23”, shouldn’t the generator use the latter instead (see solution)?
- And, what are the dangers of doing so?
Error Message:
error: ISO C restricts enumerator values to range of 'int' before C23
Full Error message:
[build] C:/PathTo/ProjectName/source/App/generated/hal/mx_def.h:32:38: error: ISO C restricts enumerator values to range of 'int' before C23 [-Werror=pedantic]
[build] 32 | SYSTEM_OK = 0xEAEAEAEAU, /* System initialization successfully */
[build] | ^~~~~~~~~~~
[build] C:/PathTo/ProjectName/source/App/generated/hal/mx_def.h:33:38: error: ISO C restricts enumerator values to range of 'int' before C23 [-Werror=pedantic]
[build] 33 | SYSTEM_PRESYSTEM_ERROR = 0xF5F5F5F5U, /* Error during System pre-initialization */
[build] |
Screen Shot:

Reproduce Issue
To recreate this error:
- Open your generated project’s root “CMakeLists.txt” (generated by CubeMX2)
- Add the following to the
target_compile_options(..)section: - Clean and Compile Project
- The above errors will appear
Path: (root)/CMakeLists.txt
target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC
# Add additional compiler flags here
# Warnings for C code:
-Wpedantic # Enable strict standards-conformance warnings (cleaner portable C code).
-Wno-strict-prototypes # Ignore pre-ANSI C style of '(void)'
-Werror # Treats all warnings as errors
)
The Cause of the issue
The generated mx_def.h enum, system_status_t uses values too large for C11 standard (i.e. greater than signed int), however they are allowed in C23 and GNU23 C Standard.
The Solution
- Open your generated file, “cmake/flags.cmake”
- Locate the “
target_compile_options(“ that defines, “-std=gnu11” - Update this value to “
-std=gnu23” - Clean project and recompile.
Path: cmake/flags.cmake
# file-format: 1.0.0
if(CMAKE_BUILD_TYPE STREQUAL "debug_GCC_STM32C562RET6")
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES SUFFIX ".elf")
target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC ${CPU_FLAGS})
target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC "SHELL:-g3 -O0 -fdata-sections -ffunction-sections -std=gnu11 -Wall -fstack-usage --specs=nano.specs --specs=nosys.specs -Werror=implicit-function-declaration ${CC_SECURE}")
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
Recommendation
The generated project’s Target Compiler Option “-std” specification should match the code that is generated.
