Skip to main content
dodonny
Associate III
January 12, 2021
Question

There is a plan to allow STM32cubeMX for generating main.cpp instead of .c only ?

  • January 12, 2021
  • 9 replies
  • 9326 views

Hi, I know this question have been asked several time, but STM32CubeIDE and STM32CubeMx are only generating .c file... this is very frustriating, time consuming and source of multiples error when working with cpp environment. We have to manually rename .c to .cpp file and vice et versa after each modification... this is so 1990 workflow.

Does this implementation have been planned in the future or shall I forget about it ?

Thanks for your reply.

9 replies

dodonny
dodonnyAuthor
Associate III
January 13, 2021

So painfull to lose your earlier 4 hours work because you have to rename main.c to main.cpp but forget to add it to your commit, because git see it as suppressed file... So unproductive, so unprofessional...

EDIT :

git mv <source.c> <destination.cpp>

could be small workaround for git issues

Cartu38 OpenDev
Graduate II
January 15, 2021

Many other thread on such topic. My opinion is such is not supported but not so much a pain. C++ projects are fully supported by IDE and obviously all STM32CubeMX / device configuration tool is generating C++ compliant material.

Just to be aware of then you have to organize your project in sync. ... you just have to take your C++ material out of main.c and that's all. According to me such is not a constraint but kind of good pratice. Main.c file is updated by tool ... I'm always in flavor to add my own material out of automated part ...

dodonny
dodonnyAuthor
Associate III
January 15, 2021

But you can't compile project as C++ with a main.c ? Because it's the entry point of the software and because I cannot import C++ file inside a C file (but can do the opposite).

dodonny
dodonnyAuthor
Associate III
January 15, 2021

Yes, this is the best option for solid big project. But could be very annoying for small speed test.

The most frustrating thing is that only need to generate main.cpp file instead of .c file. Just file naming...

I'll explore the : Do not generate main() option. Maybe use another IDE, eclipse only with STMcube beside and 1 or 2 openOBD script. But losing debugging in IDE is very annoying.

Cartu38 OpenDev
Graduate II
January 15, 2021

Even quick & dirty test may go quite fast no ?.

main.c file:

/* USER CODE BEGIN Includes */

#include "Process.h"

/* USER CODE END Includes */

int main(void)

{

...

 /* USER CODE BEGIN 'x' */

doMyCppProcess();

 /* USER CODE END 'x' */

...

}

then Process.h

#ifdef __cplusplus

 #define EXTERNC extern "C"

 #else

 #define EXTERNC

 #endif

EXTERNC void doMyCppProcess(void);

 then Process.cpp

#include "Process.h"

extern "C" {

  #include "Initializer.h"

}

void doMyCppProcess(void) {

....

}

dodonny
dodonnyAuthor
Associate III
January 18, 2021

Are you opposing 5/10 mn OR conditional programming inside CubeMX (and IDE) saving hundred of hours for worldwide developer to complex, unreadable macro definition.... ?

From my point of view, you have two options : Let people chose name and extension of the generated file... this way everybody could do his own kitchen...

or, pseudo code :

IF main.c file exist THEN 
write into it as we alwready do
ELSE IF main.cpp exist THEN 
write into it as we alwready do
ELSE 
throw error
END IF

Piranha
Principal III
January 16, 2021
Tesla DeLorean
Guru
January 16, 2021

They were peddling that snake-oil back in the late 1980's.. I'm still waiting for my "kitchen of the future"

If you have to go through a process more than once, or expect too, automate it, write a script or small app.

The CubeMX tools don't do iterative fiddling very well, they really expect you to define the end goal once, and not keep pounding on the button. Keep your main branch separate from your auto-generated sandbox, and merge what you need, or script the process.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
dodonny
dodonnyAuthor
Associate III
January 18, 2021

My best workaround for the moment in eclipse STM32CubeIDE :

1) create main.cpp and main.hpp file right next to main.c and main.h (it's ugly, I know it)

1.1) don't forget to change your include "main.h" to "main.hpp"

2) exclude main.c from the compilation (right click on it > resource config > Exclude from build > check debug and release) > OK )

3) for any change made to you cubeMX config and applied to the file main.c (and main.h) you can use the compare function in eclipse to maintain the two files consistent

4 ) Select main.h and main.hpp for instance, and right clic on them > Compare with > Each other.

doddonny
Associate
February 16, 2021

flag

jose luu
Associate III
January 27, 2024

Workaround starting with CubeMX 6.9.0 in Project Manager -> Code generator -> After code generation

 

 

Specify a .bat script located in Core/Src containing:

 

 

set SCRIPT_DIR=%~dp0%
cd %SCRIPT_DIR%
copy main.c main.cpp
del main.c

 

 

In -> Before code generation you also need the converse script, otherwise CubeMX will regenerate from scratch and will not take into account the modifications you may have done

 

set SCRIPT_DIR=%~dp0%
cd %SCRIPT_DIR%
copy main.cpp main.c
del main.cpp

 

 

As of 6.9.2 the script path as to be absolute (eg. C:\Users\...)

 

Associate III
August 14, 2024

Is there a workaround for CubeIDE ?

Where can I insert the renaming scripts in the CubeIDE ?

Associate II
September 24, 2024

This is my workaround and It's working fine.

Lets assume you have already renamed original main.c / main.h into main.cpp / main.hpp files.

1) in shell go to directory where your main.cpp is located

2) (on windows) mklink main.c main.cpp (you have to be admin)
    (on linux) ln -s main.cpp main.c

3) goto main.hpp directory

4) (on windows) mklink main.h main.hpp (you have to be admin)
    (on linux) ln -s main.hpp main.h

5) in STMCubeIDE after refreshing the project find main.c file and:
Right click on it > Resource config > Exclude from build > check debug and release) > OK

So now the compiler still works with fine as C++ project, and in Device Configuration Tool which uses only main.c/main.h files, it sees and modifies the original main.cpp/main.hpp file trough symlinks.

Visitor II
May 23, 2025

i have the same issue and did a small python script (on linux) that i am using to copy changes from main.c to main.cpp, didn't come up with anything smarter, if you want you can check it out here , maybe it will solve the issue.

but please be careful and check the changes anyway