2023-09-01 05:44 AM
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
Solved! Go to Solution.
2023-09-02 06:16 AM
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();
2023-09-01 06:02 AM
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) {
...
}
}
}
2023-09-01 11:59 PM
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.
2023-09-02 06:16 AM
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();
2023-09-04 09:57 PM - edited 2023-09-04 09:58 PM
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.
2024-05-17 07:49 AM
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"