2024-03-07 10:21 AM
Hi Everyone,
I am trying to create C++ project for NUCLEO-H745ZI-Q board. When I used the project wizard, even though I selected the C++ project, it creates it as a C project. Because of this, IDE can not recognize "class" keyword. How can I solve the problem?
Thanks in advance.
2024-03-07 11:29 AM
Hello @Catutigernos and welcome to the ST Community :smiling_face_with_smiling_eyes:.
I suggest you to take a look at the answers of this post. That should be helpful.
Best Regards.
STTwo-32
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-03-07 12:24 PM
STM32CubeMX is only set up to produce C code. You can't change this.
You can add C++ to your project if you want though. You'll need to convert the project to C++ by right clicking it and selecting Convert to C++. This basically just adds the C++ compiler step and will interpret *.cpp files as such.
2024-03-07 12:57 PM
Hi @STTwo-32,
Thanks for your response. Actually I tried these things before, but when I create new project, that worked. Only issue, when I change the configuration, the IDE regenerate the codes as main.c not main.cpp and everything I done is gone. Actually, I do not understand that if the IDE will generate the code as C, why there is an option for C++. The post, you mantioned, asked two years ago and we face same issue today. I suppose it is not important for ST, so they have not done fix. I create the project as C++, so I expect the all configuration is done automatically according to C++.
2024-03-07 01:42 PM
What you "expect" is not what ST implemented. Yes, it would be nice if CubeMX was able to generate cpp files, but it can't. If you use Cube to generate code, it will always create main.c. You can either live with that, and have main() call an application_main() (or some such function) is a separate cpp file that can do C++ things, or you manually rename (or copy, or merge) main.c to main.cpp every time to re-gen the code. Personally I just have main() from main.c call into a different cpp main function and be done with it.
2024-03-07 02:12 PM
Thanks for your response. I already selected the target language as C++, so when I click right, there is only "Convert to C" option. Selecting C or C++ not matter, always create main file as C.
2024-03-07 02:23 PM
Thanks for your response. Renaming is not issue. Problem is the IDE generate codes in every configuration changes, so I lose everything done in main file.
2024-03-07 02:43 PM - edited 2024-03-07 02:47 PM
Normally CubeMX/IDE won't touch files added by user. So, if you follow the advice of Bob S. your custom c++ files will stay. Once there were regression issues when CubeMX/IDE deleted user files on re-generation, this has been fixed long ago. If your added files are deleted, please report a bug. Otherwise, as Bob S. and TDK advised, call your C++ "main" function from the cube-generated main.c. The user code section /* USER CODE BEGIN WHILE */ is a good place to insert this call. User-added code between /* USER CODE BEGIN ... */ and /* USER CODE END ... */ lines is preserved by Cube. Code that you've left anywhere else will be lost.
2024-03-07 04:02 PM
CubeMX is not set up to generate main.cpp. There is no changing this. It is not intended to create main.cpp even if you select a C++ project.
The only way to add C++ code and retain the "generate code" functionality is to put your C++ in a separate file and call it from main.c.