Many beginners, when they want to introduce some test changes in their project, don’t always use Git. Sometimes there’s also a real need to clone a project in order to develop it completely separately. First thought? Copy-Paste. It’s not a bad solution, but when using CubeIDE we’ll discover that there’s quite a big problem with it.
Update
Since STM32CubeIDE version 1.7.0, the problem stopped occurring. I’m leaving the article anyway, because it might come back somewhere someday 🙂
My story with the “problem”
I’m currently working on my biggest project, which is the STM32 Course for Beginners (the course content is in Polish). For its needs I’m forced to split appropriate pieces of code into separate projects. All so that the lessons are relatively separated from each other and the code from the project fits them.
For example, while writing UART using Polling, I declared some variables, etc. In general, there is something in this project. Now I’d like to use this base for subsequent lessons, for example one about reception with interrupts.
However, I don’t want to touch this base, because I will share it as code for the earlier lesson. Idea – a copy of the project. Right mouse button -> Copy -> Paste next to it.
You change the folder name. You change the name of the *.ioc file for Cube. Besides, Cube itself will start yelling that the folder and *.ioc must be named identically to the project folder for it to work.
Everything is beautiful, you write code in the new project and everyone is happy. The devil is happiest because when that moment comes when you want to upload the base project for the course participants, it turns out that THERE ARE NO SOURCE FILES IN THE Src FOLDER. ALL OF THEM!
“Shock, despair, disbelief. Some remnant of hope”. This quote from a well-known politician comes to mind. For some, like me, even a few harsher words. The base project evaporated. There’s nothing. What now? Unfortunately, if there was no copy in an external repository – everything is gone. You have to recreate it.
Where is the problem?
I googled, I read, but I didn’t find a correct answer. Some say it’s about the project settings file in the IDE – .project. So the problem should occur in every Eclipse-based environment. However, I don’t remember ever having such problems with AVRs… I copied, moved and modified AVR projects without anything disappearing.
Unfortunately, the problem lies in CubeMX and its file operations. Specifically, in which paths within the project it refers to. Steps to reproduce the disappearing files:
- You create the Source Project.
- You generate files in it via CubeMX
- You go into the projects folder.
- You copy the Source Project
- You paste the copy
- You change the name of the Copy Project
- You change the name of the Copy Project’s *.ioc
- You launch the Copy Project’s *.ioc
- You make changes if you want
- You regenerate the Copy Project
- Src files in the Source Project disappear
Why?
Each CubeMX project has a special .mxproject file in its directory. It contains information about which files CubeMX uses to generate the project.
And the most important piece of information – which files were generated previously. This is intended to select all files that should be regenerated. From my observations it follows that the “previous” files are deleted entirely.
Only fragments inside USER CODE tags are taken from them and copied into completely new files. Cube doesn’t modify existing files. It deletes them wholesale and only переносi user code.
After copying “Projekt_Zrodlo” to “Projekt_Kopia” I opened the .mxproject file. Inside it looks like this.
[PreviousLibFiles] LibFiles=Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_uart.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_exti.h;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_uart.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_exti.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f411xe.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/tz_context.h; [PreviousUsedCubeIDEFiles] SourceFiles=Core\Src\main.c;Core\Src\stm32f4xx_it.c;Core\Src\stm32f4xx_hal_msp.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Core\Src/system_stm32f4xx.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Core\Src/system_stm32f4xx.c;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;; HeaderPath=Drivers\STM32F4xx_HAL_Driver\Inc;Drivers\STM32F4xx_HAL_Driver\Inc\Legacy;Drivers\CMSIS\Device\ST\STM32F4xx\Include;Drivers\CMSIS\Include;Core\Inc; CDefines=USE_HAL_DRIVER;STM32F411xE;USE_HAL_DRIVER;USE_HAL_DRIVER; [PreviousGenFiles] AdvancedFolderStructure=true HeaderFileListSize=3 HeaderFiles#0=D:/LIVE/Workspace/Projekt_Zrodlo/Core/Inc/stm32f4xx_it.h HeaderFiles#1=D:/LIVE/Workspace/Projekt_Zrodlo/Core/Inc/stm32f4xx_hal_conf.h HeaderFiles#2=D:/LIVE/Workspace/Projekt_Zrodlo/Core/Inc/main.h HeaderFolderListSize=1 HeaderPath#0=D:/LIVE/Workspace/Projekt_Zrodlo/Core/Inc HeaderFiles=; SourceFileListSize=3 SourceFiles#0=D:/LIVE/Workspace/Projekt_Zrodlo/Core/Src/stm32f4xx_it.c SourceFiles#1=D:/LIVE/Workspace/Projekt_Zrodlo/Core/Src/stm32f4xx_hal_msp.c SourceFiles#2=D:/LIVE/Workspace/Projekt_Zrodlo/Core/Src/main.c SourceFolderListSize=1 SourcePath#0=D:/LIVE/Workspace/Projekt_Zrodlo/Core/Src SourceFiles=;
Pay attention to the [PreviousGenFiles] section. There are paths to the source project. And they are ABSOLUTE paths. These files are deleted when the project is regenerated. If so, then you can replace the path—or basically just the project name—with the new one, right?
After replacing it, the [PreviousGenFiles] section looks like this.
[PreviousGenFiles] AdvancedFolderStructure=true HeaderFileListSize=3 HeaderFiles#0=D:/LIVE/Workspace/Projekt_Kopia/Core/Inc/stm32f4xx_it.h HeaderFiles#1=D:/LIVE/Workspace/Projekt_Kopia/Core/Inc/stm32f4xx_hal_conf.h HeaderFiles#2=D:/LIVE/Workspace/Projekt_Kopia/Core/Inc/main.h HeaderFolderListSize=1 HeaderPath#0=D:/LIVE/Workspace/Projekt_Kopia/Core/Inc HeaderFiles=; SourceFileListSize=3 SourceFiles#0=D:/LIVE/Workspace/Projekt_Kopia/Core/Src/stm32f4xx_it.c SourceFiles#1=D:/LIVE/Workspace/Projekt_Kopia/Core/Src/stm32f4xx_hal_msp.c SourceFiles#2=D:/LIVE/Workspace/Projekt_Kopia/Core/Src/main.c SourceFolderListSize=1 SourcePath#0=D:/LIVE/Workspace/Projekt_Kopia/Core/Src SourceFiles=;
Trying to regenerate the copy project no longer destroys the source project!
Success!
Any FIX for this, Mr. ST?
The fix would be trivially simple. It would be enough to replace absolute paths with relative paths in the .mxproject file. However, ST itself would have to do this in its tool.
What’s interesting is that in the earlier sections of the .mxproject file there are relative paths. I’m curious what guided ST when doing something like this…
Because it’s quite easy to discover such a surprise within the same Workspace. Worse if we move the project completely somewhere else. Then there’s an armed bomb lurking, just waiting to delete files under the absolute path 🙂
Summary
The problem exists and it’s quite serious. It cost me a few extra hours of work on the course. The course is half the problem. Worse if some big commercial projects were deleted.
One thing is certain – backups, backups, backups. You need to make backups outside of your computer. Git with a cloud repository will be suitable for that. Then it’s easy to simply roll back such a surprise.
It would be nice if ST fixed this problem. Who knows, maybe my post will reach them. I’d be happy 🙂
For now, remember what you need to do to stay safe project-wise.
If you liked the article, buy something from me! https://sklep.msalamon.pl/
If you noticed any mistake, disagree with something, would like to add something important, or simply feel like you’d like to discuss this topic, write a comment. Remember that the discussion should be polite and in accordance with the rules of the Polish language.

0 Comments