2022-11-07 01:49 PM
Hi,
I have a STM32CubeIDE project and as far as I remember I checked the C++ box.
But when adding .cpp and .hpp files with a class definition, I get the message:
error: unknown type name 'class'
So it seems the compiler does not compile as C++.
How can I correct this?
2022-11-07 02:32 PM
> checked the C++ box
What C++ box?
In the "Project Explorer" window, right click on the project name. Near the bottom of the pop-up menu is either "Convert to C++" or "Convert to C". If you see "Convert to C++" then select that and it will enable the C++ compiler for that project. If you see "Convert to C" then it appears you already did that, and I'm not sure why it is not compiling your C++ files as C++.
Or - is it possible that you are including an .hpp file from a .c source?
2022-11-07 03:39 PM
It says "Convert to C". Thus it already is a C++ project.
And yes, I'm trying to include a .hpp file from the main.c. If that is not possible how can I use C++ classes then? STM32CubeMx generate the main.c. So I can not change that.
2022-11-08 01:26 PM
If you need to access "C" compatible parts of the .hpp file, then the .hpp needs to have sections like this for the C++ only code:
#ifdef __cplusplus
// Put C++ ONLY declataions here before the 'extern "C"' below
class DUMMY
{
// Class stuff here
};
extern "C" {
#endif
// Put "C" compatible defines and function prototypes here
int Some_C_Function( int a );
#ifdef __cplusplus
}
#endif
If instead you are wanting to use C++ class instances or call C++ functions from main.c you have two choices: