Showing results for 
Search instead for 
Did you mean: 

How to configure STM32CubeIDE to support C++ development?

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:


I've just been renaming main.cpp back to main.c any time I want to change the ioc. I rename back to main.c, let it generate it's code, then rename back to .cpp so things compile properly.

It's a bit of a pain, but it's what works for me.

I have forgotten to rename the .cpp to .c every now and then. Using the idea of a linking program lets you keep the .c file, and no renaming is needed. I opted for a permanent solution rather than "remember to do this each time."

Associate II


Il est possible de renommer le fichier main.c en main.cpp et de convertir ensuite le projet en C++.

Ensuite, il faut prendre le soin d'ajouter extern "C" juste après l'insertion des directives de préprocesseur #include afin de prendre en compte le langage C dans le fichier main.cpp

Le compilateur gcc compilera les fichiers d'extension .c et g++ compilera séparément les fichiers d'extension .cpp

J'espère que ce post résoudra le problème de nombre de programmeurs.

Cordiales salutations,


Associate II

Voici les principaux sources de mon projet C++

Cordiales salutations,


Associate II

Sorry for this necromancy, but this is simple problem I think.

A.h must be understandable for both C and C++ compilers.

In example below, Serial.h is C++ header with some classes and so on. A.h is included in main.c and both setup() and loop() functions are called in main.c.


#ifndef INC_A_H_
#define INC_A_H_
#include "main.h"
#ifdef __cplusplus
extern "C" {
#include "Serial.h"
void setup();
void loop();
#ifdef __cplusplus
#endif /* INC_A_H_ */


#include "A.h"
class CustomType {
void setup() {
void loop() {

I should point out an error that the code somewhat obscured.

CPP_LINK sets things up and then returns, so the main user loop does run.  The delay of 10000 clock ticks says that it will run, delay for 10 seconds, then run again.  If you wanted to put something there, it would work.  Structurally, however, I generally don't.