Skip to main content
Associate II
Aug 27, 2024 3:35 AM UTC
Solved

STM32CubeMx Code generation duplicating lines in main.c

Hi. I am using NUCLE-F439ZI board

STM32CubeIDE Version: 1.16.0
Build: 21983_20240628_1741 (UTC)

Lately I am experiencing problem, when I make changes in .IOC file and save and generate code

it is messing my main.c file. It is copying big portion of main.c lines and adding them again in main.c file.

It is impossible to continue working. Is this a setting problem or a bug in the software?

Please help.

Thank you.

Rasem

This topic has been closed for replies.
TDK
TDKBest answer
Sep 3, 2024 2:14 AM UTC

This really shouldn't be this difficult.

> I am attaching good main.c which builds without errors.

The main.c file you have attached contains many "/* USER CODE" texts within USER CODE blocks.

TDK_0-1725329258903.png

It also includes several duplicate definitions, including two of main() where the first one is unfinished. It definitely does not compile.

TDK_1-1725329579039.png

Perhaps edit your file so "USER CODE" is not used anywhere within the user code sections.

When I edit your file to correct the above errors, it compiles. If I edit the IOC and regenerate code, it does not duplicate sections.

"If you feel a post has answered your question, please click ""Accept as Solution""."

11 replies

TDK
Aug 27, 2024 12:44 PM UTC

Are you modifying the USER CODE lines? What lines is it copying/adding? Your code should only be put within a USER CODE block.

Include your main.c file here.

"If you feel a post has answered your question, please click ""Accept as Solution""."
mƎALLEm
ST Technical Moderator
Aug 27, 2024 1:58 PM UTC

Hello,

Are you putting your custom code between:

/* USER CODE BEGIN XXX */

<Your custom code>

/* USER CODE END XXX  */

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
rasemAuthor
Associate II
Aug 27, 2024 4:12 PM UTC

HI SofLit,

Thanks for the reply.

Yes, all codes are between the user specified locations. The problem happens when I copy and paste some code from

another project, and then I make changes in .IOC file, Save  and generate code.

Now, I am trying now to Build the project every time I paste a new code, and before I make changes in .IOC and before

Generating code. It seems to be working without problem.

This is my assumption of the problem, I hope it will solve future problems.

Rasem

 

mƎALLEm
ST Technical Moderator
Aug 27, 2024 4:18 PM UTC

Could you please describe the exact operation you are doing?

Could you please share the exact code you are copying/pasting in the generated code and where? Is the code you are copying contains /* USER CODE BEGIN XXX */ and /* USER CODE END XXX  */ ?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.