cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure STM32CubeIDE to support C++ development?

TLeaf
Associate II

1. How to configure the IDE to use g++ compiler to compile all the files includes "*.c" files in the project?

Current it compiles the .c files by using gcc and .cpp by using g++ ...

2. If I rename main.c to main.cpp, the cube code generator will create a new main.c file instead of using main.cpp. Any sulotions for this?

Becasue main.c is .c file, so the IDE use gcc to compile this file, and this caused that I can't use any objects in it. But if I changed the main.c to main.cpp, the cube can't generate code into it...:face_screaming_in_fear:

55 REPLIES 55

You have two infinite while loop in your code. 

Hi. Yes, that's correct and that hurts nobody.

For my example all the action takes place in the MainApp()'s infinite loop, and it's very unlikely that you abandon it. In the remote case it happens, then the code is safely contained in the main()'s infinite loop.

 

What we want is to code in C++, not in C. The MainApp() function is inside the MainApp.cpp file; it will be compiled with the g++ compiler so you can use all your C++ knowledge in your project.

 

Greetings!

Yes, you're right.

Could you also elaborate a little more on "in the remote case" please? 

As well as, what instructions do you follow to use a C++ class in an embedded project?
Suppose it's a simple project, like blinking the LED. 

 

 

The loop in main.c can be easily removed:

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  //while (1)
  {
    run();
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

I generally use a FreeRTOS environment.  YMMV.

Since the underlying structure of a CubeMX application is C, you have to make some exceptions, especially when dealing with interrupts (and the DMA structure) as in the interrupt handler.

I do have a reasonably elaborate structure.

I call a C++ routine, CPP_LINK (more or less trampoline, if you want) before the main FreeRTOS loop.  I increase the main delay on the FreeRTOS loop to about 10 seconds.  It's not doing anything. 

the C++ routine (see CPP_LINK) sets up all the drivers and sets up the main C++ task.  Note (see example code) that the actual task is NOT owned by the class, and is separate.  Having the class own the task never worked.  Creating the task sets the passed argument to the location of the C++ class, note the use of "me" as a pointer in the task.  This allows accessing class variables for the instance of this class.  (see LED driver)

For LEDS, I'm using a direct drive from processor pins, and use #ifdef statements to determine the state of the pin for LED on.  The main.h file uses standard names for LED pins.

In this case, the driver has stubs to work with I2C and FPGA, controlled LEDS, and has routines to use Azure as well.  I gave up on AZURE, since it doesn't seem to work well with C++.

In summary:

Main calls CPP_LINK before the main task's endless loop

CPP_LINK sets up low level drivers (SPI, USART, low level display) and the application task.

APPLICATION sets up higher level tasks (overall display system as needed, system level LED drivers (LED.cpp is low level).

application programs call system level drivers for functions.  System level drivers can talk to hardware not on the board.

Hope this helps a bit

 

 

 

 

 

 

"Could you also elaborate a little more on "in the remote case" please? "

When you screw things up =) For example, the loop in MainApp.cpp is the main loop, but you somehow do this:

 

// MainApp.cpp
void MainApp()
{
  // setup ...

  while( 1 )
  {
     // some code ...

     return; // (or break;) Conceptual error: MainApp must never return.
   }
}

 

 "As well as, what instructions do you follow to use a C++ class in an embedded project?"

Anything related with C++, from templates to classes, having in the middle: namespaces, function overload, lambdas, user defined literals, qualifed enums, ... you name it!

Features list will become long very quickly and to give an example here can be clumsy, but you can visit my blog.

 

In this entry I talk about 7 C++ features that aren't related with classes:

 https://fjrg76.com/2023/04/22/7-tips-noo-cpp-en/

 

In this other entry you can see how to use C++ classes for embedded systems, in this case a display controller featuring the HC495 shift register:

 https://fjrg76.com/2022/01/29/display-d4595-en/

 

Actually, my blog is almost all about C++ and embedded systems, so feel free to find something that is interesting for you and see how I use C++.