cancel
Showing results for 
Search instead for 
Did you mean: 

My STM32CubeIDE project does not compile C++ files.

CJans.1
Associate III

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?

3 REPLIES 3
Bob S
Principal

> 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?

CJans.1
Associate III

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.

Bob S
Principal

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:

  1. Have main() call another function that is defined in a separate c++ file but has its prototype surrounded by the 'extern "C"' structure shown above, which makes it callable by "C" functions.
  2. Rename your main.c to main.cpp. BUT - every time you regenerate code from CubeMX (or CubeMX built in to CubeIDE) it will re-generate main.c