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:

45 REPLIES 45

Is google broken today?? The option is -x c++

For example gcc -x c++ foo.c

You're quite the friendly lad, aren't you? Thank you!

CubeMX always creates a C file. FreeRTOS is a C program, the HAL drivers (and LL drivers) are C programs. I'll bet all of the provided routines are C.

CubeMX is C.

Renaming main.c to main.cpp does work, but you eventually have to automate it to keep from getting mixed up with the latest version (.c) and the version you tweaked (.cpp). Note also that CubeMX has to read your .c file to keep the user sections.

I'll disagree that calling the C++ from C is an ugly hack. Once done properly (at least through a bridging call), you don't have to worry about it ever again. Period. Your main.c stays main.c, your application code is in C++, and barring a few hiccups (or hiccoughs) with FreeRTOS and some other support routines, you're done.

I doubt that the support software will ever go to C++, there's a lot out there. FreeRTOS, because of design philosophy, may (likely will) never go to C++. You can write the equivalent of FreeRTOS (limited or otherwise) in C++, so that's not the problem.

I have a C++ project with graphics, FreeRTOS, SD cards, HAL drivers, touchscreen, SPI, I2C, serial, NRF networking. As much of it is C++ as possible, but FATFS, FreeRTOS, hal drivers, they're all C. It still works.

I don't think the situation will change with respect to C, C++. I do think that a good solution (at least to the main.c problem) is to allow CubeMX to make changes to main.cpp if it can't find main.c. It could be a special configuration box with all sorts of lawyer induced warnings but it would save the renaming problem.

Compiling C code with C++ compiler is like torturing yourself. One can write literally a few lines of code and solve this "problem" correctly and completely. Why torture yourself?

Since renaming the main.c to main.cpp worked I was just assuming there would be no problem compiling c code with a c++ compiler. That is what I use right now, renaming main.c I mean. I still have to find a solution I like. Just switching the compiler seemed like the easy way.

Renaming the files is even more of a torture. Examples of calling C++ from C are all around the internet. Harvey has written it in this forum multiple times, even in this topic. How can you NOT find it?

https://community.st.com/s/question/0D50X0000BFER0CSQX/cube32-feature-request-need-to-handle-cpp-files

Oh I've seen the example. That's a simple misunderstanding. That's not what my sentence meant. I'm looking for the solution I personally like the best. This might be the link example. Right now I seem to tend towards it.

Seems so. I created "C++" project using wizrd in STM32CubeIDE, but in the end got main.c anyway.

CubeMX (any variety) creates ONLY C code.

Adding a C++ nature to a project does not change any code at all. What it does is (when going to properties/build tools) add a section where you can configure the C++ compiler.

The C++ compiler works on .hpp and .cpp files, the C compiler wants to see .c and .h. Each compiler has separate configuration sections and separate paths.

Avoid changing main.c to main.cpp; the CUBEMX tools will not update main.cpp. The solution I use gets around that problem.

Oh, and a note: the do while loop in main.c does get executed, so the code with the delay(10000) is exactly what you want if you're not using that loop.

AKana.1
Associate II

hmm...

why not use standard c/c++ assotiation?

1) add new my_main.hpp:

#ifdef __cplusplus

extern "C"{

#endif

void my_cpp_main(void);

#ifdef __cplusplus

}

#endif

2) add new my_main.cpp:

#include <my_main.hpp>

void my_cpp_main(void){

// YOUR C++ "MAIN" CODE

for(;;);

}

3) add into generated main.c your header and function call:

...

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include "my_main.hpp"

/* USER CODE END Includes */

...

int main(void){

...

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

my_cpp_main(); /// <<<<<<<<<<<<<<<<< your c++ Infinite loop

// while (1)

 {

  /* USER CODE END WHILE */