cancel
Showing results for 
Search instead for 
Did you mean: 

C++ with STM32CubeIDE

Sarim-Jalil
Associate II

Hi,

So I have been trying for last few days to port a library from Arduino and I am using STM32CubeIDE to generate code for the peripherals as it would be quick for my work and I do not want to change the Arduino code much other than the required things. The library is purely written in C++.

I have created a C++ project in CubeIDE but it is generating main.c file. One solution that I found after searching on the internet was to rename the main.c to main.cpp. I did that and it worked, the compiler compiled ran the main.cpp successfully but whenever I change some settings through .ioc file it regenerates the main.c file and I have to move the changes from main.c to main.cpp. I am pretty sure that there have to be  a proper way to create c++ projects for stm32 boards. Suggest me whatever is the proper way to do this. Another solution that I found was to use main.c some how and call .cpp in main.c but I an unable to use that method because lack of understanding how that would work.

Kindly help me in this regard, TIA

1 ACCEPTED SOLUTION

Accepted Solutions

You can't include C++ headers in C code.

application.cpp:

extern "C" {

void my_new_main_function() {
  ...
}

}

in main.c:

// in declaration section
void my_new_main_function();

// before main while loop within main():
  my_new_main_function();

 

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

View solution in original post

5 REPLIES 5
TDK
Guru

You have identified the two options. CubeMX will not generate a main.cpp file.

To leave main.c as a C file and call a C++ function in another file, use "extern C" when you define the function being called:

<in application.cpp>

extern "C" {

void main_loop() {
  while (1) {
    ...
  }
}

}

 

If you feel a post has answered your question, please click "Accept as Solution".
Sarim-Jalil
Associate II

Sorry, but I am a bit confused, and may be I am unable to explain my situation. I have some header files which I made using Default C++ header template and I have some namespaces defined in them. when I include that header in the main.c it gives me error on the namespaces because the compiler is using the gcc compiler to build them and for the .cpp files in src folder its using the g++ compiler. I am unable to understand how can I use the described approach to use my hearder files...Can you please give an example using two three dummy files.That would be great help.

You can't include C++ headers in C code.

application.cpp:

extern "C" {

void my_new_main_function() {
  ...
}

}

in main.c:

// in declaration section
void my_new_main_function();

// before main while loop within main():
  my_new_main_function();

 

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

Hi, I don't know the practicality of the approach you are describing because I tried to do the similar things and that doesnt work. if there is such settings to generate c++ files from .ioc by just configuring the cubemx do let us know. I am accepting TDK's approach as that is widely used by community. If such settings exist I will accept your solution.

yasser-m
Associate

Hi,

Just rename main.cpp to main.c before generating the .ioc then you after generating the code from .ioc rename it again to main.cpp, make sure all your code between the "/* USER CODE BEGIN..." and "/* USER CODE END"