cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE creates main.c but I am using main.cpp!

MAnto.2
Associate III

I converted project from C to C++ with right click on project-> Convert to C++. The problem occurs when I generate the code in graphic configurator . It creates main.c file. Which is also loaded to controller if I run it.

0693W00000GZecYQAT.png 

How to solve this problem?

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

CubeMX always generates main.c. You'll need to rename it every time you regenerate if you want it to be main.cpp.

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

View solution in original post

3 REPLIES 3
TDK
Guru

CubeMX always generates main.c. You'll need to rename it every time you regenerate if you want it to be main.cpp.

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

You can do that, or you can call a bridge program that can be called by main.c; which then calls a .cpp program. With the bridge program having a constant name. you're free to use main.c and change your programs as needed.

RCham.1
Associate

As part of experiments with an STM32F3DISCOVERY board I ran into the same problem.

In an intro article I wrote, I mentioned this problem and my work around, Beginning with the ST32F3DISCOVERY Microcontroller Board from STMicroelectronics - CodeProject. See the sub-section "Source Code" in the section "Using the Code" which provides the details of the changes.

What I did was to create a new file, mymain.cpp, which contained the entry point for my application, `mymain()`, and then modified the `main() `function in main.c to call that entry point, `mymain()`. The source code file mymain.cpp has an include directive to include the header file main.h so that any changes the IDE may make to main.h will be available in mymain.cpp.

The IDE can then modify the source in main.c and main.h for any initialization and setup and I can recompile without having to do anything further to the source in main.c or at least that's the idea and it seems to work.

The `main()` in main.c has the `while()` loop in the USER CODE areas modified as follows:

/* Infinite loop */

 /* USER CODE BEGIN WHILE */

  // this is the entry point to my C++ source code files.

  // the STM32CubeIDE does not seem to be able to generate and update

  // C++ main.cpp files for some reason.

  // the work around is to have another C++ source file that contains the

  // real entry point which this generated main.c source file will call after

  // completing initialization.

  extern int mymain(void);

// while (1) comment out the while() because the main loop is in mymain() in mymain.cpp

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 // the main entry point in the C++ source after initializing

 // the runtime environment.

 return mymain();

 /* USER CODE END 3 */