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
Markus GIRDLAND
ST Employee

Have you converted the project?

By right-clicking the project and selecting "New -> Convert to a C/C++ Project (Adds C/C++ Nature)" you should be able to convert. And cube is supposed to still generate code for the project afterwards.

If you're starting a new project you can also select it during project creation.

Thank you for providing the solution, but it still haven't solved my problem.

Please see my clarification below

  • My project already supported the C++ feature.
  • I create a class "A" for example.0690X000008wTZWQA2.png
  • Then I include this class in main.c (if I renamed main.c to main.cpp when generating code, there will be another main.c be created)0690X000008wTZlQAM.png
  • Then click "Build", because it's "main.c", so it use gcc as default compiler and error is reported.0690X000008wTZvQAM.png

So my question is

  1. Can I force to use g++ to compile all the files(*.c, *.cpp) in this project? If yes, how to configure the STM32CubeIDE?
  2. Or can the code be generated into main.cpp, not just create a new main.c file?

I'm new to this IDE, please correct me if the question is not make sense...😀

I wasn't too sure about this myself so I've gotten some more information about it.

The way CubeMX works at the moment is that it forces you to use a main.c file and will only generate code to the main.c file.

You can set up a "myMain.cpp" with a "myMain()" function inside and immediately call it from "main.c", as a workaround.

Fahad Mirza
Associate II

I know it is an old thread. But I had the same issue. I initially started with a 'C' project and then wanted to turn it into 'C++' project. This is how it worked for me:

1) Right Click on the project file and select 'Convert to C++' (Note: The image shows convert to C because I already changed it to C++)

0690X00000A9bihQAB.jpg

2) Then I changed the main.c to main.cpp by right click on the main.c and click 'Rename'. This is important. DON'T change it manually. If you change using 'Rename' there shouldn't be any problem.

At least that worked for me.

SpinKernel
Senior

Thanks, that worked for me (I didn't rename the main.c file, which was my problem).

I still have issues with .h files not compiling correctly, i assume I have to name them .hpp? or .hh? I'll play with it now that i know.

Are you talking about including '*.h' that belongs to a '*.c' file? If so, you need ---- extern "C" ---- like this:

#ifndef DUMMY_H_
#define DUMMY_H_
 
#ifdef __cplusplus
 extern "C" {
#endif
 
 // Function declarations here
 
#ifdef __cplusplus
}
#endif
#endif /* DUMMY_H_ */

 If not, post your errors.

Harvey White
Senior III

Just got finished with this: What I've managed is.....

C++ can call C, but I've not had any success the other way around.

so the following:

1) I've rewritten all of my code into C++ (NOT main.c)

2) any C++ routine calls a C routine, but there are no C routines to call a C++ routine

3) I'm using FreeRTOS, which is written in C, but has the C++ nature included *if* the compiler is working in a C++ environment

so do the following:

1) create a basic project using main.c

2) create your main program application as a C++ loop, calling what you will

3) since this is FreeRTOS, you want a setup, which creates a task, then a task, that task is your main application loop

4) all these are in C++

5) I have a bridge routine in C++ that is called, that calls the create task for the main application loop, that's all it does

6) before the RTOS loop in main.c, call the bridge program to start your program. That application loop takes over the functions of the loop in the main program

7) rename (from the IDE) main.c to main.cpp

😎 program should run

9) to make any changes to the environment through cubeMX, rename main.cpp back to main.c

10) make changes as needed

11) rename main.c to main.cpp (if you don't, you get errors. Then rebuild and reindex the project)

12) any time you need to rework the cubeMX settings, you must rename main.cpp to main.c first, then name it back again.

All that is needed for CubeMX to do is to generate code and recognize that a main.cpp file is valid. That ought to run for this scenario.

Since CubeMX doesn't do this yet, you need to.

When I write C++ code, I do change the extensions to .cpp and .hpp

One thing I found is that it was very convenient to keep the c and cpp code in different directories

limit the c compiler to the C directories, and limit the C++ compiler to the C++ directories.

I've gotten errors when the C compiler mistakenly tries to understand a C++ file and cannot recognize C++ keywords.

rbarris
Associate II

I'm running into this same type of problem here in September of 2020...

Create a working project through the wizard, even checked the "C++" language box, all the ST Cube / HAL code is still in C but that's fine, I can write a couple glue functions and keep the C++ stuff in my own sources.

I add a single .cpp file to the project, I put it in CM7/Core/Src ("buzz.cpp").

I added a header file ("buzz_glue.h") and the main.c is able to include it and see the prototypes for the C glue functions.

But my .cpp file never gets compiled, and the link fails.

I know it's not getting compiled because I put some garbage and an "#error 1" in the .cpp and it's ignored.

As an aside, the "Convert to C++" menu item that keeps coming up in these threads, is gone in 1.4.2, or I don't know which thing I actually should click on.

As an experiment, I went back and (through the IDE) created a new source file in Src, "dummy.c", and put an #error 1 in it, dummy.c does get an attempt to compile which fails.

So the symptom that I have is that if I add a .cpp file to this project, it is ignored. Which leads me to believe that I don't really have a C++ project. I don't know how to create one, and I don't see the option to convert C to C++ afterward.

So

a) even though I asked for a C++ project in the setup dialog, it's not really acting like one

b) the C side seems to see the header I added

c) the CPP file I added is being ignored

d) the "convert to C++" menu option is nowhere to be found

[UPDATE - I found the "Convert to C++" menu item, I had the wrong tab open, you only get it when you have "project explorer" open on the left sidebar and not "C/C++ Projects" - will see if that unblocks me. ]

0693W000003QqGPQA0.png0693W000003QqGFQA0.png0693W000003QqGAQA0.png