2025-11-18 9:14 AM
STM32CubeMX supports CMake since 6.11.0 as a tool chain option
It generates:
You can add your own sources and settings in ./CMakeLists.txt. But sometimes you want to change the way stm32cubeMX generated code is compiled:
I will reply how to do these things and this will be the accepted answer. It's like a knowledge base article. Please expand it with more ideas and I will add it to that answer.
Solved! Go to Solution.
2025-11-18 9:14 AM - edited 2025-11-18 9:30 AM
The generated CMakeLists.txt files are interface libraries and not compiled libraries. They are basically a list of source files and compile definitions. When they are included in your ./CMakeLists.txt this information can be modified without modifying those generated files. This is what you want, because you don't want to constantly have to manually change (re)generated files.
Add the following to your ./CMakeLists.txt file:
# modifications to stm32cubemx interface library without modifying its CMakeLists.txt file:
# add a define to stm32cubemx interface libary:
target_compile_definitions(stm32cubemx INTERFACE
HTTPD_FSDATA_FILE="../httpd/fsdata.c"
)
# Add extra include directories to stm32cubemx interface libary:
target_include_directories(stm32cubemx INTERFACE
httpd
Communication
Config
Common
Process
Display
)
# Remove sources from stm32cubemx interface libary:
get_target_property(APP_SOURCES ${CMAKE_PROJECT_NAME} SOURCES)
#message(WARNING ${APP_SOURCES})
list(REMOVE_ITEM APP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32cubemx/../../USB_HOST/App/usb_host.c
${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32cubemx/../../TouchGFX/target/generated/OSWrappers.cpp
)
#message(WARNING ${APP_SOURCES})
set_property(TARGET ${CMAKE_PROJECT_NAME} PROPERTY SOURCES "${APP_SOURCES}")
Additionally you can generate a dependency graph of your CMake project:
# add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} "--graphviz=cmake_dependency_graph.dot" .
COMMAND dot -Tpng cmake_dependency_graph.dot -o cmake_dependency_graph.png
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)This will generate something like:
With me it works, but it requires a successful build first. There is some type of racing condition. So I need to comment it out first and then I can uncomment it again.
My TouchGFX project uses external flash. In order to debug without always flashing the entire external flash you can create a seperate elf file that has those sections removed. You can use the following command for that:
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}_NO_OFLASH.elf --remove-section=.textoctoflash --remove-section=ExtFlashSection
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)This saves a lot of time when debugging.
2025-11-18 9:14 AM - edited 2025-11-18 9:30 AM
The generated CMakeLists.txt files are interface libraries and not compiled libraries. They are basically a list of source files and compile definitions. When they are included in your ./CMakeLists.txt this information can be modified without modifying those generated files. This is what you want, because you don't want to constantly have to manually change (re)generated files.
Add the following to your ./CMakeLists.txt file:
# modifications to stm32cubemx interface library without modifying its CMakeLists.txt file:
# add a define to stm32cubemx interface libary:
target_compile_definitions(stm32cubemx INTERFACE
HTTPD_FSDATA_FILE="../httpd/fsdata.c"
)
# Add extra include directories to stm32cubemx interface libary:
target_include_directories(stm32cubemx INTERFACE
httpd
Communication
Config
Common
Process
Display
)
# Remove sources from stm32cubemx interface libary:
get_target_property(APP_SOURCES ${CMAKE_PROJECT_NAME} SOURCES)
#message(WARNING ${APP_SOURCES})
list(REMOVE_ITEM APP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32cubemx/../../USB_HOST/App/usb_host.c
${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32cubemx/../../TouchGFX/target/generated/OSWrappers.cpp
)
#message(WARNING ${APP_SOURCES})
set_property(TARGET ${CMAKE_PROJECT_NAME} PROPERTY SOURCES "${APP_SOURCES}")
Additionally you can generate a dependency graph of your CMake project:
# add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} "--graphviz=cmake_dependency_graph.dot" .
COMMAND dot -Tpng cmake_dependency_graph.dot -o cmake_dependency_graph.png
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)This will generate something like:
With me it works, but it requires a successful build first. There is some type of racing condition. So I need to comment it out first and then I can uncomment it again.
My TouchGFX project uses external flash. In order to debug without always flashing the entire external flash you can create a seperate elf file that has those sections removed. You can use the following command for that:
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}_NO_OFLASH.elf --remove-section=.textoctoflash --remove-section=ExtFlashSection
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)This saves a lot of time when debugging.