Skip to main content
Associate II
September 1, 2023
Solved

C++ with STM32CubeIDE

  • September 1, 2023
  • 5 replies
  • 8489 views

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

Best answer by TDK

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();

 

5 replies

TDK
Super User
September 1, 2023

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""."
Associate II
September 2, 2023

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.

TDK
TDKBest answer
Super User
September 2, 2023

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""."
September 2, 2023

I understand your situation. STM32CubeIDE is primarily designed for C projects, so when you create a new project, it generates a main.c file by default. However, you can still work with C++ code in your STM32CubeIDE project, but there are a few steps you need to follow:

vermoegeninsider

  1. Create a C++ Source File: In STM32CubeIDE, you can create a C++ source file alongside the generated main.c file. Here's how to do it:

    • Right-click on your project in the Project Explorer.
    • Go to "New" -> "File."
    • In the "File name" field, enter a name for your C++ file, like my_cpp_code.cpp.
    • Make sure to select the "C++ Source File" type for the file.
    • Click "Finish" to create the C++ source file.
  2. Configure Build Settings:

    • After creating the C++ source file, go to your project's properties.
    • Under "C/C++ Build," go to "Settings."
    • In the "Tool Settings" tab, you will find options related to C and C++ compilation.
    • Make sure to set the "Source file extensions" to include .c and .cpp (e.g., *.c *.cpp).
  3. Add C++ Code:

    • Write your C++ code in the .cpp file you created. You can include your Arduino library and work with C++ as you normally would.
  4. Update main.c:

    • STM32CubeIDE will generate a main.c file by default. This file contains the main() function, which is the entry point for your code.
    • If you want to use C++ features in your main() function, you can do so. For example, you can create objects of C++ classes and call their methods from main().
    • Keep the main.c file as minimal as possible, and place most of your C++ logic in separate .cpp files.
  5. Regenerating Code via .ioc File:

    • If you need to regenerate code using the .ioc file, make sure to configure it to generate both C and C++ code files. You can do this by selecting the appropriate options in the STM32CubeMX configuration.

By following these steps, you can work with C++ code in an STM32CubeIDE project without constantly renaming files. Just keep in mind that the main.c file generated by STM32CubeIDE will still be part of your project, and you can use it to set up your hardware peripherals and initialize your C++ code as needed.

 
 
Associate II
September 5, 2023

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.

Visitor II
May 17, 2024

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"

KToot.1
Visitor II
February 11, 2025

Hi,

Renaming main.c to main.cpp works well.

Here is a trick to automate it.

  1. Create file ‘before.sh’ in project’s root directory as:
    mv ./Core/Src/main.cpp ./Core/Src/main.c
  2. Create file ‘after.sh’ in project’s root directory as:
    mv ./Core/Src/main.c ./Core/Src/main.cpp
  3. Start IOC configurator ( STM32CubeMX )
  4. In IOC ‘ProjectManager’ → ‘Code Generation’ fill ‘User actions’ → ‘Before Code Generation’ and ‘After Code Generation’ browsing scripts generated in 1. and 2.
  5. Now the renaming is automated and user changes in main.cpp are preserved during future STM32CubeMX code generation.
 
Associate III
April 7, 2025

Did yuou managed to create projects with it without any issues? 

KToot.1
Visitor II
April 8, 2025

Yes, I use this approach continuously.